|
import arcpy |
|
import os, sys |
|
from arcgis.gis import GIS |
|
|
|
# set path based on which office I'm working at that day |
|
office = input("Please select office location: 1)UCSB or 2)Home Office [1/2]? : ") |
|
if office == "1": |
|
# UCSB office paths |
|
prjPath = r"G:\\GIS_Projects\\web\\utilities\\utilities_web_update.aprx" |
|
|
|
elif office == "2": |
|
# home office paths |
|
prjPath = r"D:\\UCSB_Data\\web\\utilities\\utilities_web_update\\utilities_web_update.aprx" |
|
|
|
# login credentials |
|
# password from locally stored profile |
|
# setup by |
|
# from arcgis.gis import GIS |
|
# GIS(profile = 'learn_user', password = 'areallygoodpassword') |
|
user = "pdbar" |
|
portal = "https://ucsb.maps.arcgis.com" |
|
gis = GIS(profile="learn_user") |
|
|
|
# not currently using the ID but I may in the future if errors continue to |
|
# arise from finding the wrong map file on agol |
|
mapList = [ |
|
["atlas_index", True, True, "UCSB DFSS", "c7810fc2256b4f4b95aa53d6df276e63"], |
|
["ChilledWater", False, False, "UCSB DFSS", "2f07cee025034e38bf2cc4fc0ff943b4"], |
|
["Electricity", False, False, "UCSB DFSS", "4f8eaeba6153496fb26b5188b7185de4"], |
|
["NaturalGas", False, False, "UCSB DFSS", "f7ede31bfcb845b9a41bbfcaf5abc654"], |
|
["PotableWater", False, False, "UCSB DFSS", "3f8fd607663d4be1ab3e06a4ecb23379"], |
|
["ReclaimedWater", False, False, "UCSB DFSS", "0b8dd79a41244e66aa6f97c7fd2ff75e"], |
|
["SeaWater", False, False, "UCSB DFSS", "62c0c6ffaeac464fada6f8600dc081b7"], |
|
["Sewer", False, False, "UCSB DFSS", "5b57472ccc154aca877be20cb2a1e825"], |
|
["Storm", False, False, "UCSB DFSS", "ad4d775128e0486e910f8c5d5501e5e1"], |
|
["Telecom", False, False, "UCSB DFSS", "cabc73190433448db7a3e52f95e2f704"], |
|
[ |
|
"UCSB Solar Arrays", |
|
False, |
|
False, |
|
"UCSB DFSS", |
|
"b9cf4141bb6144289f739c2161f4eb2d", |
|
], |
|
["UCSB_Light_Poles", False, False, "UCSB DFSS", "b65dc6e55dcc41379159231a0c942a8c"], |
|
] |
|
|
|
|
|
def update_fs(): |
|
# 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) |
|
# finds map in local aprx file that matches name given |
|
mp = prj.listMaps(sd_fs_name)[0] |
|
arcpy.mp.CreateWebLayerSDDraft( |
|
mp, sddraft, sd_fs_name, "MY_HOSTED_SERVICES", "FEATURE_ACCESS", "", True, True |
|
) |
|
arcpy.StageService_server(sddraft, sd) |
|
# Connect to your AGO portal |
|
print("Connecting to {}".format(portal)) |
|
# Find the SD, update it, publish /w overwrite and set sharing and metadata |
|
print("Search for " + sd_fs_name + " SD on portal…") |
|
sdItem = gis.content.search( |
|
"{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition" |
|
)[0] |
|
# check to see if names match before overwriting |
|
if sdItem.title == sd_fs_name: |
|
print( |
|
"Found SD: {}, ID: {} 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), "\n") |
|
else: |
|
print("passing") |
|
print(sd_fs_name, ": ", sdItem.title, " did not match") |
|
pass |
|
|
|
|
|
# loop through list of maps, set variables and run the main function |
|
x = range(len(mapList)) |
|
for n in x: |
|
l = mapList[n] |
|
sd_fs_name = l[0] |
|
shrOrg = l[1] |
|
shrEveryone = l[2] |
|
shrGroups = l[3] |
|
update_fs() |