-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodAGOL.py
More file actions
192 lines (160 loc) · 7.11 KB
/
modAGOL.py
File metadata and controls
192 lines (160 loc) · 7.11 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# ---------------------------------------------------------------------------
# modAGOL.py
# Created on: 1/11/2017
# Leon Scott, Town of Easton, MA
# Description:
# Shared functions related to publishing and updating items in ArcGIS Online
#---------------------------------------------------------------------------
import arcpy
from arcgis.gis import GIS
import xml.etree.ElementTree as ET
import base64
import os
import logging
# Function: metadata_to_list
# Description: Read metadata XML extracted from ArcCatalog
def metadata_to_list(metadatafile, thumbpath):
insummary = ""
incredits = ""
#intags = ""
# inuselimit = ""
logging.info("Start metadata_to_list")
#Get metadata from xml file
xmlparser = ET.XMLParser(encoding="UTF-8")
tree = ET.parse(metadatafile, parser=xmlparser)
root = tree.getroot()
dataIdInfo = root.find('dataIdInfo')
try:
metasummary = dataIdInfo.find('idPurp')
insummary = metasummary.text
except:
metasummary = 'There is no summary for this item.'
logging.info("Metadata missing purpose")
logging.info("Metadata - Purpose")
try:
metacredits = dataIdInfo.find('idCredit')
incredits = metacredits.text
except:
metacredits = 'There is not credit information for this item.'
logging.info("Metadata missing purpose")
logging.info("Metadata - credits")
# Create tags
intags = ''
try:
metaTags = dataIdInfo.find('searchKeys')
for keyword in metaTags.findall('keyword'):
if intags == '':
intags = keyword.text
else:
intags = intags + "," + keyword.text
except:
logging.info("Metadata missing tags")
logging.info("Metadata - tags")
inuselimit = ''
# Create constraints from Use and then Legal
try:
for resconst in dataIdInfo.findall('resConst'):
for const in resconst.findall('Consts'):
UseConst = const.find('useLimit')
if inuselimit == '':
inuselimit = UseConst.text
else:
inuselimit = inuselimit + "\n" + UseConst.text
except:
logging.info("Metadata missing use limits")
try:
for resconst in dataIdInfo.findall('resConst'):
for const in resconst.findall('LegConsts'):
LegalConst = const.find('othConsts')
if inuselimit == '':
inuselimit = LegalConst.text
else:
inuselimit = inuselimit + "\n" + LegalConst.text
except:
logging.info("Metadata missing legal constraints")
if inuselimit == '':
inuselimit = 'There are not access and use constraints for this item.'
logging.info("Metadata - use limits")
thumbdata = root.findall("Binary/Thumbnail/Data")
if thumbdata:
metathumbnail = thumbdata[0].text
with open(thumbpath,"wb") as f:
f.write(base64.b64decode(metathumbnail))
logging.info("Metadata - thumbnail")
metadatalist = [insummary,incredits,intags,inuselimit]
return metadatalist
# Function: update_featureservice
# Description: Update feature service item information and sharing
def update_featureservice(itemsummary, itemcredits, itemuselimits, itemtags, itemdesc, itemthumb_file, itemid, agol_url, agol_user, agol_pass, shared, share_everyone, share_org, share_groups):
item_properties = {"snippet": itemsummary,
"description": itemdesc,
"accessInformation": itemcredits,
"licenseInfo": itemuselimits,
"tags": itemtags
}
gis = GIS(agol_url, agol_user, agol_pass)
fs_items = gis.content.search("id:" + itemid + " AND owner:" + agol_user, item_type="Feature Service")
if fs_items:
fs_item = fs_items[0]
if os.path.isfile(itemthumb_file):
fs_item.update(item_properties, thumbnail = itemthumb_file)
else:
fs_item.update(item_properties)
if shared:
fs_item.share(everyone = share_everyone, org = share_org, groups = share_groups)
# Function: update_iteminfo
# Description: Update feature service item information
def update_iteminfo(itemsummary, itemcredits, itemuselimits, itemtags, itemdesc, itemthumb_file, itemid, agol_url, agol_user, agol_pass):
item_properties = {"snippet": itemsummary,
"description": itemdesc,
"accessInformation": itemcredits,
"licenseInfo": itemuselimits,
"tags": itemtags
}
gis = GIS(agol_url, agol_user, agol_pass)
fs_items = gis.content.search("id:" + itemid, item_type="Feature Service")
if fs_items:
fs_item = fs_items[0]
if os.path.isfile(itemthumb_file):
fs_item.update(item_properties, thumbnail = itemthumb_file)
else:
fs_item.update(item_properties)
# Function: createSD_and_overwrite
# Description: Creates a SD file from ArcGIS Pro, then updates an existing SD file in ArcGIS Online. The new SD file is published.
def createSD_and_overwrite(aprx_file, map_name, draft_sd, service_name, folder_name, edit_enabled, export_enabled,
itemsummary, itemtags, itemcredits, itemuselimits, final_sd, agol_url, agol_user, agol_pass, sd_itemid ):
logging.info("Start createSD_and_overwrite")
aprx = arcpy.mp.ArcGISProject(aprx_file)
maplist = aprx.listMaps(map_name)
if not maplist:
logging.info("Map not found in ArcGIS project file. \nMake sure a valid map exists.")
sys.exit()
m = maplist[0]
logging.info("Map found")
# create sd file
try:
logging.info("SDDraft: " + draft_sd)
logging.info("Service: " + service_name)
logging.info("AGOL folder: " + folder_name)
arcpy.mp.CreateWebLayerSDDraft(m, draft_sd, service_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS', folder_name, enable_editing = edit_enabled, allow_exporting = export_enabled, summary=itemsummary, tags = itemtags, credits = itemcredits, use_limitations = itemuselimits)
logging.info("Created draft sd")
arcpy.StageService_server(draft_sd, final_sd)
logging.info("Staged service")
except:
logging.info(arcpy.GetMessages())
sys.exit()
#Push sd file to AGOL
gis = GIS(agol_url, agol_user, agol_pass)
#Find SD file, then update or add, then publish
sd_items = gis.content.search("id:" + sd_itemid +" AND owner:" + agol_user, item_type="Service Definition")
#Parameters for editor tracking
pub_params = {"editorTrackingInfo" : {"enableEditorTracking":'true', "preserveEditUsersAndTimestamps":'true'}}
if sd_items:
sd_item = sd_items[0]
updatesuccess = sd_item.update({},final_sd)
if updatesuccess:
sd_item.publish(publish_parameters=pub_params, overwrite="true")
logging.info("SD file published")
else:
logging.info("SD item not found. \nMake sure a sd file exists.")
sys.exit()