3

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) 
mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked Jun 30, 2015 at 15:10
1
  • Remember to escape your backslashes. The workspace above should be workspace = "D:\\Jiawei default download" Commented Jun 30, 2015 at 17:47

1 Answer 1

2

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) 
answered Jun 30, 2015 at 15:29
4
  • Thanks! Seems easy enough but it gave me error message "ExecuteError: ERROR 999999: Error executing function." Trying to figure out Commented Jun 30, 2015 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. Commented Jun 30, 2015 at 15:49
  • Then what should I do to change output location? Commented Jun 30, 2015 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. Commented Jun 30, 2015 at 16:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.