Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to create a number of polygons in a loop from a raster. Essentially, the loop will call a function a number of times which needs to convert it to a polygon:

def myfunc(raster):

    #do some raster processing
    return arcpy.RasterToPolygon_conversion(raster, "in_memory\poly")

But the object it returns seemingly has nothing in it? The end result is I want to create a single shapefile from all the polygon features I will create. It seems silly to call RasterToPolygon, writing out a temporary shapefile for each polygon and then collecting them altogether as they will easily all fit in the RAM available.

Is there any way to output a Polygon geometry from RasterToPolygon ?. I.e, can I not do some thing like this:

poly = arcpy.RasterToPolygon_conversion(raster, arcpy.Polygon)
share|improve this question
    
Can you post the error message you are getting when you run the code above? –  bcollins Feb 12 at 15:40
    
For the second code snippet it does not return an error, it simply writes out a shapefile in the current or scratch workspace. the 'poly' variable returned is the path to the file. For the first code snippet it returns an object I seemingly cant do anything with? (there are no methods/attributes like in a Polygon object) –  James Feb 12 at 15:45

3 Answers 3

All Geo-Processing tools return a results object but many code samples don't show this. Your function is returning a results object because your code is assuming that it always executes without error. What your code should be is this:

res = arcpy.RasterToPolygon_conversion(raster,"in_memory/poly","NO_SIMPLIFY")

You can then query the results object to see if your code has executed, so something like:

if res.status == 4:
     arcpy.AddMessage("Conversion executed without error!")
else:
     # Burst into tears
     pass

Also your upload code is flawed as you keep overwriting in_memory/poly, you need to increment the "poly" featureclass name so you stack up all your polygons in memory then do your final merge. You can achieve the increment by having a global counter or you pass in a pre-computed featureclass name.

share|improve this answer
    
Good point on the erroring handling. –  bcollins Feb 12 at 16:12

one thing is to watch out for your "in_memory\poly", that string should be made "raw" by adding an r-prefix, or the back-slash will be interpreted as an escape character:

 return arcpy.RasterToPolygon_conversion(raster, r"in_memory\poly")

or

return arcpy.RasterToPolygon_conversion(raster, "in_memory\\poly")

I usually like to just use a variable so I can easily refer to the in memory file later:

out_polygon = r"in_memory\poly"
arcpy.RasterToPolygon_conversion(raster, out_polygon)

Edit/Update 1: If you are running this logic in a loop, then you should create a list of polygon outputs and then a final append to get all raster-to-polygon outputs in on feature class. Something like:

intermediate_outputs = []
for i in range(0,len(rasters),1):
    out_poly = "in_memory/poly{}".format(i)
    arcpy.RasterToPolygon_conversion(raster, out_poly)
    intermediate_outputs.append(out_poly)

final_output = os.path.join(OUTPUT_WORKSPACE, 'vectorized_rasters')
arcpy.Append_management(intermediate_outputs, final_output, "TEST","","")
share|improve this answer
    
Yes, that is not a problem, just sloppy typing on my part –  James Feb 12 at 15:47

An answer to my question: You can create an arcpy.Geometry() object and pass this to RasterToPolygon...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.