My data consists of two featureclasses:
- Points = points representing trees
- Polygons = Polygons representing % canopy area by area. Each polygon in the FC has a % canopy measurement in the attributes.
I am trying to accomplish the following:
- Select points underneath polygon features
- For the points under each polygon, delete X% of the points based on the polygon attribute
The screenshot (Figure 1) shows a ModelBuilder only tool called Iterate Feature Selection. What is the correct Python scripting method to iterate through features in a feature class in order to pass the feature off to the SelectLayerByLocation_management command?
Figure 2 shows the output of the select by location. All 4 layers are the same, which will be a problem when I try and delete points by the canopy % measurement.
This is what I have tried so far:
import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r'C:\temp_model_data\OutputData'
outWorkspace = env.workspace
# The polygons have canopy % data in attributes
polygons = r'C:\temp_model_data\CanopyPercentages.shp'
points = r'C:\temp_model_data\points_20_2012.shp'
if arcpy.Exists("pointsLayer"):
print "pointsLayer exists already"
else:
arcpy.MakeFeatureLayer_management (points, "pointsLayer")
print "pointsLayer created"
count = 1
#Create a search cursor to step through the polygon features
polys = arcpy.da.SearchCursor(polygons, ["OID@", "SHAPE@"])
for poly in polys:
# Create a name for the polygon features
count = count + 1
featureName = "polygon_" + str(count)
print featureName
# Select points that lie under polygons
arcpy.SelectLayerByLocation_management('pointsLayer', 'intersect', polygons)
arcpy.SaveToLayerFile_management('pointsLayer', outWorkspace + featureName + ".lyr", "ABSOLUTE")
# Add the random point selection script here...
# Delete selected points within each polygon based on the % canopy cover...
Figure 1 enter image description here
Figure 2 enter image description here
-
2I'm looking over you code but a quick point, your polygon names are going to start at 2; the count is incremented before the name is set. You'll either want to set count = 0 before the loop starts, or put the count = count + 1 (which you can shorten to count += 1) after you assign the feature name.HeyOverThere– HeyOverThere2013年12月05日 21:09:13 +00:00Commented Dec 5, 2013 at 21:09
3 Answers 3
Nick Ochoski is right about the SearchCursor, but there is a cleaner way to use it WITHOUT a while and manually calling next:
import arcpy
fc = "c:/data/base.gdb/roads"
field = "StreetName"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
print(row.getValue(field))
A SearchCursor in arcpy is the most direct route for accomplishing this:
import arcpy
fc = "c:/data/base.gdb/roads"
field = "StreetName"
cursor = arcpy.SearchCursor(fc)
row = cursor.next()
while row:
print(row.getValue(field))
row = cursor.next()
Note that you can use the where_clause property to perform your selection.
I think You can also add (to trevstanhope neat answer) a WITH for even cleaner code as it will delete cursor automatically after finishing
import arcpy
fc = "c:/data/base.gdb/roads"
field = "StreetName"
with arcpy.da.SearchCursor(fc) as cursor:
for row in cursor:
print(row.getValue(field))
-
I have used search cursors to access and get info on a feature, but can a geoprocessing be run on each row, or do you need to pull the ID for that row and then run a select by attribute using that ID and THEN run your geoprocessing?Rex– Rex2017年02月07日 22:41:53 +00:00Commented Feb 7, 2017 at 22:41