I am using for loops to loop through a list of hexagon feature classes, selecting those hexagons in each feature class that overlap hexagons in each of the other feature classes and then calculating conflict scores for each of the overlapping records. The conflict scores are derived from an Excel table containing names of the two feature classes being compared and the relevant conflict score. The code seems to work fine, with no errors, except that it populates all records with the relevant conflict score, not just those records that overlap (the selected records). The code below shows the last few lines of the code. fc1 and fc2 represent the two layers being compared; fieldnames 1, 2 and 3 are defined earlier in the code and are derived by modifying feature class and field names.
with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore"]) as cursor:
for row in cursor:
print("START ROWS)")
if row[0] == fieldname1 and row[1] == fieldname2:
score = row[2]
print(score)
arcpy.management.SelectLayerByLocation(fc1, "HAVE_THEIR_CENTER_IN", fc2, "", "NEW_SELECTION")
arcpy.management.CalculateField(fc1, fieldname3, score)
print(fc1, fieldname3, score)
Solved! Go to Solution.
So, unless I'm missing something, this should be a very simple fix.
with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore"]) as cursor:
for row in cursor:
print("START ROWS)")
if row[0] == fieldname1 and row[1] == fieldname2:
score = row[2]
print(score)
sel = arcpy.management.SelectLayerByLocation(fc1, "HAVE_THEIR_CENTER_IN", fc2, "", "NEW_SELECTION")
arcpy.management.CalculateField(sel, fieldname3, score)
print(fc1, fieldname3, score)
What was happening was you were making a selection on a layer, but not feeding the selection to calculate field.
Give this a shot and see how it goes.
Also not sure if you meant to post a picture, but if you did, it didn't come through.
I've never seen a select by location in a cursor but I'd guess you need to use the geometry of each cursor row - rather than fc1 and fc2 which I assume are your entire features.
Maybe try this but no idea if it would work. If not I'd prefer to compare with a geometry.disjoint on the SHAPE token.
#needs to be a feature layer if not already
arcpy.MakeFeatureLayer_management(fc1,"fc1_layer")
with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore", "SHAPE@"]) as cursor:
for row in cursor:
print("START ROWS)")
if row[0] == fieldname1 and row[1] == fieldname2:
score = row[2]
print(score)
arcpy.management.SelectLayerByLocation("fc1_layer", "HAVE_THEIR_CENTER_IN", row[3], "", "NEW_SELECTION")
arcpy.management.CalculateField("fc1_layer", fieldname3, score)
print(fc1, fieldname3, score)
You have to make the selection a variable, then feed that variable into calculatefield
So, unless I'm missing something, this should be a very simple fix.
with arcpy.da.SearchCursor(conflictScores, ["Activity1", "Activity2", "ConflictScore"]) as cursor:
for row in cursor:
print("START ROWS)")
if row[0] == fieldname1 and row[1] == fieldname2:
score = row[2]
print(score)
sel = arcpy.management.SelectLayerByLocation(fc1, "HAVE_THEIR_CENTER_IN", fc2, "", "NEW_SELECTION")
arcpy.management.CalculateField(sel, fieldname3, score)
print(fc1, fieldname3, score)
What was happening was you were making a selection on a layer, but not feeding the selection to calculate field.
Give this a shot and see how it goes.
Also not sure if you meant to post a picture, but if you did, it didn't come through.