Many organizations share public maps on ArcGIS online and have defined a process to update and synchronize the feature layers from their local data. One method is well defined using ArcMap; you select the option to overwrite the hosted feature layers when publishing your updates back to ArcGIS online. In 2013 and 2014 we explained how you could update your hosted feature services automatically, at a prescribed time, using Python. In the spirit of this incredibly popular workflow to schedule updates, this blog will provide simpler Python code to update services using ArcGIS Pro 1.4 and the new ArcGIS API for Python.
This post assumes you’ve already shared a web map from ArcGIS Pro to ArcGIS online. You’ll need to make note of the user name and password of the publisher account, the service name as well as the original project file (.aprx). When publishing a web map from ArcGIS Pro, the underlying feature layers may have _WFL1 appended to the name to ensure they are unique within organization. Investigate your hosted content and be certain of the service definition (.sd) and feature layer names before proceeding.
The first step is to install the ArcGIS API for Python; from within ArcGIS Pro, find and add the arcgis package using the Python Package Manager. Alternatively, open a Python command prompt from: Start > ArcGIS > ArcGIS Pro > Python Command Prompt and use the following command, agreeing to update packages (if necessary):
conda install -c esri arcgis
After installing the ArcGIS Python API you need to define the steps of your update process. Typically, this will involve one or more authors working on the local datasets that were shared to ArcGIS online. The source documents and data will be used to create a service definition (.sd) file using Python. At a prescribed time, perhaps every night at midnight, the local .sd file with your new data will be uploaded to ArcGIS online where it will be used to replace the hosted service.
Copy the following code and save it locally as a Python file (updatewebmap.py). This code creates the .sd file and upload/publishes it on ArcGIS online. A number of variables will need to be set within the script; the path to the project, service name username, password, metadata and sharing options when you create the file.
[sourcecode language=”python”]
import arcpy
import os, sys
from arcgis.gis import GIS
### Start setting variables
# Set the path to the project
prjPath = r”C:PROJECTSNightlyUpdatesNightlyUpdates.aprx”
# Update the following variables to match:
# Feature service/SD name in arcgis.com, user/password of the owner account
sd_fs_name = “MyPublicMap”
portal = “http://www.arcgis.com” # Can also reference a local portal
user = “UserName”
password = “p@sswOrd”
# Set sharing options
shrOrg = True
shrEveryone = False
shrGroups = “”
### End setting variables
# Local paths to create temporary content
relPath = sys.path[0]
sddraft = os.path.join(relPath, “WebUpdate.sddraft”)
sd = os.path.join(relPath, “WebUpdate.sd”)
# Create a new SDDraft and stage to SD
print(“Creating SD file”)
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, ‘MY_HOSTED_SERVICES’, ‘FEATURE_ACCESS’,”, True, True)
arcpy.StageService_server(sddraft, sd)
print(“Connecting to {}”.format(portal))
gis = GIS(portal, user, password)
# Find the SD, update it, publish /w overwrite and set sharing and metadata
print(“Search for original SD on portal…”)
sdItem = gis.content.search(“{} AND owner:{}”.format(sd_fs_name, user), item_type=”Service Definition”)[0]
print(“Found SD: {}, ID: {} n Uploading and overwriting…”.format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print(“Overwriting existing feature service…”)
fs = sdItem.publish(overwrite=True)
if shrOrg or shrEveryone or shrGroups:
print(“Setting sharing options…”)
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print(“Finished updating: {} – ID: {}”.format(fs.title, fs.id))
[/sourcecode]
You can run the code from command line with the following command.
C:Program FilesArcGISProbinPythonenvsarcgispro-py3python.exe c:mycodeupdatewebmap.py
Once you’re sure the script has properly updated your hosted feature service, you can use the command in a scheduled task. This blog has more information about creating a scheduled task.
Important Notes:
- The Python script needs to be run on a machine with a valid ArcGIS Pro license. The script uses arcpy to create and stage the data to be published.
- The Python script assumes the first map found within a project will be the one to publish. If you’re working in a project with more than one map, update the Python code or create a new project with only the map you wish to share.
- The associated service definition (.sd) file must exist on ArcGIS online, along with the hosted feature layers and web map. If either of these items are missing, you’ll need to publish a web map from ArcGIS Pro independent of this script before implementing the update workflow.
The ArcGIS API for Python provides many options and settings for working with your hosted content. Explore the options on publish and content for more fine grain control on your update process.
Commenting is not enabled for this article.