I would like to go through my feature layers in my ArcGIS Online account and download/save the feature layers as shapefiles to my computer. I've tried this tool and also this and others but with no success. Now I'm trying to load the feature layers from the catalog to TOC in ArcMap. It works fine, I can see them in the TOC and I can copy them manually using CopyFeatures tool, or by right-click and then data-export data. Because I have a lot of feature layers, I wrote a script that will iterate over the layers and save them as shp:
import arcpy
import os
destPath = r"C:\Users\data from AGOL"
mxd = arcpy.mapping.MapDocument(r"C:\Users\data from AGOL\Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if lyr.isFeatureLayer == True:
dest1 = os.path.join(destPath, str(lyr.name) + ".shp")
arcpy.CopyFeatures_management(lyr,dest1)
This works fine for some feature layers but for others it fails with the error:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/untitled1/copyFeatures.py", line 15, in <module>
arcpy.CopyFeatures_management(lyr, dest1)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 2429, in CopyFeatures
raise e
arcgisscripting.ExecuteError: ERROR 000210: Cannot create output C:\Users\data from AGOL\AOI_151213.shp
Failed to execute (CopyFeatures).
The following image show one of the problematic feature layer (AOI_151213) and one that is OK (AOI_2015):
The only difference I can think of is that the problematic one, contain some txt files, but I can save it manually and it's fine. This error rises only in the iteration process.
Is there a way to modify my code so that: - I will be able to save the problematic feature layer as shp file - OR at least to add a condition that will not terminate the script, but skip this error (and the following errors) - I'm not sure how to embed such a thing.
-
I am wondering why the attempted output filename is different from the layername? AOI_151213 vs AOI_151213477954. I heard before that shapefiles have some limitations in filename and filepath length, but I could not that documented anywhere, so not sure if it's true.najel– najel2018年01月10日 20:53:59 +00:00Commented Jan 10, 2018 at 20:53
-
hi, this is not the issue, I accidentally copied the error from one of my tries and not the first try. I edited the error.user88484– user884842018年01月11日 06:47:27 +00:00Commented Jan 11, 2018 at 6:47
-
The example you have given, is that exactly how you have been testing it? What if you tried a simpler path, with no spaces?Keagan Allan– Keagan Allan2018年01月11日 10:26:38 +00:00Commented Jan 11, 2018 at 10:26
-
What software is this? By that I mean is the arcpy from ArcGIS Desktop or Server, and what version? And are you running the script standalone or from within ArcGIS?najel– najel2018年01月11日 14:06:56 +00:00Commented Jan 11, 2018 at 14:06
-
The script was executed in pycharm, and GIS software is ArcMap 10.2.2 desktopuser88484– user884842018年01月11日 14:22:34 +00:00Commented Jan 11, 2018 at 14:22
1 Answer 1
I suspect that the problem was that there were 2 layers with the same name, but I'm not sure. Anyway, I modified my script so it will ignore all errors, because it is enough for what I'm trying to accomplished. The script that worked for me:
import os
import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\data from AGOL\Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
destPath = r"D:\data from AGOL"
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if lyr.isFeatureLayer == True:
d = lyr.name.encode('ascii', 'ignore')
file_name = d + ".shp"
dest1 = os.path.join(destPath, file_name)
print file_name
print type(file_name)
print dest1
try:
arcpy.CopyFeatures_management(lyr, dest1)
except:
pass
-
1Thanks for posting your solution. One not is that you may consider changing except: to except arcgisscripting.ExecuteError: so that you only pass over that specific error, and you may also want to print that error to the console or a log file so you know you're missing a layer.najel– najel2018年01月11日 16:37:34 +00:00Commented Jan 11, 2018 at 16:37
-
Note that with this approach you can only pull features up to the 'Max Record Count' set on the server, 1000 at default I think.ericoneal– ericoneal2019年01月17日 19:48:41 +00:00Commented Jan 17, 2019 at 19:48
Explore related questions
See similar questions with these tags.