GetCount and Update Cursor not honoring selection

309
1
Jump to solution
11-14-2022 09:12 AM
Labels (1)
ChelseaVSmith
New Contributor II

I'm using arcpy, teaching myself so apologies for my code! 

I want the update cursor to input the number of selected rows from a feature class into the score field of another using this code. It all seems to be working apart from the selection is not being honoured, I'm expecting to see a raw score of 1 and actually I'm getting 879.

 

 

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-11-14 10:26:09
"""
import arcpy

# Set environments
arcpy.env.workspace = r"c:\Users\CVSmith\OneDrive - Fauna & Flora International\Projects\ENI_HBRA\projects\ENI_HBRA_Scratch\HBRA_Scratch.gdb"

def Model1():  # Model1

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = True

    arcpy.ImportToolbox(r"c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx")
    CulturalSites = r"c:\Users\CVSmith\OneDrive - Fauna & Flora International\Projects\ENI_HBRA\datasets\HBRA_data\whc_sites_2021_CulturalBuffer_MLW.shp"
    AU_Test = "AU_FR"
   
    # Process: Select Layer By Location (Select Layer By Location) (management)
    CulturalSites = arcpy.management.SelectLayerByLocation(CulturalSites, "INTERSECT", AU_Test, "", "NEW_SELECTION", "NOT_INVERT")

    # Add Fields for scoring
    arcpy.management.AddFields(AU_Test, [['F2_Raw', 'SHORT'], ['F2_Score','LONG']])

    # Count number of intersecting features for F2_Raw score using Update cursor and Get Count
    fields = ['F2_Raw']

    with arcpy.da.UpdateCursor(AU_Test,fields) as cursor:
        for row in cursor:
            row[0] = int(arcpy.GetCount_management(CulturalSites).getOutput(0))
            cursor.updateRow(row)

    del row
   
    # Process: Calculate Field (2) (Calculate Field) (management)
    arcpy.management.CalculateField(AU_Test, "F2_Score", "Score(!F2_Score!,!F2_Raw!)", "PYTHON3", """def Score(F2_Score,F2_Raw):
    if (F2_Raw > 0):
        return 5
    else:
        return 1""", "TEXT", "NO_ENFORCE_DOMAINS")
    
if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"c:\Users\CVSmith\OneDrive - Fauna & Flora International\Projects\ENI_HBRA\projects\ENI_HBRA_Scratch\HBRA_Scratch.gdb", workspace=r"c:\Users\CVSmith\OneDrive - Fauna & Flora International\Projects\ENI_HBRA\projects\ENI_HBRA_Scratch\HBRA_Scratch.gdb"):
        Model1()

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesWilcox1970
New Contributor III

I had an issue like this when running a Python tool. You would need to create a feature layer from your original feature class to make selections on, rather than the original feature class. That worked just fine for me, once I did that.

 

Edited for typos and clarity.

View solution in original post

1 Reply
JamesWilcox1970
New Contributor III

I had an issue like this when running a Python tool. You would need to create a feature layer from your original feature class to make selections on, rather than the original feature class. That worked just fine for me, once I did that.

 

Edited for typos and clarity.