-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGOL_item_updates.py
102 lines (77 loc) · 3.27 KB
/
AGOL_item_updates.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import getpass
from pathlib import Path
import arcgis
import arcpy
def define_service(project_path, map_name, layer_name, fc_path_str, item_name, temp_dir_path):
#: Reference a Pro project so that we can get a map and share it
print('getting Pro project and map...')
proj = arcpy.mp.ArcGISProject(str(project_path))
#: Select the right map from the project
for m in proj.listMaps():
if m.name == map_name:
agol_map = m
del m
#: Remove any other layers to create a single-layer hosted feature service
print('clearing existing layers...')
for l in agol_map.listLayers():
agol_map.removeLayer(l)
for t in agol_map.listTables():
agol_map.removeTable(t)
#: Add the desired data as a layer and give it the desired name
print('adding layer...')
layer = agol_map.addDataFromPath(fc_path_str)
# print(layer)
layer.name = layer_name
#: Save the project so that the changes stick.
proj.save()
#: Create paths for the service definition draft and service definition files
draft_path = Path(temp_dir_path, f'{item_name}.sddraft')
service_definition_path = Path(temp_dir_path, f'{item_name}.sd')
#: Delete draft and definition if existing
for file_path in (draft_path, service_definition_path):
if file_path.exists(): #: This check can be replaced in 3.8 with missing_ok=True
print(f'deleting existing {file_path}...')
file_path.unlink()
#: Create the service definition draft and stage it to create a service definition file
print('draft and stage...')
sharing_draft = agol_map.getWebLayerSharingDraft('HOSTING_SERVER', 'FEATURE', item_name, [layer])
sharing_draft.exportToSDDraft(str(draft_path))
arcpy.server.StageService(str(draft_path), str(service_definition_path))
#: Return the path to the service definition file
return service_definition_path
def publish(org, service_definition_path):
print('uploading...')
source_item = org.content.add({}, data=str(service_definition_path))
print('publishing...')
item = source_item.publish()
return item
def overwrite(org, service_definition_path, item_id, sd_id):
# item = org.content.get(item_id)
sd_item = org.content.get(sd_id)
print('updating...')
sd_item.update(data=str(service_definition_path))
print('publishing...')
sd_item.publish(overwrite=True)
portal = 'https://www.arcgis.com'
user = ''
fc_path_str = r''
#: id of the hosted feature service's AGOL item
item_id = ''
#: id of the service definition AGOL item for the feature service.
sd_id = ''
item_name = 'md_test'
layer_name = 'md_test'
map_name = 'Map'
temp_dir_path = Path(r'') #: Place to store the .sd and .sddraft files
new_item = False #: True to publish new item, False to overwrite existing data
project_path = Path(r'')
print('Getting AGOL organization object...')
org = arcgis.gis.GIS(portal, user, getpass.getpass())
print(f'Staging {fc_path_str}...')
service_definition_path = define_service(project_path, map_name, layer_name, fc_path_str, item_name, temp_dir_path)
#: publish if it doesn't exist yet
if new_item:
publish(org, service_definition_path)
#: otherwise, update using the item_id and sd_id above
else:
overwrite(org, service_definition_path, item_id, sd_id)