2

I want to transform some vectors to rasters in my folder but I have difficulties setting the output folder. The following code only allows me to output to the original database. I hope to output all results to the same folder instead of in their original database.

import arcpy
import os

workspace = "D:\Jiawei default download"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
    if "Subbasin" in filenames:
        filenames.remove('Subbasin')
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print feature_classes

for item in feature_classes[98:]:
   inFeatures = item
   valField = "wtdepaprjunmin"
   outRaster = item+"_r"
   print "processing "+item
# Execute PolygonToRaster
   arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster) 
  • Remember to escape your backslashes. The workspace above should be workspace = "D:\\Jiawei default download" – BruceDoh Jun 30 '15 at 17:47
1

You set a workspace, so any outputs from the script will default to that location unless you explicitly put them somewhere else. Create a folder for the outputs, then write your output rasters to that folder.

import arcpy
import os

workspace = "D:\Jiawei default download"
feature_classes = []
outputFolder = r'c:\outputfolder'
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
    if "Subbasin" in filenames:
        filenames.remove('Subbasin')
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print feature_classes

for item in feature_classes[98:]:
   inFeatures = item
   valField = "wtdepaprjunmin"
   outRaster = outputFolder+item+"_r"
   print "processing "+item
# Execute PolygonToRaster
   arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster) 
  • Thanks! Seems easy enough but it gave me error message "ExecuteError: ERROR 999999: Error executing function." Trying to figure out – April Jun 30 '15 at 15:41
  • Print your outRaster variable... I think you'll find it to be an invalid path. Using os.path.join() like you did earlier is really the best way to construct paths. – mr.adam Jun 30 '15 at 15:49
  • Then what should I do to change output location? – April Jun 30 '15 at 15:55
  • Well, because item looks like it'll be a full path to a feature class (not sure if it's in a gdb or if it's a shapefile), you need to pull just the basename of it, and then you need to decide what format you want the raster to be in. Because your out folder is not a geodatabase, I'd recommend creating tiffs. So: outRaster = os.path.join(outputFolder,os.path.splitext(os.path.basename(item))[0]+".tif") would probably do it. But, start by printing the variable, then read up on os.path.basename and os.path.splitext... that's where I'd recommend starting at least. – mr.adam Jun 30 '15 at 16:10

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.