ArcPy: Iterating Intersect_analysis over a shapefile of polygons (ArcGIS 10.3.1)

المشرف العام

Administrator
طاقم الإدارة
I have two shapefiles:

  1. A set of stream (vector line) data
  2. A fishnet of polygons
I want to know, for each polygon in that fishnet, what the total length of streams is within that polygon. So far, I've tried iterating the intersect_analysis tool over the fishnet table, summing the lengths of the output and writing that to the relevant field, but each iteration performs the intersect on the entire grid, not the selected polygon in that grid.

Code:

import arcpy#set some environmental variablesarcpy.env.workspace = "C:\Users\Alex\Documents\SoilMoisture"arcpy.env.overwriteOutput = 1#define variables for all necessary feature classes and fieldsstreams = "pathto/str_split_lamb.shp" ## shapefile of stream network split at vertices, in Lambert projection, with Length value field pre-calculatedrainGrid = "pathto/net_test.shp" ## polygons for rain cells, with empty "DRN_LENGTH" fieldcursor = arcpy.UpdateCursor(rainGrid) ## will iterate across every row of the rainfall grid featurefor row in cursor: intStreams = arcpy.Intersect_analysis([streams,rainGrid],"output.shp","ALL","","LINE") ## intStreams becomes all stream segments inside polygon strCursor = arcpy.SearchCursor(intStreams) ##iterate over streams in intersection totalLen = 0 for rowmore in strCursor: totalLen += rowmore.length ## sum all length values into one variable print totalLen # this is just here to check what's going on row.DRN_LENGTH = totalLen ## add value to field cursor.updateRow(row)print "Done!"Any ideas?



أكثر...
 
أعلى