0

This currently loops through my dataset and calculates the installation ID field from my dataset with the installation code from my Bragg Area feature that are within the polygon of each row. It works on the first feature in my dataset but on other features, in that dataset, it only calculates the features that fall into the last row of the Bragg feature class. The picture below shows how it looped through the monitor stations twice. It should have also shown the same for a couple of other features.

How do I get it to calculate every feature in my dataset within every row in the Bragg area as it does with the first feature?

import arcpy

# Set overwrite option
arcpy.env.overwriteOutput = True

# Define your workspace. This should be your SDE database with the feature class
arcpy.env.workspace = arcpy.GetParameterAsText(0)
fcZone = arcpy.GetParameterAsText(1)
aFieldID = arcpy.GetParameterAsText(2)
zFieldID = arcpy.GetParameterAsText(3)
flZone = arcpy.GetParameterAsText(4)
flAsset = arcpy.GetParameterAsText(5)
# This can be done once for all iterations
arcpy.MakeFeatureLayer_management(fcZone, flZone)

for fc in arcpy.ListFeatureClasses():
    fcs = fc
    fields = arcpy.ListFields(fcs, aFieldID)
    for field in fields:
        if field.name == aFieldID:
            # KEEPING ORIGINAL LAYERS (NOT CREATING ADDITIONAL "JOINED" LAYER)
            # Create FeatureLayers
            arcpy.MakeFeatureLayer_management(fc, flAsset)
            # Create a search cursor for the Area
            rows = arcpy.SearchCursor(flZone)
            for row in rows:
                # What you'll do is select each Area one at a time, and then select all the Features in that Area and calculate the field
                # NOTE: If you are using not using shapefiles, then you'll have to change the FID in the line below to OBJECTID (or similar)
                arcpy.SelectLayerByAttribute_management(flZone, "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
                arcpy.SelectLayerByLocation_management(flAsset, "WITHIN", flZone, "", "NEW_SELECTION")
                arcpy.CalculateField_management(flAsset, aFieldID, "'{0}'".format(str(row.getValue(zFieldID))), "PYTHON_9.3", "")
                arcpy.AddMessage(fcs)

enter image description here

2
  • 2
    Do you have a question? What are you trying to accomplish?
    – Tom
    Oct 18, 2019 at 15:45
  • Thank you ill update my message
    – Tony
    Oct 18, 2019 at 15:53

1 Answer 1

0

I thought maybe it was stuck on the last row of the feature because it was still selected so I tried clearing that selection and it worked

           # NOTE: If you are using not using shapefiles, then you'll have to change the FID in the line below to OBJECTID (or similar)
        arcpy.SelectLayerByAttribute_management(flZone, "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
        arcpy.SelectLayerByLocation_management(flAsset, "WITHIN", flZone, "", "NEW_SELECTION")
        arcpy.CalculateField_management(flAsset, aFieldID, "'{0}'".format(str(row.getValue(zFieldID))), "PYTHON_9.3", "")
        arcpy.AddMessage(fcs)
        arcpy.SelectLayerByAttribute_management(flZone, 'CLEAR_SELECTION', '#')

enter image description here

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.