-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpublish_service.py
More file actions
368 lines (303 loc) · 14.5 KB
/
publish_service.py
File metadata and controls
368 lines (303 loc) · 14.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os as OS
import sys as SYS
import getpass as GETPASS
import json as JSON
import tempfile as TEMPFILE
import time as TIME
import urllib as URLLIB
import urllib2 as URLLIB2
import zipfile as ZIPFILE
from xml.etree import ElementTree as ET
import xml.dom.minidom as DOM
import arcpy as ARCPY
import arcpy.management as DM
import arcpy.mapping as MAP
import requests as REQUESTS
import arcpy.server as SERVER
import arcpy.analysis as ANALYSIS
class AGOLHandler(object):
"""ArcGIS Online handler class."""
def __init__(self, username, password, serviceName):
self.username = username
self.password = password
self.serviceName = serviceName
self.token, self.http = self.getToken(username, password)
self.itemID = self.findItem("Feature Service")
self.SDitemID = self.findItem("Service Definition")
if self.SDitemID:
self.exists = True
else:
self.exists = False
self.hiveNumber = None
def findItem(self, findType):
"""Find the itemID of whats being updated."""
searchURL = self.http + "/search"
query_dict = {'f': 'json',
'token': self.token,
'q': "title:\""+ self.serviceName + "\"AND owner:\"" + self.username + "\" AND type:\"" + findType + "\""}
jsonResponse = send_AGOL_Request(searchURL, query_dict)
if jsonResponse['total'] == 0:
return None
else:
print("found {} : {}").format(findType, jsonResponse['results'][0]["id"])
return jsonResponse['results'][0]["id"]
def findItemURL(self, findType):
"""Find the item url."""
searchURL = self.http + "/search"
query_dict = {'f': 'json',
'token': self.token,
'q': "title:\""+ self.serviceName + "\"AND owner:\"" + self.username + "\" AND type:\"" + findType + "\""}
jsonResponse = send_AGOL_Request(searchURL, query_dict)
if jsonResponse['total'] == 0:
return None
else:
print("found {} : {}").format(findType, jsonResponse['results'][0]["id"])
return jsonResponse['results'][0]['url']
def getToken(self, username, password, exp=60):
"""Generates a token."""
referer = "http://www.arcgis.com/"
query_dict = {'username': username,
'password': password,
'referer': referer}
query_string = URLLIB.urlencode(query_dict)
url = "https://www.arcgis.com/sharing/rest/generateToken"
token = JSON.loads(URLLIB.urlopen(url + "?f=json", query_string).read())
if "token" not in token:
print(token['error'])
SYS.exit()
else:
httpPrefix = "http://www.arcgis.com/sharing/rest"
if token['ssl'] == True:
httpPrefix = "https://www.arcgis.com/sharing/rest"
return token['token'], httpPrefix
def publish(self, itemID):
"""Publish the existing SD on AGOL."""
publishURL = self.http+'/content/users/{}/publish'.format(self.username)
fs_id = self.findItem('Feature Service')
if fs_id:
self.delete_existing(fs_id)
query_dict = {'itemID': itemID,
'filetype': 'serviceDefinition',
'f': 'json',
'token': self.token}
jsonResponse = send_AGOL_Request(publishURL, query_dict)
print("successfully updated...{}...").format(jsonResponse['services'])
#### Get Hive Number ####
encodedURL = jsonResponse['services'][0]['encodedServiceURL']
if "services1.arcgis.com" in encodedURL:
self.hiveNumber = 1
elif "services2.arcgis.com" in encodedURL:
self.hiveNumber = 2
else:
self.hiveNumber = None
print "AGOL Hive Number: {0}".format(self.hiveNumber)
def delete_existing(self, item_id):
"""Delete existing feature service."""
deleteURL = self.http + '/content/users/{}/items/{}/delete'.format(self.username, item_id)
if not deleteURL == '':
query_dict = {'f': 'json', 'token': self.token}
jsonResponse = send_AGOL_Request(deleteURL, query_dict)
print("successfully deleted...{}...").format(jsonResponse['itemId'])
def upload(self, fileName, tags, description):
"""Overwrite the SD on AGOL with the new SD.
This method uses 3rd party module: requests.
"""
if self.exists:
updateURL = self.http+'/content/users/{}/items/{}/update'.format(self.username, self.SDitemID)
else:
updateURL = self.http+'/content/users/{}/addItem'.format(self.username)
#sd_id = self.findItem('Service Definition')
#if sd_id:
# delete_existing(sd_id)
filesUp = {"file": open(fileName, 'rb')}
url = updateURL + "?f=json&token="+self.token+ \
"&filename="+fileName+ \
"&type=Service Definition"\
"&title="+self.serviceName+ \
"&tags="+tags+\
"&description="+description
response = REQUESTS.post(url, files=filesUp);
itemPartJSON = JSON.loads(response.text)
if "success" in itemPartJSON:
itemID = itemPartJSON['id']
print("uploaded SD: {}").format(itemID)
return itemID
else:
print("\n.sd file not uploaded. Check the errors and try again.\n")
print(itemPartJSON)
SYS.exit()
def enrich(agol, service, output_service, rest_token):
service_name = """{{"serviceProperties":{{"name":"{}"}}}}""".format(output_service)
job_data = {"inputLayer": """{{"url":"{}"}}""".format(service),
"analysisVariables": ["LandscapeFacts.NLCDAgPt","Soils.MeanSoilRa","employees.N02_TOTEMP"],
"country":"US", "outputName": service_name, "f":"json"}
# Uncomment below when using angp portal
## analysis_url = "http://analysis1.arcgis.com/arcgis/rest/services/tasks/GPServer/EnrichLayer"
# Comment analysis_url below when using angp portal
#### Assure Hive Number ####
if agol.hiveNumber != None:
analysis_url = "http://analysis{0}.arcgis.com/arcgis/rest/services/tasks/GPServer/EnrichLayer".format(agol.hiveNumber)
else:
analysis_url = "http://analysis.arcgis.com/arcgis/rest/services/tasks/GPServer/EnrichLayer"
print "Analysis URL: {0}".format(analysis_url)
url = "{}/submitJob?token={}".format(analysis_url, rest_token)
headers = {"Accept":"*/*",
"Connection":"keep-alive",
"Content-Type":"application/x-www-form-urlencoded",
"Origin":"http://test-nossl.maps.arcgis.com",
"Referer":"http://test-nossl.maps.arcgis.com/home/webmap/viewer.html?useExisting=1",
"Host":"analysis.arcgis.com",
"Accept-Language":"en-US,en;q=0.8"}
# Uncomment headers below and comment out headers above when using angp portal
## headers = {"Accept":"*/*",
## "Connection":"keep-alive",
## "Content-Type":"application/x-www-form-urlencoded",
## "Origin":"http://angp.maps.arcgis.com",
## "Referer":"http://angp.maps.arcgis.com/home/webmap/viewer.html?services=83426143304141a3b0984bf6ec0d323f",
## "Host":"analysis1.arcgis.com",
## "Accept-Language":"en-US,en;q=0.8"}
request = URLLIB2.Request(url, URLLIB.urlencode(job_data), headers)
response = URLLIB2.urlopen(request)
json_data = JSON.load(response)
response.close()
return analysis_url, json_data
def check_job_status(analysis_url, json_data, rest_token):
if "jobId" in json_data:
job_id = json_data["jobId"]
job_url = "{}/jobs/{}?token={}".format(analysis_url, job_id, rest_token)
request = URLLIB2.Request("{}/jobs/{}?f=json&token={}".format(analysis_url, job_id, rest_token))
response = URLLIB2.urlopen(request)
json_data = JSON.load(response)
while not json_data["jobStatus"] == "esriJobSucceeded":
request = URLLIB2.Request("{}/jobs/{}?f=json&token={}".format(analysis_url, job_id, rest_token))
response = URLLIB2.urlopen(request)
json_data = JSON.load(response)
print(json_data)
if json_data["jobStatus"] == "esriJobFailed":
failed = StandardError("job failed")
response.close()
raise failed
elif json_data["jobStatus"] == "esriJobCancelled":
cancelled = StandardError("job cancelled")
response.close()
raise cancelled
elif json_data["jobStatus"] == "esriJobTimedOut":
timed_out = StandardError("job timed out")
response.close()
raise timed_out
TIME.sleep(10)
def make_sd_draft(MXD, serviceName, tempDir):
"""Ceate a draft SD and modify the properties to overwrite an existing FS."""
ARCPY.env.overwriteOutput = True
# All paths are built by joining names to the tempPath
SDdraft = OS.path.join(tempDir, "drought.sddraft")
newSDdraft = OS.path.join(tempDir, "droughtupdated.sddraft")
MAP.CreateMapSDDraft(MXD, SDdraft, serviceName, "MY_HOSTED_SERVICES")
# Read the contents of the original SDDraft into an xml parser
doc = ET.parse(SDdraft)
root_elem = doc.getroot()
if root_elem.tag != "SVCManifest":
raise ValueError("Root tag is incorrect. Is {} a .sddraft file?".format(SDDraft))
# Change service type from map service to feature service
for config in doc.findall("./Configurations/SVCConfiguration/TypeName"):
if config.text == "MapServer":
config.text = "FeatureServer"
#Turn off caching
for prop in doc.findall("./Configurations/SVCConfiguration/Definition/" +
"ConfigurationProperties/PropertyArray/" +
"PropertySetProperty"):
if prop.find("Key").text == 'isCached':
prop.find("Value").text = "false"
for prop in doc.findall("./Configurations/SVCConfiguration/Definition/Extensions/SVCExtension"):
if prop.find("TypeName").text == 'KmlServer':
prop.find("Enabled").text = "false"
# Turn on feature access capabilities
for prop in doc.findall("./Configurations/SVCConfiguration/Definition/Info/PropertyArray/PropertySetProperty"):
if prop.find("Key").text == 'WebCapabilities':
prop.find("Value").text = "Query,Create,Update,Delete,Uploads,Editing"
# Add the namespaces which get stripped, back into the .SD
root_elem.attrib["xmlns:typens"] = 'http://www.esri.com/schemas/ArcGIS/10.1'
root_elem.attrib["xmlns:xs"] ='http://www.w3.org/2001/XMLSchema'
# Write the new draft to disk
with open(newSDdraft, 'w') as f:
doc.write(f, 'utf-8')
return newSDdraft
def send_AGOL_Request(URL, query_dict):
"""Helper function which takes a URL
and a dictionary and sends the request."""
query_string = URLLIB.urlencode(query_dict)
jsonResponse = URLLIB.urlopen(URL, URLLIB.urlencode(query_dict))
jsonOuput = JSON.loads(jsonResponse.read())
wordTest = ["success", "results", "services", "notSharedWith"]
if any(word in jsonOuput for word in wordTest):
return jsonOuput
else:
print("\nfailed:")
print(jsonOuput)
SYS.exit()
def publish_service(agol, service_name, mxd_template, layer_file):
"""Publishe the service."""
# Create an sddraft file from the mxd.
sd_dir = TEMPFILE.mkdtemp()
mxd_temp = MAP.MapDocument(mxd_template)
mxd_temp.summary = service_name
mxd_temp.tags = service_name
layer = MAP.Layer(layer_file)
MAP.AddLayer(mxd_temp.activeDataFrame, layer)
mxd_temp.saveACopy(OS.path.join(sd_dir, '{}.mxd'.format(service_name)))
mxd = MAP.MapDocument(OS.path.join(sd_dir, '{}.mxd'.format(service_name)))
# Make the sd draft and enable feature server.
sd_draft = make_sd_draft(mxd, service_name, sd_dir)
# Stage the sddraft file.
SERVER.StageService(sd_draft, OS.path.join(sd_dir, "drought.sd"))
# Upload (publish) map service.
id = agol.upload(OS.path.join(sd_dir, "drought.sd"), "US Drought", "Current US Drought Conditions.")
agol.publish(id)
def drought_analysis(date_string):
ARCPY.env.overwriteOutput = True
working_dir = r"C:\Data\git\devsummit-14-python"
zip_name = "USDM_" + date_string + "_M.zip"
url = "http://droughtmonitor.unl.edu/data/shapefiles_m/" + zip_name
mxd_path = OS.path.join(working_dir, "MapTemplate.mxd")
lyr_template = OS.path.join(working_dir, "CurrentDroughtConditions.lyr")
zip_name = OS.path.basename(url)
drought_zip_file = URLLIB.URLopener()
dzf = drought_zip_file.retrieve(url, OS.path.join(r"C:\Temp", zip_name))
zf = ZIPFILE.ZipFile(dzf[0], "r")
shp_name = [n for n in zf.namelist() if n.endswith('.shp')][0]
zf.extractall(working_dir)
drought = OS.path.splitext(shp_name)[0]
DM.MakeFeatureLayer(OS.path.join(working_dir, shp_name), drought)
#### Add Winery Data ####
beerWinePath = OS.path.join(working_dir, "BeerWine",
"BeerWine.gdb", "BeerWine")
intermediate_output = OS.path.join(working_dir, "BeerWine",
"BeerWine.gdb", "BeerWineDrought")
wine = "BeerWine"
wine_drought = "Wine_Drought"
DM.MakeFeatureLayer(beerWinePath, wine)
DM.SelectLayerByAttribute(wine, "NEW_SELECTION", "Type = 'Winery'")
ANALYSIS.SpatialJoin(drought, wine, intermediate_output, "JOIN_ONE_TO_ONE", "KEEP_ALL")
try:
DM.DeleteField(intermediate_output, "NAME")
except:
pass
final_wine_drought = "Wine_Drought_Summary"
DM.MakeFeatureLayer(intermediate_output, final_wine_drought)
lf = DM.SaveToLayerFile(final_wine_drought,
OS.path.join(working_dir, '{}.lyr'.format(final_wine_drought)))
DM.ApplySymbologyFromLayer(lf, lyr_template)
pw = "PASSWORDHERE" #GETPASS.getpass("Enter AGOL password:")
service_name = "Drought_Wine_Service"
agol = AGOLHandler("USERNAMEHERE", pw, service_name)
publish_service(agol, service_name, mxd_path, lf[0])
TIME.sleep(5)
fs_url = agol.findItemURL('Feature Service')
TIME.sleep(35)
gp_url, jsondata = enrich(agol, fs_url + '/0', '{}_Enriched'.format(service_name), agol.token)
check_job_status(gp_url, jsondata, agol.token)
DM.Delete(OS.path.join(working_dir, shp_name))
DM.Delete(OS.path.join(working_dir, lf[0]))
if __name__ == '__main__':
date_string = "20140225"
drought_analysis(date_string)