2

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)
2
  • Can you post the error message you are getting when you run the code above?
    – bcollins
    Commented Feb 12, 2014 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
    Commented Feb 12, 2014 at 15:45

3 Answers 3

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.

1
  • Good point on the erroring handling.
    – bcollins
    Commented Feb 12, 2014 at 16:12
2

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","","")
1
  • Yes, that is not a problem, just sloppy typing on my part
    – James
    Commented Feb 12, 2014 at 15:47
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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