Hey all,
I've got an arcpy question for you. I'm still new to python as well but I think i have a decent handle on it. But i'm not sure why this script isn't working. It's pretty basic but I keep getting an error. Can someone point out the issue for me? I'd really appreciate it.
import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure2.mxd')
layers = arcpy.mapping.ListLayers(mxd)
for lyr in layers:
arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION")
arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', lyr+'.shp')
And the error i'm receiving
Traceback (most recent call last):
File "C:\Users\JOC-001\Desktop\ArcGIS.py", line 7, in <module>
arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION")
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6812, in SelectLayerByLocation
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.
ERROR 000840: The value is not a Raster Catalog Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByLocation).
Solved! Go to Solution.
I would add some print statements since it appears that the full path to the files, either input or output, are not being found. As a suggestion, it is useful to do testing in a path much shorter than the one you are currently using. Is there any reason you have to use r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb rather than something nicer like r"C:\Test\Test.gdb" it makes code debugging so much easier
I could be mistake but the only thing I'm pulling from the Default GDB is the one shapefile that i'm using to compare files to. Correct me if i'm mistaken? And i am out putting as a test to the file that once I get this script working I'll purge the file and start using the actual MXD that I need this script to process. The current one is just a subset of the data from the original.
I also just verified my file paths again. they are correct. Any other suggestions?
Do you have grouplayers in your mxd? If so this will produce errors since it will also be included in the list of layers.
I do have group layers. Thats why I scripted it the way I did. Any chance you could advise me on how to successfully iterate though them? I see what you mean though... I did a "for lyr in layers: print lyr and see why it's causing the error.
A group layer cannot be converted to shapefile. You will have to exclude them.
Maybe this:
import arcpy mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure2.mxd') layers = arcpy.mapping.ListLayers(mxd) for lyr in layers: if lyr.isFeatureLayer: arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION") arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', lyr+'.shp')
Right I knew that I just didn't think about it iterating the actual group too. It was a silly oversight on my part:/
So it's close I think. I had to fix the FC2FC a bit more but now i'm getting this error that provides no help at all 😕
Runtime error Traceback (most recent call last): File "<string>", line 9, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 1675, in FeatureClassToFeatureClass raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (FeatureClassToFeatureClass).
import arcpy mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure2.mxd') layers = arcpy.mapping.ListLayers(mxd) for lyr in layers: if lyr.isFeatureLayer: arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION") arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', str(lyr))
Are you positive that C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois exists, is a valid input, and is in the same coordinate system as your input layer?