1

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 intermediate STEPS 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.

enter image description here

When it should be giving me something like this (created from running manually in QGIS):

enter image description here

This is what my attribute table looks like:

enter image description here

What do I need to change in my script?

0

2 Answers 2

3

You are creating the same feature layer for each row, and the layer is the entire shapefile. Then rasterizing the shape.

Add a where clause to only make a feature layer of current cursor row and rasterize "Layer" :

...
with arcpy.da.SearchCursor(shp, PIDname) as sCur:
    for row in sCur:
        out_Raster = "PID{}.tif".format(row[0])
        sql = """{0} = {1}""".format(arcpy.AddFieldDelimiters(datasource=shp, field=PIDname), row[0])
        arcpy.MakeFeatureLayer_management(in_features=shp, out_layer="Layer", where_clause=sql)
        arcpy.PolygonToRaster_conversion("Layer", "COUNT", os.path.join(outfolder, out_Raster))
        arcpy.Delete_management("Layer")
...
0
0

And if it helps I have this Python code that uses arcpy 2.7 to create a new raster from a feature class of drone image footprints. The raster values are the number of overlapping images at each pixel.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.