How do I loop this to go through the every feature in the layer.

764
4
05-21-2018 09:32 AM
JesusJoffre
New Contributor II

I'm trying to loop this to go through the every feature in a layer. New to python and learning on the go here. This is the first time writing something so if see anything else let me know.

 

Import arcpy

...

... # Local variables:

... GasUtilNode_Segment = "GasUtilNode_Segment"

... GasUtilNode_Segment__3_ = GasUtilNode_Segment

... GasUtilNode_gControlFitting = "GasUtilNode_gControlFitting"

... GasUtilNode_gControlFitting_ = "\\\\zpstg07.mcip.usmc.mil\\roaming_profile\\home$\\geofi\\JJoffre\\Documents\\ArcGIS\\Default.gdb\\GasUtilNode_gControlFitting_"

...

...

...

... # Process: Select

... arcpy.Select_analysis ("GasUtilNode_gControlFitting", r'P:\YU_Restricted\Yuma_Admin\Projects\FY18\WO18_100_199\18-190_Gas_Utilities_Update_(Plicka_Joffre)\Gas_Utilites.gdb\UTILITIES_GAS\Fitting2', "OBJECTID = 1")

...

... # Process: Make Feature Layer

... arcpy.MakeFeatureLayer_management( "Fitting2",r'P:\YU_Restricted\Yuma_Admin\Projects\FY18\WO18_100_199\18-190_Gas_Utilities_Update_(Plicka_Joffre)\Gas_Utilites.gdb\UTILITIES_GAS\Fitting2Layer', '#', '#', "OBJECTID OBJECTID VISIBLE NONE;gasUtilNodeIDPK gasUtilNodeIDPK VISIBLE NONE;gasNodeType gasNodeType VISIBLE NONE;mediaID mediaID VISIBLE NONE;operationalStatus operationalStatus VISIBLE NONE;realPropertyNetworkIdentifier realPropertyNetworkIdentifier VISIBLE NONE;realPropertySiteUniqueID realPropertySiteUniqueID VISIBLE NONE;realPropertyUniqueIdentifier realPropertyUniqueIdentifier VISIBLE NONE;sdsFeatureDescription sdsFeatureDescription VISIBLE NONE;sdsFeatureName sdsFeatureName VISIBLE NONE;sdsMetadataID sdsMetadataID VISIBLE NONE;facilityConditionIndex facilityConditionIndex VISIBLE NONE;facilityIDFK facilityIDFK VISIBLE NONE;facilityNumber facilityNumber VISIBLE NONE;contractNumber contractNumber VISIBLE NONE;dateInService dateInService VISIBLE NONE;diameter diameter VISIBLE NONE;diameterUOM diameterUOM VISIBLE NONE;fittingType fittingType VISIBLE NONE;installationCode installationCode VISIBLE NONE;isCUI isCUI VISIBLE NONE;maximoIDFK maximoIDFK VISIBLE NONE;projectID projectID VISIBLE NONE;utilityOwner utilityOwner VISIBLE NONE;createdDate createdDate VISIBLE NONE;createdBy createdBy VISIBLE NONE;lastEditedBy lastEditedBy VISIBLE NONE;lastEditedDate lastEditedDate VISIBLE NONE;sdsID sdsID VISIBLE NONE;Shape Shape VISIBLE NONE")

...

... # Process: Select Layer By Location

... arcpy.SelectLayerByLocation_management('GasUtilNode_Segment', 'BOUNDARY_TOUCHES', r'P:\YU_Restricted\Yuma_Admin\Projects\FY18\WO18_100_199\18-190_Gas_Utilities_Update_(Plicka_Joffre)\Gas_Utilites.gdb\UTILITIES_GAS\Fitting2Layer', '#', 'NEW_SELECTION', 'NOT_INVERT')

...

... # Process: Copy Features

... arcpy.CopyFeatures_management(GasUtilNode_Segment__3_, results_shp, '#', "0", "0", "0")

...

... # Process: Get Count

... arcpy.GetCount_management(results_shp)

...

... # Local variables:

... lyrfile = r"P:\\YU_Restricted\\Yuma_Admin\\Projects\\FY18\\WO18_100_199\\18-190_Gas_Utilities_Update_(Plicka_Joffre)\\results.shp"

... result = arcpy.GetCount_management(lyrfile)

... count = int(result.getOutput(0))

... calcuFld = count

...

...

...

... # Process: Calculate Field

... arcpy.CalculateField_management ("GasUtilNode_gControlFitting","mediaID",calcuFld)

...

... # Process: Delete

... arcpy.Delete_management(r'P:\YU_Restricted\Yuma_Admin\Projects\FY18\WO18_100_199\18-190_Gas_Utilities_Update_(Plicka_Joffre)\results.shp', 'ShapeFile')

... arcpy.Delete_management("Fitting2")

... arcpy.Delete_management(r'P:\YU_Restricted\Yuma_Admin\Projects\FY18\WO18_100_199\18-190_Gas_Utilities_Update_

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Jesus, 

You can use a Search Cursor to iterate through your feature class.  You can specify "OID@" to retrieve the OBJECTID value.  You can add the rest of your code within this FOR loop.  Ex:

with arcpy.da.SearchCursor(fc, ["OID@"]) as cursor:    
  for row in cursor:      
    arcpy.MakeFeatureLayer(fc, "fcLyr", "OBJECTID = " + row[0])
    arcpy.SelectLayerByLocation‍‍‍‍‍‍‍‍‍...
    arcpy.CopyFeatures...
    arcpy.GetCount....
JoshuaBixby
MVP Esteemed Contributor

The only suggestion I would make is to create the feature layer outside of the loop.  I have found calling Make Feature Layer within a loop when only working with 1 data set tends to create both performance and memory management issues.

JesusJoffre
New Contributor II

Once I get it up and running I will create a feature layer outside of the loop. This computer is slow!!!!!!!!!!! 

0 Kudos
JesusJoffre
New Contributor II

Thank You I will work on it first thing tomorrow and probably run into more problems.

0 Kudos