I am using ArcMap 10.8.1.
I have shapefiles that are a series of overlapping polygons of drone imagery footprints. I want to loop through each shapefile, and for every shapefile loop through and rasterize every polygon/row in each of those shapefiles. I wrote some code that I adapted from here (Polygon to raster: Creating 1 raster for each polygon in feature class) where I:
- find a list of all the shapefiles that end in
"*footprints.shp"
in an intermediateSTEPS
folder - create an output subfolder in
STEPS\rasterfootprints
for each file I process to contain the rasterized footprints - SearchCursor through and create rasters of each polygon in the shapefile.
The following code executes:
import arcpy
import math
import os
root = r"C:\users\myusername\Desktop\ORTHO_FOOTPRINT_AUTOMATION" #change the filepath here
inputs = root + r"\INPUT_CSVS" #input directory of geotag CSVs
steps = root + r"\STEPS" #location of temporary file storage of intermediate processing steps
rfoots = root + r"\STEPS\rasterfootprints" #subfolder in steps to store the 100's of rasterized individual photo footprints
outputs = root + r"\OUTPUT_SHAPEFILES" #location of final
arcpy.env.workspace = steps
arcpy.env.overwriteOutput = True
shplist = arcpy.ListFiles("*footprints.shp")
for shp in shplist:
prefix = shp.rsplit('footprints.shp')[0]
os.makedirs(rfoots + '\\' + prefix)
outfolder= (rfoots + '\\' + prefix)
PIDname = "PID" #Sequential PhotoID field
InFeatures=shp
with arcpy.da.SearchCursor(shp, PIDname) as sCur:
for row in sCur:
out_Raster = "PID{}.tif".format(row[0])
arcpy.MakeFeatureLayer_management(shp, "Layer") # make a new layer
arcpy.PolygonToRaster_conversion(shp, "COUNT", os.path.join(outfolder, out_Raster))
arcpy.Delete_management("Layer")
But it seems to ignore the search cursor through the field "PID"
(type:Long) and produces the same raster of the entire area (i.e. a raster of ALL photo footprints) for every file that should be of a single photo.
When it should be giving me something like this (created from running manually in QGIS):
This is what my attribute table looks like:
What do I need to change in my script?