0

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):

enter image description here

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. – PhilippNagel Jan 10 '18 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 Jan 11 '18 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 Jan 11 '18 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? – PhilippNagel Jan 11 '18 at 14:06
  • The script was executed in pycharm, and GIS software is ArcMap 10.2.2 desktop – user88484 Jan 11 '18 at 14:22
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
  • 1
    Thanks 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. – PhilippNagel Jan 11 '18 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 Jan 17 at 19:48

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.