0

I am rewriting a 2.x script for compatibility with ArcGIS Pro and Python 3.x. My question is, how do I export a shapefile from a hosted feature layer from ArcGIS online using ArcPy (Python 3.x) and ArcGIS Pro?

The working python 2.x script written for ArcGIS Desktop does it this way:

mxd = arcpy.mapping.MapDocument("path_to_mxd_containing_online_layer.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0] 
### exporting the data to a local TEMP layer
inspector_layer = arcpy.mapping.ListLayers(mxd, "name_of_layer_in_mxd", df)[0]
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(wkspce, file_name)
 try:
 arcpy.CopyFeatures_management(lyr, dest1)
 except:
 pass

I was thinking of creating an aprx project file that contained a map with the layer in it, as that seems the closest to the original script. This is one way of writing it:

aprx = arcpy.mp.ArcGISProject("G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\Mapping\\Automation\\Auto_District_Mapping_PRO\\Auto_District_Mapping_PRO.aprx")
m = aprx.listMaps("name_of_map")
inspector_layer = m.listLayers('name_of_layer')
arcpy.CopyFeatures_management(inspector_layer, r"C:\\testfolder\\name_of_layer.shp")

I am getting the following error: 'list' object has no attribute 'listLayers' on the line that reads inspector_layer = m.listLayers('name_of_layer'), hence my question of how to do it correctly.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Mar 30, 2020 at 19:41

1 Answer 1

0

Using addDataFromPath function can add hosted feature layers using the service url.

The code looks like: inspector_layer = m.addDataFromPath('service_url_for_hosted_feature_layer')

answered Mar 30, 2020 at 20:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.