2

This is the first time I have posted a question on here so please have some patience! 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.

Thank you in advance for your help.

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)
  • Thank you for taking the time responding to my question. I've managed to get it to work with afalciano's code but I will try this one for the next batch I've got to do as it's a bit more succinct. Thank you again, as I said to afalciano this has saved me a lot of faffing about this afternoon not getting anywhere! – Catchment_Jack Nov 15 '13 at 12:59
  • I've updated the answer and added useful piece of code (I think so). – a_v_zhukov Nov 15 '13 at 14:07
  • 1
    Further optimizations: 4. Don't declare cell and months for each fc, because they are always the same; 5. Use calendar.month_name instead of the months array. – Antonio Falciano Nov 15 '13 at 16:09
  • When we need to obtain unique values from field, SearchCursor can be more useful :) – a_v_zhukov Nov 15 '13 at 16:25
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"
  • Thanks for the response afalciano. That solution does actually make the code run apart from it stops after the first output because the current output name is duplicated for each field. Would you know if there a way of including the field name used in the polygon to raster conversion in the output name? Thanks again though! – Catchment_Jack Nov 15 '13 at 12:27
  • I've updated the answer in order to change the output raster dataset names. It can be done more elegantly using a month list, but this should work the same. Hope this helps. – Antonio Falciano Nov 15 '13 at 12:43
  • Lovely stuff! That's worked a treat. Thank you for taking the time to respond you've saved me an afternoon of going around in circles! – Catchment_Jack Nov 15 '13 at 12:51
  • There's a more elegant and succinct solution in the latest update. :) – Antonio Falciano Nov 15 '13 at 14: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.