1

I have a shapefile, that has lots of individual rows. My goal is simple, to export each row as a raster. I am trying to use SearchCursor, to select each row and perform a feature to raster conversion. However I am not getting the desired output, so I'm wondering where is a mistake (the tool runs, but creates a nonsense big and all the same rasters)

I didn't find any relevant info on how to combine Cursor with geoprocessing tools.

My code so far...

... all the variables, enviroments setting...

with arcpy.da.SearchCursor(input_file, ["FID"]) as tab:
    for count, row in enumerate(tab):
        arcpy.FeatureToRaster_conversion(input_file, input_field, output_location + str(count) + ".tif", cell_size)

I guess this is not the right way to combine cursors with geoprocessing tools. How can I do this?

3
  • Cursors do not tamper with the selection environment, so if you want to iterate the features as features, you need to use a Layer and SelectLayerByAttribute
    – Vince
    Jun 9 at 17:58
  • Use oid token in cursor, select by current row oid, feature to raster. Or use modelbuilder with iterator
    – BERA
    Jun 9 at 18:10
2

Try something like this...

with arcpy.da.SearchCursor(input_file, ["FID"]) as tab:
    for row in tab:
        arcpy.MakeFeatureLayer_management(input_file, "MyLayer", "\"FID\" = {0}".format(row[0]))
        arcpy.FeatureToRaster_conversion("MyLayer", input_field, output_location + str(count) + ".tif", cell_size)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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