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.
Ok i fixed that but i'm still getting the error due to the files being in groups. I like Xander Bakker pointed out.
Try using "print lyr.name" under "for lyr in layers:" to see if you are getting any done.
Agriculture\Animal Aquaculture Facilities
Runtime error Traceback (most recent call last): File "<string>", line 10, 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).
This is what printing before the operation gives me. I think it has to do with how the layer A) has the group name attached. I'll need to parse that out. B) has spaces in the name, I'll need to parse that out as well i think.
if lyr.isGroupLayer: pass else: print lyr.name
i had to tweak it just a little for the output to work on mine... but you sir are a wizard
"Agriculture\Animal Aquaculture Facilities" is not a valid output name. The name combines the grouplayer name and the name of the layer,
You could use this:
import arcpy import os 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") out_name = "{0}.shp".format(os.path.split(lyr)[-1]) print out_name arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', out_name)
Most important is to know that the name of the layer might not be valid as a file name.
You and Wes Miller were on the same track. I knew that was the issue after adding the print statement but I didnt know that arcpy had an inherent option for dealing with it! You guys are great! Thank you so much!
I want to thank everyone who helped me! I really appreciate all of you taking the time to help me solve this!!