There's an external ArcGIS Server that I've been manually going into through ArcCatalog to pull down feature services and save them off as feature classes. I do this twice a month, so I would like to explore if it's possible to do this automatically with Python/ArcPy.

Is this possible?

Current process is that I save off 4 feature classes from 3 feature services. My process is to make a connection to the server in Catalog --> drag the FS from Catalog into my TOC --> right click on the layer of interest and save off as a feature class to my local GDB.

closed as off-topic by Vince, whyzar, Midavalo Jul 15 at 1:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Vince, whyzar, Midavalo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    How many features are you saving and what is your manual workflow for saving them? – Marcelo Villa Jul 12 at 14:20
  • 1
    Yes, it's possible. There are even examples in the documentation. Please Edit the question to provide clarifications. – Vince Jul 12 at 15:04

Quick Tips: Consuming Feature Services with Geoprocessing explains the various techniques to do this.

The absolute easiest and shortest solution would be to:

  1. Drag the feature service into ArcMap
  2. Right click and Save as Layer file (LYR)
  3. Run Copy Features on that LYR file:

arcpy.CopyFeatures_management('C:/temp/FeatureService.lyr', 'c:/temp/LocalCopy.shp")

Otherwise, give a read of the blog and see how you can use arcpy.FeatureSet to make a query and "Save" data.


You have two different questions here:

1) To save features from an ArcGIS Feature service you can query the service and then use the JSON To Features tool.

The most straightforward way to query a service in Python is using requests, which already comes with the ArcGIS Python installation. After querying the service you save a JSON file to your mahcine and then use the JSON To Features tool to convert that JSON file to features and save them inside a GDB. Here is a snippet that does this process by getting all the features from the service (it is up to you to do the query so you get the 4 feature classes you mentioned, if they don't represent all the features in the service).

import json

import arcpy
import requests

# arcpy env settings
arcpy.env.workspace = 'path_to_your_gdb'

# get data from rest service
params = {'where': '1=1', 'outFields': '*', 'f': 'pjson', 'returnGeometry': True}
r = requests.get('service_url/query', params)
data = r.json()

# save JSON as a Feature class
json_path = 'path_to_rest_service.json'
with open(json_path, 'w') as f:
    json.dump(data, f)
arcpy.JSONToFeatures_conversion(json_path, 'output_fc')

For information about all the parameters you can pass to the query be sure to read the ArcGIS REST API documentation for the query operation.

You would have to do this for each of the three services you are getting features from.

2) To automatically add the new feature to a DataFrame you have to use the arcpy.mapping module. First you need to specify the MXD and the DataFrame you are going to add the features. Second you have to create an arcpy.mapping.Layer() object for each Feature Class in the GDB. Finally you have to add the layer to the DataFrame using the arcpy.mapping.AddLayer() function. Here is a snippet showing this process for one Feature Class:

import arcpy

arcpy.env.workspace = 'path_to_your_gdb'

# specify MXD and Data Frame
mxd_fn = r'path_to_mxd'
mxd = arcpy.mapping.MapDocument(mxd_fn)
df = arcpy.mapping.ListDataFrames(mxd)[0]

# create layer from Feature Class and add it to the Data Frame
lyr = arcpy.mapping.Layer('my_feature_class')
arcpy.mapping.AddLayer(df, lyr, 'TOP')

# refresh view, TOC and save MXD

In case you wanted to remove previous layers from the Data Frame, use the arcpy.RemoveLayer() function.

Not the answer you're looking for? Browse other questions tagged or ask your own question.