Just Trying to Copy a Feature Service to a Local Geodatabase

250
2
Jump to solution
08-12-2019 02:38 PM
PhilipShutler1
New Contributor

I will preface this post, in saying that I am new to script workflows in Python, and I believe that this process is probably very simple to accomplish but keep getting an error.  

All I am trying to do is export/copy a feature service it to a local geodatabase where I can work on it locally.  

I have provided the code below. I keep getting an "Object: Error in executing tool" for the last line where I am trying to copy the feature. 

from arcgis.gis import GIS
from arcgis.features.use_proximity import create_buffers

import arcpy

#########################################################################################

GDB_Name = input("Name Geodatabase")

Folder_Input = input("Provide Folder Path of Project")

Item_ID = input("Provide item ID")


#########################################################################################

gis = GIS("http://arcgis.com", "XXXXXXXX", "XXXXXXXXXXX")

search_results = gis.content.search('{}'.format(Item_ID))

Trail_Data = search_results[0]

WorkspaceGDB = arcpy.CreateFileGDB_management(Folder_Input, '{}.gdb'.format(GDB_Name))
print("GDB created")

arcpy.env.workspace = r'{}'.format(WorkspaceGDB)

arcpy.CopyFeatures(Trail_Data, "Area_Trail_Data")
0 Kudos
1 Solution

Accepted Solutions
PhilipShutler1
New Contributor

Thanks, Luke Webb

I think I may have found a workaround. I changed my code to include and export and download of the feature service. This way I can essentially make a copy of a feature service and then plug it in locally from a workspace folder.   

# import modules
from arcgis.gis import GIS

import zipfile
from zipfile import ZipFile

#######################################################################################

# Input working folder for shapefile to download too
Folder_Input = input("Provide Folder Path of Project")

# Provide item id of feature service
Item_ID = input("Provide item ID")

# Input name for shapefile export
Shapefile_Input_Name = input("Name Shapefile for Export")

#######################################################################################

# formatting inputs to fit syntax
Folder_Path = r'{}'.format(Folder_Input)

Shapefile_Name = Shapefile_Input_Name + "_Shapfile"
#######################################################################################

# searching and returning feature service
gis = GIS("http://arcgis.com", "XXXXXXXXX", "XXXXXXXXXX")

search_results = gis.content.search('{}'.format(Item_ID))

Trail_Data = search_results[0]


#######################################################################################

# Exporting feature service to shapefile
Trail_Data.export("{}".format(Shapefile_Name), 'Shapefile', parameters=None, wait=True)

#search for new shapefile export
item = gis.content.search(Shapefile_Name)

#getting shapefile item
shp = gis.content.get(item[0].itemid)

# download shapefile item
shp_Download = shp.download(save_path=Folder_Input)

# unzip shapefile in working folder
with ZipFile(shp_Download, 'r') as zipObj:
zipObj.extractall(Folder_Input)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
2 Replies
LukeWebb
Occasional Contributor III

I must say it looks like you are doing very well with the pythons

Sadly, I dont think you can do what you are hoping, using the GIS API. This is largely for managing of "content" on the online ecosystem, and less involved with the actual raw data and map services behind the content.   (For example, if you clone a map layer, both copies still point to the same published REST service on ArcGIS Server) 

------------

I am not certain as haven't tried this workflow, but I think you have a data type issue, rather than a python.  It is likely that this code:

search_results = gis.content.search('{}'.format(Item_ID))

Trail_Data = search_results[0]

Probably returns a "ArcGIS Portal Layer",   this has properties such as the name, title, sharing information etc.  However, this type of data cannot be used inside a "Geo-Processing Tool".

Possibly someone else can but in here, but I dont think that the features on a map service, are exposed in this manner.   

(As an example, If you tried dragging the services layers manually into an arcmap window, then dragging those layers into a geoprocessing tool, I think you will find even this is not permitted, possibly if the layer is published as a feature service instead of a map service they may work?)

I think you would need to either:

  • Look into ArcGIS Offline editing and synchronisation.

Enable layers for offline mapping—Portal for ArcGIS | ArcGIS Enterprise 

  • create a geoprocessing tool, on the server, that exports it to a shapefile/gdb format, similar to the clip and ship example:

Geoprocessing service example: Clip And Ship—Documentation | ArcGIS Enterprise 

0 Kudos
PhilipShutler1
New Contributor

Thanks, Luke Webb

I think I may have found a workaround. I changed my code to include and export and download of the feature service. This way I can essentially make a copy of a feature service and then plug it in locally from a workspace folder.   

# import modules
from arcgis.gis import GIS

import zipfile
from zipfile import ZipFile

#######################################################################################

# Input working folder for shapefile to download too
Folder_Input = input("Provide Folder Path of Project")

# Provide item id of feature service
Item_ID = input("Provide item ID")

# Input name for shapefile export
Shapefile_Input_Name = input("Name Shapefile for Export")

#######################################################################################

# formatting inputs to fit syntax
Folder_Path = r'{}'.format(Folder_Input)

Shapefile_Name = Shapefile_Input_Name + "_Shapfile"
#######################################################################################

# searching and returning feature service
gis = GIS("http://arcgis.com", "XXXXXXXXX", "XXXXXXXXXX")

search_results = gis.content.search('{}'.format(Item_ID))

Trail_Data = search_results[0]


#######################################################################################

# Exporting feature service to shapefile
Trail_Data.export("{}".format(Shapefile_Name), 'Shapefile', parameters=None, wait=True)

#search for new shapefile export
item = gis.content.search(Shapefile_Name)

#getting shapefile item
shp = gis.content.get(item[0].itemid)

# download shapefile item
shp_Download = shp.download(save_path=Folder_Input)

# unzip shapefile in working folder
with ZipFile(shp_Download, 'r') as zipObj:
zipObj.extractall(Folder_Input)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos