2

I tried to iterate over the rows from a Shapefile with the search cursor. Every row is a polygon and I want to make a raster out of this polygon so I tried this with arcpy.PolygonToRaster_conversion. But it is not working. What did I wrong? This is what I have so far:

import os
import arcpy

arcpy.env.workspace = r"C:\Jul\birds_bear\BOTW.gdb" #path to workspace
arcpy.env.overwriteOutput= True
inputshp = r"C:\Jul\birds_bear\BOTW.gdb\kleiner100"
outraster= r"C:\Jul\birds_bear\BOTW.gdb"
rows = arcpy.SearchCursor(inputshp)
count= 0 
for row in rows:
    print row
    valField = "VALUE"
    outraster = os.path.join(outraster, "outRaster" + str(count))
    arcpy.PolygonToRaster_conversion(row, valField, outraster,"","","0,0833333")
    count = count + 1 

The shapefile has 16.400 rows. At the end I have to sum the overlapping raster with, I hope it will work for 16400 rasters, see my original question here . I can work with 10.0 and 10.2.

  • Do you get any error messages? Also, depending on your computer settings you might have to change the last parameter in arcpy.PolygonToRaster to use decimal point instead of comma. – Martin Oct 27 '14 at 12:03
3

Your cursor is returning a row object. If you look at the syntax section of the help file for the Polygon to Raster tool what do you see as the input data type? You need to supply the actual geometry (the polygon). You are using the older SLOWER cursor. Consider editing your code to use the cursor from the da module. There are plenty examples in the help and on the internet.

The more I looked at this code I realized it was flawed as you feed back outraster into self which would create a nonsense file name.

Below is the working code but for some dummy data I have, so you need to change the parameters accordingly.

import os
import arcpy

arcpy.env.overwriteOutput= True
inputshp = "sites_Buffer"
outFolder = "C:/scratch/"
rows = arcpy.SearchCursor(inputshp)
count = 0 
for row in rows:
    # Get ID and extract a single polygon to in_memory workspace
    fid = row.getValue("id")
    query = '"id" = ' + str(fid)
    arcpy.Select_analysis(inputshp,"in_memory/temp",query)

    # Create output name
    outRaster = os.path.join(outFolder, "out" + str(count))
    print outRaster

    # Export  to raster
    arcpy.FeatureToRaster_conversion("in_memory/temp","id", outRaster,10)
    count = count + 1 
  • That module did not come out until v10.1 (resources.arcgis.com/en/help/main/10.1/index.html#/…). – artwork21 Oct 27 '14 at 12:13
  • 1
    Also note from this answer, you can't convert a row's geometry directly into a raster, it first needs to be converted to a feature class by itself. – Menno Oct 27 '14 at 15:51
  • Thanks, Hornbydd. I also tried it with the select tool but I didn't know how to create the "query". You helped me a lot. I will try the code later on because at the moment my computer is blocked by another operation. Thanks. – Nora Oct 28 '14 at 8:35
  • It works brilliant! – Nora Oct 28 '14 at 8:56

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.