2

I've been trying to create a python script to loop through numerous shapefiles converting various fields to raster outputs.

I've been working off a lot of what has been said in this previous thread on the same issue -> Python loop Polygon to raster not working?

But can still not get it to work. I keep getting the dreaded 999999 error.

The code I've been trying is shown below:

#import arcpy module
import arcpy
from arcpy import env

#set working environment
env.workspace = "C:\\Users\\s175850\\Documents\\GIS\\GIS_Data\\Source_Methodology\\Pesticides\\Application_Term\\2008"
Dir = env.workspace

#List feature classes
fclist = arcpy.ListFeatureClasses()

#loop
for fc in fclist:

  output = Dir + "\\r" + fc + "_ras"
  cell = 5

  #process polygon to raster
  arcpy.PolygonToRaster_conversion(fc, "January", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "February", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "March", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "April", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "May", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "June", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "July", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "August", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "September", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "October", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "November", output, "CELL_CENTER", "", cell)
  arcpy.PolygonToRaster_conversion(fc, "December", output, "CELL_CENTER", "", cell)

print "finished polygon to raster"

I'm not sure if there would be issues with outlining the 12 polygon to raster conversions for the 12 fields like I have done so I tried it with just one polygon to raster run for one field name. I still managed to get the same error.

I am using ArcGIS 10.

2 Answers 2

2
  1. Try not to use concatenation for path. Use os.path.
  2. You are trying to create 12 rasters with the same name. Specify different names for rasters.
  3. Do you iterate shape files in folder? If you do, your output path contains "." in name.

You can try this:

for fc in fclist:
    cell = 5
    months =["January","February","March","April","May","June","July","August","September","October","November","December"]
    for month in months:
        output = os.path.join(Dir, ("r_" + fc.rstrip(".shp") + "_" + month + ".tif"))
        arcpy.PolygonToRaster_conversion(fc, month, output, "CELL_CENTER", "", cell)

And if you want to take unique values from field you can try this (field - it's your field from shape file, "MONTH" maybe):

rawValues = [row[0] for row in arcpy.da.SearchCursor(fc, (field))]
months = set(rawValues)
0
2

If your output raster are e.g. in GeoTIFF format, try to substitute the output variable with:

output = Dir + "\\r" + fc.rstrip(".shp")

or more simply:

import os
output = Dir + os.sep + fc.rstrip(".shp")

and then update the output raster dataset name, for instance:

arcpy.PolygonToRaster_conversion(fc, "January", output + "_Jan.tif", "CELL_CENTER", "", cell)
arcpy.PolygonToRaster_conversion(fc, "February", output + "_Feb.tif", "CELL_CENTER", "", cell)
#...and so on 

EDIT :

This is a "more elegant" solution:

import arcpy
from arcpy import env
import os, calendar

env.workspace = "C:/Users/s175850/Documents/GIS/GIS_Data/Source_Methodology/Pesticides/Application_Term/2008"
Dir = env.workspace   
fclist = arcpy.ListFeatureClasses()  

for fc in fclist:
    for month in calendar.month_name:
        output = os.path.join(Dir, 'r_' + fc.rstrip('.shp') + '_' + month + '.tif')
        arcpy.PolygonToRaster_conversion(fc, month, output, "CELL_CENTER", "", 5)

print "Finished polygon to raster"
0

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.