1

I am currently trying to convert 1 feature class with many rows of polygons into several separate rasters; 1 for each polygon.

For this, i am trying to use Arcpy and the tool Polygon to raster, iterating through the feature class' rows:

 cursor10m = arcpy.da.SearchCursor(buff10m, 
                          ["ORIG_FID", "SHAPE@", "RASTERVALU"])
 for row in cursor10m:
    arcpy.PolygonToRaster_conversion(row[1], row[2], "outraster"+str(row[0]))

When running the script like this, i get the error that the field "43.182" does not exist - 43.182 is the first value in the field "RASTERVALU". I have tried to replace "row[2]" with "RASTERVALU" but the same error happens (it says the field "RASTERVALU" does not exist). It is quite important that the RASTERVALU field value is transferred to the raster.

Do any of you know a smart workaround?

  • 1
    I recently answered a question for 'extracting each polygon' gis.stackexchange.com/questions/247818/… which could be easily modified to 'raster for each polygon', is this what you're after? – Michael Stimson Jul 27 '17 at 21:56
  • I agree with @MichaelStimson that you should use "Split By Attribute" functionality to both Select each feature into its own feature class and then Polygon To Raster each single feature class. – PolyGeo Jul 27 '17 at 22:05
2

Here is the script that I posted previously modified to create a raster for each polygon using the OID field name (FID for shapefiles, OBJECTID for geodatabase etc..) as the unique identifier. One must only supply the input polygon feature class and the output folder to store the rasters in.

import arcpy, sys, os

InFeatures = sys.argv[1]
OutFolder  = sys.argv[2]

d       = arcpy.Describe(InFeatures)
OIDname = d.oidFieldName # get the oid field name

with arcpy.da.SearchCursor(in_grid,OIDname) as sCur:
    for row in sCur:
        out_Raster   = "OutRaster{}.tif".format(row[0]) # put the FID into the name, type GeoTIFF
        where_clause = "{} = {}".format(OIDname,row[0]) # put the FID into the where clause
        # Executing Select
        arcpy.MakeFeatureLayer_management(InFeatures,"Layer",where_clause)    # make a new layer
        arcpy.PolygonToRaster_conversion("Layer","RASTERVALU",os.path.join(OutFolder,out_Raster))
        arcpy.Delete_management("Layer")                                        # clean up the used layer
  • 1
    Bad idea to start raster name with number – FelixIP Jul 27 '17 at 22:35
  • @FelixIP it can be, that's why the out_raster is named OutRasterXXXX.tif where XXXX is the unique ID of the polygon. In general it's good practice to get used to starting a file name with a letter as it's a definite no-no for some formats and could come back to bite. – Michael Stimson Jul 27 '17 at 22:40
  • Many thanks! the scripts works really well - i did change it slightly, but that is mostly to fit my own specific purpose. – Jonas Jul 30 '17 at 11:41

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.