I am fairly new to the ArcGIS development world. I am attempting to write a script that will have an end result of selecting features by proximity to a selected portion of a different feature. So far, my code executes the following steps (with varying degrees of success):
- Create a layer from selected features,
- Save the layer onto a disk location,
- Add that layer back to an ArcMap document,
- And then use the layer in a Geoprocessing tool (Select Layer By Location).
The reason I am attempting to do it in this order is that I am running my code from a python window external to the python window within an ArcMap document (i.e. the ArcMap document could be closed while I am running this code). Thus far, I have been able to create a temporary layer from a selection of an original layer, and save a copy of this temporary layer to a file geodatabase (I used the arcpy.SaveToLayerFile_management function to save as a .lyr file). When I look at the file geodatabase through the Windows file explorer, the temporary layer is successfully saved there. However, when I look in the ArcMap catalog, the .lyr file is not visible. On top of that, any time I try to use the arcpy.mapping.Layer function to call the layer, I get the error:
Traceback (most recent call last):
File "H:/Work/Python_Project/ExportGBSSDataV1.0/Spatial_Location_Test.py", line 46, in <module>
addLayer = arcpy.mapping.Layer(database + '\\' + temp_CSlyr + '2.lyr')
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\mixins.py", line 429, in __init__
super(LayerMixin, self).__init__(lyrfile)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\_base.py", line 47, in __init__
for arg in args))
ValueError: Object: CreateObject Layer invalid data source
What is going on here, and how do I fix it?
I’ve read other posts where the same error is encountered, but they were slightly different problems.
My Script is below:
# declaring initial variables and workspaces
arcpy.env.workspace = 'G:\\foo\\GDB.gdb'
database = 'G:\\foo\\GDB.gdb'
LRS_Loc = 'G:\\foo\\OtherGDB.gdb' #this layer was from a different Geodatabase
# Input for attribute to select
CS = raw_input('Enter the Highway Number and Control section with no spaces: ')
# create whereclause for MakeFeatureLayer_management function
CS_whereclause = '"ROADNAME" LIKE ' + "'" + str(CS) + "%'"
# Create variables for Layer names
temp_CSlyr = 'temp_CS'
BHLOClyr = 'Boreholes'
# Open mxd to work with Geoprocessing tools later
mxd = arcpy.mapping.MapDocument('G:\\bar\\Map.mxd')
#Create temporary layer from selected features
arcpy.MakeFeatureLayer_management(in_features=LRS_Loc + "\\HIGHWAYS",
out_layer=temp_CSlyr,
where_clause=CS_whereclause,
workspace=database)
# Save temporary layer to file Geodatabase
arcpy.SaveToLayerFile_management(temp_CSlyr, temp_CSlyr, "ABSOLUTE")
# Add Layer to map (code stops working here)
df = arcpy.mapping.ListDataFrames(mxd)
addLayer = arcpy.mapping.Layer(database + '\\' + temp_CSlyr + '2.lyr')
arcpy.mapping.AddLayer(data_frame=df,
add_layer=addLayer)
# Select Layer By Location Geoprocessing tool
arcpy.SelectLayerByLocation_management(in_layer=BHLOClyr,
overlap_type='WITHIN_A_DISTANCE',
select_features=temp_CSlyr,
search_distance='50' + " Meters")
del mxd, addLayer