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– BruceDoh2015年06月30日 17:47:09 +00:00Commented Jun 30, 2015 at 17:47
1 Answer 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 outApril– April2015年06月30日 15:41:05 +00:00Commented Jun 30, 2015 at 15:41
-
Print your
outRaster
variable... I think you'll find it to be an invalid path. Usingos.path.join()
like you did earlier is really the best way to construct paths.mr.adam– mr.adam2015年06月30日 15:49:46 +00:00Commented Jun 30, 2015 at 15:49 -
Then what should I do to change output location?April– April2015年06月30日 15:55:02 +00:00Commented 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 onos.path.basename
andos.path.splitext
... that's where I'd recommend starting at least.mr.adam– mr.adam2015年06月30日 16:10:29 +00:00Commented Jun 30, 2015 at 16:10