Skip to content

Instantly share code, notes, and snippets.

@nommuna2
Last active April 26, 2019 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nommuna2/15f33a5734db106957add566a83fb5fe to your computer and use it in GitHub Desktop.
Save nommuna2/15f33a5734db106957add566a83fb5fe to your computer and use it in GitHub Desktop.
(ArcGIS API for Python) Publish a feature layer from server to AGO idea
# Sample of how to copy a feature layer from a local ArcGIS server and publish it to AGOL.
import arcgis
from arcgis import features
from arcgis import GIS
from arcgis.gis import Item
import json
gis = GIS(username="username", password= "password")
#Turn Feature Layer to a Feature Set
feature_layer = features.FeatureLayer(url="https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0")
featureSet = feature_layer.query(where='1=1', out_fields= '*')
#Turn FeatureSet to JSON
FeatureSetAsjson = featureSet.to_json
#Write the JSON to a file on disk
with open(r'path to local\featureCollection.json', 'w') as f:
json.dump(FeatureSetAsjson, f)
#Add the file to AGO and publish
fcItem = gis.content.add({'type': 'Feature Collection', 'title': 'MyFeautrelayerFromFC'}, data= r'path to local\featureCollection.json')
fcItem.publish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment