2

I have a really basic script that selects an input Polygon by an Attribute and then does a series of Buffers on that, a select by location and then appends the result into a final output. I need to run this process through each feature individually. I created a tool parameter for the initial Select By Attribute, but how do I alter this script to loop through all features rather than manually altering the SQL statement?

Code below:

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# IndexMask_Standard_SQL.py
# Created on: 2015-09-02 09:01:40.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: IndexMask_Standard_SQL <Expression> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Script arguments
Expression = arcpy.GetParameterAsText(0)
if Expression == '#' or not Expression:
    Expression = "PageNumber = 11" # provide a default value if unspecified

# Local variables:
IndexPoly = "IndexPoly"
IndexPolys__3_ = IndexPoly
IndexPolys_Buffer5_Buffer_Mu1__3_ = IndexPolys__3_
IndexPolys_Buffer5 = "C:\\Users\\bwhiteley\\Documents\\ArcGIS\\Default.gdb\\IndexPolys_Buffer5"
IndexPolys_Buffer5_Buffer =  "C:\\Users\\bwhiteley\\Documents\\ArcGIS\\Default.gdb\\IndexPolys_Buffer5_Buffer"
IndexPolys_Buffer5_Buffer_Mu = "C:\\Users\\bwhiteley\\Documents\\ArcGIS\\Default.gdb\\IndexPolys_Buffer5_Buffer_Mu"
IndexPolys_Buffer5_Buffer_Mu1 = "IndexPolys_Buffer5_Buffer_Mu"
IndexMask_Export__3_ = IndexPolys_Buffer5_Buffer_Mu1__3_
IndexMask_Export = "C:\\Users\\bwhiteley\\Documents\\ArcGIS\\Default.gdb\\IndexMask_Export"

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(IndexPoly, "NEW_SELECTION", Expression)

# Process: Buffer
arcpy.Buffer_analysis(IndexPolys__3_, IndexPolys_Buffer5, "150 Feet", "OUTSIDE_ONLY", "ROUND", "NONE", "", "PLANAR")

# Process: Buffer (2)
arcpy.Buffer_analysis(IndexPolys_Buffer5, IndexPolys_Buffer5_Buffer, "5000 Feet", "OUTSIDE_ONLY", "ROUND", "NONE", "", "PLANAR")

# Process: Multipart To Singlepart
arcpy.MultipartToSinglepart_management(IndexPolys_Buffer5_Buffer,  IndexPolys_Buffer5_Buffer_Mu)

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(IndexPolys_Buffer5_Buffer_Mu, IndexPolys_Buffer5_Buffer_Mu1, "", "", "OBJECTID OBJECTID VISIBLE NONE;SHAPE SHAPE VISIBLE NONE;PageNumber PageNumber VISIBLE NONE;ScaleRelative ScaleRelative VISIBLE NONE;ScaleDescription ScaleDescription VISIBLE NONE;Rotation Rotation VISIBLE NONE;PageName PageName VISIBLE NONE;SHAPE_Length SHAPE_Length VISIBLE NONE;SHAPE_Area SHAPE_Area VISIBLE NONE;BUFF_DIST BUFF_DIST VISIBLE NONE;ORIG_FID ORIG_FID VISIBLE NONE;SHAPE_length SHAPE_length VISIBLE NONE;SHAPE_area SHAPE_area VISIBLE NONE")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(IndexPolys_Buffer5_Buffer_Mu1, "INTERSECT", IndexPolys__3_, "", "NEW_SELECTION", "INVERT")

# Process: Append
arcpy.Append_management("IndexPolys_Buffer5_Buffer_Mu", IndexMask_Export, "NO_TEST", "PageName \"PageName\" true false false 25 Text 0 0 ,First,#,IndexPolys_Buffer5_Buffer_Mu,PageName,-1,-1;SHAPE_Length \"SHAPE_Length\" false true true 8 Double 0 0 ,First,#,IndexPolys_Buffer5_Buffer_Mu,SHAPE_Length,-1,-1;SHAPE_Area \"SHAPE_Area\" false true true 8 Double 0 0 ,First,#,IndexPolys_Buffer5_Buffer_Mu,SHAPE_Area,-1,-1", "")
3

You'll want to use a search cursor that iterates through your feature layer and reads values in the PageNumber field.

Something like this:

with arcpy.da.SearchCursor (IndexPoly, "PageNumber") as cursor:
    for pageNumber, in cursor:
        Expression = "PageNumber = {}".format (pageNumber)
        arcpy.SelectLayerByAttribute_management(IndexPoly, "NEW_SELECTION", Expression)
        #etc etc etc
1

It sounds like you need the python equivalent to Model Builder's Iterate Feature Selection. See if this question/answer will help you What is Python equivalent of ModelBuilder's Iterate Feature Selection?

Otherwise, if you are trying to iterate through a series of feature classes, you can use the walk function.

1

I think you want a for loop to iterate through your polygon features.

So if you have the list of features in your Expression variable:

Expression = ...
if stuff:
   ...

for i in Expression:
  arcpy.Select
  arcpy.Buffer1
  arcpy.Buffer2
  ...
  arcpy.Append

That will run all your processes on each element in Expression (i). Use 'print i' if you want to see which one is being evaluated as it loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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