Select by Location Error

2314
2
Jump to solution
06-20-2016 11:40 AM
RachelAlbritton
Occasional Contributor III

I have a script that should select points by location, and then switch the selection.

I have made the input feature classes in-memory layers using the MakeFeatureLayer tool, and the print out says these have been made successfully, however, when the script runs the SelectLayerByLocation script I get the following error:

ExecuteError: Failed to execute. Parameters are not valid.

ERROR 000368: Invalid input data.

ERROR 000732: Selecting Features: Dataset S:/Rachel/Data/UTA_Data.gdb/newStopsFL does not exist or is not supported

Failed to execute (SelectLayerByLocation).

I've tried this using feature classes and shapefiles and converting those to in memory layers and both have failed. Any ideas?

import arcpy, os

#workspace & parameters
workspace = arcpy.env.workspace  = "S:/Rachel/Data/UTA_Data.gdb"
if not os.path.exists(workspace): os.makedirs(workspace)
arcpy.env.overwriteOutput = True

#Input Data
newStops = "S:/Rachel/Data/UTA_Data.gdb/newStopstest" #change to user input
clipBoundary = "S:/Rachel/Data/UTA_Data/Clip_Boundary.shp"


#Clip new stops dataset to campus boundary
clippedStops = workspace+"/clippedStops"
arcpy.Clip_analysis(newStops, clipBoundary, clippedStops)
print arcpy.GetMessages()+"\n"

#Count record #'s in both new and old sets
oldStopsCount = arcpy.GetCount_management(oldStops)
oldCntResults = int(oldStopsCount.getOutput(0))
print "There are "+str(oldCntResults)+" old stops.\n"
newStopsCount = arcpy.GetCount_management(clippedStops)
newCntResults = int(newStopsCount.getOutput(0))
print "There are "+str(newCntResults)+" new stops.\n"

if newCntResults < oldCntResults: 
#then stops from the old feature class need to be selected out and deleted

    #Convert new & old stops to a feature layer
    newStopsFL = workspace+"/newStopsFL"
    oldStopsFL = workspace+"/oldStopsFL"
    arcpy.MakeFeatureLayer_management(clippedStops, newStopsFL)
    print arcpy.GetMessages()+"\n"
    arcpy.MakeFeatureLayer_management(oldStops, oldStopsFL)
    print arcpy.GetMessages()+"\n"
                                 
    #Select by Location
    arcpy.SelectLayerByLocation_management(oldStopsFL,"INTERSECT",newStopsFL,0,"SWITCH_SELECTION")
    print arcpy.GetMessages()
    oldStopsCount2 = arcpy.GetCount_management(oldStopsFL)
    print oldStopsCount2

else:
    print "done"
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

So those are the name of the layers.  Layer names should not include a workspace path, just a layer name.   Change the code to:

# You need to create a variable named oldStops in your code:
    oldStops = workspace+"oldStops"  
    newStopsFL = "newStopsFL"  
    oldStopsFL = "oldStopsFL"  
    arcpy.MakeFeatureLayer_management(clippedStops, newStopsFL)  
    print arcpy.GetMessages()+"\n"  
    arcpy.MakeFeatureLayer_management(oldStops, oldStopsFL)  
    print arcpy.GetMessages()+"\n" 

The variable oldStops is nothing, since you never initialized a path to a feature class for that variable in your code, so it won't exist.

View solution in original post

2 Replies
RichardFairhurst
MVP Honored Contributor

So those are the name of the layers.  Layer names should not include a workspace path, just a layer name.   Change the code to:

# You need to create a variable named oldStops in your code:
    oldStops = workspace+"oldStops"  
    newStopsFL = "newStopsFL"  
    oldStopsFL = "oldStopsFL"  
    arcpy.MakeFeatureLayer_management(clippedStops, newStopsFL)  
    print arcpy.GetMessages()+"\n"  
    arcpy.MakeFeatureLayer_management(oldStops, oldStopsFL)  
    print arcpy.GetMessages()+"\n" 

The variable oldStops is nothing, since you never initialized a path to a feature class for that variable in your code, so it won't exist.

RachelAlbritton
Occasional Contributor III

Thanks Richard - that was it

0 Kudos