0

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

3

There is an issue with this line:

arcpy.SaveToLayerFile_management(temp_CSlyr, temp_CSlyr, "ABSOLUTE")

Since no directory is specified the layer file is created in some unknown location - from all my abilities to discern it's created in memory somewhere.

You then try to make a mapping layer by looking in a file geodatabase:

addLayer = arcpy.mapping.Layer(database + '\\' + temp_CSlyr + '2.lyr')

File geodatabases can't house layer files. Generally layer files can only be housed in folders. Your case, since no folder is specified when the layer file is created, may be the exception.

Here's an edit to your script which references the layer 'in memory' (or wherever it may be) to create your mapping layer:

# 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)
##### remove gdb path##############################
addLayer = arcpy.mapping.Layer(temp_CSlyr + '.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
  • Thanks @Emil Brundage! Your insight about specifying directories was really helpful. While your code edit did not quite solve my problem, I specified a file directory to save the .lyr file that wasn't my geodatabase, and the code worked beautifully: arcpy.SaveToLayerFile_management(in_layer=temp_CSlyr, out_layer=FileLocation + '\\' + temp_CSlyr + '.lyr', is_relative_path="ABSOLUTE") – The Querying Intern Apr 11 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.