Select to view content in your preferred language

Select by Layer by Location Returns Different Values between Pro and Python

397
2
01-08-2024 10:57 AM
RemyShipman
New Contributor

Hello!

I am currently working on writing a script that works in conjunction with a layer that tracks the locations of snow plows during snowy weather. For this we have a point feature class for the snow plows and a line feature class for streets. The basic idea is that the script will run a Select by Location, with the overlap type set to intersect, to select the street lines and run a Calculate Field to change the symbology. The main issue I'm dealing with currently is that the script returns a different number of selected features than what I get when I do a manual Select by Location. It seems that the script selects all of the features within the streets class and gives a result of 1,496. Whenever I do it by hand I get the correct number of 6. I am currently using local copies of both feature classes for testing purposes so I know that only 6 features should be selected. I am at a loss for why the script would be picking up all of the line features with the selection and any help would be appreciated.  Here is my script:

import sys, arcpy, traceback
try:
    # set workspace
    arcpy.env.workspace = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb"

    # set variables
    snow_routes = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\LOCAL_Snow_Routes"
    snow_plows = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\LOCAL_Verizon_Vehicles"
    export_class = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\SnowPlows_Export"

    # check if export class exists
        #if exists delete
    print("Checking for existing export class...")
    if arcpy.Exists(export_class):
        arcpy.Delete_management(export_class)
        print("Existing export class deleted.")

    # export points from snow plow class
    # arcpy.conversion.ExportFeatures(in_features, out_features, {where_clause}, {use_field_alias_as_name}, {field_mapping}, {sort_field})
    expression = "plowname IS NOT NULL"
    print("Running export on Snow Plow feature class...")
    arcpy.conversion.ExportFeatures(snow_plows, export_class, expression)
    print("Export completed.")

    # run intersect on exported points w/ center lines
    print("Checking intersections...")
    arcpy.SelectLayerByLocation_management(snow_routes, "INTERSECT", export_class)
    theCount = int(arcpy.GetCount_management(snow_routes).getOutput(0))
    print(f"{theCount} features selected.")

    # calculate field of selected center lines
    print("Calculating field in the Snow Routes layer...")
    arcpy.management.CalculateField(snow_routes, "PlowStatus", 1, "PYTHON_3")
    print("Field calculated.")

    # delete export class
    arcpy.Delete_management(export_class)

 

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Does the ArcToolbox tools's  code (save as script) have anything different when compared to your version?


... sort of retired...
0 Kudos
PedroCoutinhoMendonça
Occasional Contributor

Hello!

 

I think you are counting the entire feature in line 28 and also using it to run the calculate field in line 33.

 

You can try to make a feature layer and use it to select and calculate:

 

 

import sys, arcpy, traceback
try:
    # set workspace
    arcpy.env.workspace = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb"

    # set variables
    snow_routes = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\LOCAL_Snow_Routes"
    snow_plows = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\LOCAL_Verizon_Vehicles"
    export_class = r"G:\Projects\SnowPlow\SnowPlow\SnowPlow.gdb\SnowPlows_Export"

    # check if export class exists
        #if exists delete
    print("Checking for existing export class...")
    if arcpy.Exists(export_class):
        arcpy.Delete_management(export_class)
        print("Existing export class deleted.")

    # export points from snow plow class
    # arcpy.conversion.ExportFeatures(in_features, out_features, {where_clause}, {use_field_alias_as_name}, {field_mapping}, {sort_field})
    expression = "plowname IS NOT NULL"
    print("Running export on Snow Plow feature class...")
    arcpy.conversion.ExportFeatures(snow_plows, export_class, expression)
    print("Export completed.")
    
    # Make a Feature Layer
    arcpy.management.MakeFeatureLayer(snow_routes, "snow_routes_lyr")

    # run intersect on exported points w/ center lines
    print("Checking intersections...")
    arcpy.management.SelectLayerByLocation("snow_routes_lyr", "INTERSECT", export_class, '', 'NEW_SELECTION')
    theCount = int(arcpy.GetCount_management("snow_routes_lyr").getOutput(0))
    print(f"{theCount} features selected.")

    # calculate field of selected center lines
    print("Calculating field in the Snow Routes layer...")
    arcpy.management.CalculateField("snow_routes_lyr", "PlowStatus", 1, "PYTHON_3")
    print("Field calculated.")

    # delete export class
    arcpy.Delete_management(export_class)

 

 

0 Kudos