-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOverwriteWebLayers.py
81 lines (67 loc) · 3.42 KB
/
OverwriteWebLayers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
###################################################################################
# #
# This script updates existing feature layers on AGOL by replacing the #
# associated service definition file with one newly generated from locally #
# hosted data. #
# #
# If this script is to run successfully, certain parameters must be met prior #
# to its execution. For complete documentation of the workflow, visit: #
# https://drive.google.com/open?id=1k6b3NOAo9TztbP1DHPB3J1UFNpGAA1Q8 #
# #
# Rachel Schultz | [email protected] #
# #
###################################################################################
# Import modules
import arcpy
import os, sys
from arcgis.gis import GIS
############################ BEGIN ASSIGNING VARIABLES ############################
# Set the path to the project
prjFolder = r"C:/GIS/Projects"
prjPath = os.path.join(prjFolder, ExampleProject.aprx)
# Set login credentials (user name is case sensitive, fyi)
portal = "https://www.arcgis.com/" # or use another portal
user = User.Name
password = Password!123
# Set sharing settings
shrOrg = # True or False
shrEveryone = # True or False
shrGroups = # Leave blank (" ") or list 'em
############################# END ASSIGNING VARIABLES #############################
# Assign name and location for temporary staging files
tempPath = prjFolder
sddraft = os.path.join(tempPath, "TempFile.sddraft")
sd = os.path.join(tempPath, "TempFile.sd")
# Connect to ArcGIS online
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
print("Successfully logged in as: " + gis.properties.user.username + "\n")
# Assign environment and project, and create empty dictionaries
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mapDict = {}
servDict = {}
# Populate map dictionary with map names and objects from earlier defined project
for map in prj.listMaps():
mapDict[map.name]=map
# Search for service definition files under the current user's account and populate
# service definition dictionary with names and ID numbers
sdItem = gis.content.search(query="owner: " + user + " AND type:Service Definition", max_items=100)
for serv in sdItem:
if str(serv.name).endswith(".sd"):
servDict[str(serv.name)[:-3]]=serv.id
# Iterate through maps in project and, if a matching service definition is found,
# overwrite that service definition with the data in the local map
for sdName, sdID in servDict.items():
for mapName, mapItem in mapDict.items():
if mapName == sdName:
updateItem = gis.content.get(sdID)
arcpy.mp.CreateWebLayerSDDraft(mapItem, sddraft, sdName, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS', True, True)
arcpy.StageService_server(sddraft, sd)
updateItem.update(data=sd)
print("Overwriting {}...".format(sdName))
fs = updateItem.publish(overwrite=True)
if shrOrg or shrEveryone or shrGroups:
print("Setting sharing options...")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print("Successfully updated {}.\n".format(fs.title))