-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverwrite_hosted_shape.py
396 lines (311 loc) · 15.4 KB
/
overwrite_hosted_shape.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'''
This script automates the process of updating an arcgis online hosted shapefile/feature layer.
The data is sent by email every week. This script is called after the email attachment (.zip
shapefile) is downloaded to a specified directory. The .zip is uploaded to AGO and overwrites
an existing hosted shapefile with the same name.
Automated using Windows Task Scheduler to run weekly.
Python 2.7
Credit to GIS StackExchange user KHibma's answer here:
https://gis.stackexchange.com/questions/208763/overwrite-feature-class-in-arcgis-online-using-python
Scroll to line 366 to begin entering your information
'''
import os, sys, time
import urllib, urllib2
import json, mimetypes
import gzip
from io import BytesIO
import string
import random
class AGOLHandler(object):
def __init__(self, username, password, serviceName, folderName):
self.headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': ('updatehostedfeatureservice')
}
self.username = username
self.password = password
self.base_url = "https://www.arcgis.com/sharing/rest"
self.serviceName = serviceName
self.token = self.getToken(username, password)
self.itemID = self.findItem("Feature Service")
self.SDitemID = self.findItem("Shapefile")
self.folderName = folderName
self.folderID = self.findFolder()
def getToken(self, username, password, exp=60):
referer = "http://www.arcgis.com/"
query_dict = {'username': username,
'password': password,
'expiration': str(exp),
'client': 'referer',
'referer': referer,
'f': 'json'}
token_url = '{}/generateToken'.format(self.base_url)
token_response = self.url_request(token_url, query_dict, 'POST')
if "token" not in token_response:
print(token_response['error'])
sys.exit()
else:
return token_response['token']
def findItem(self, findType):
""" Find the itemID of whats being updated
"""
searchURL = self.base_url + "/search"
query_dict = {'f': 'json',
'token': self.token,
'q': "title:\"" + self.serviceName + "\"AND owner:\"" +
self.username + "\" AND type:\"" + findType + "\""}
jsonResponse = self.url_request(searchURL, query_dict, 'POST')
if jsonResponse['total'] == 0:
print("\nCould not find a service to update. Check the service name in the settings.ini")
sys.exit()
else:
resultList = jsonResponse['results']
for it in resultList:
if it["title"] == self.serviceName:
print("found {} : {}").format(findType, it["id"])
return it["id"]
def findFolder(self, folderName=None):
""" Find the ID of the folder containing the service
"""
if self.folderName == "None" or folderName is None:
return ""
findURL = "{}/content/users/{}".format(self.base_url, self.username)
query_dict = {'f': 'json',
'num': 1,
'token': self.token}
jsonResponse = self.url_request(findURL, query_dict, 'POST')
for folder in jsonResponse['folders']:
if folder['title'] == self.folderName:
return folder['id']
print("\nCould not find the specified folder name provided in the settings.ini")
print("-- If your content is in the root folder, change the folder name to 'None'")
sys.exit()
def upload(self, fileName, tags, description):
"""
Overwrite the SD on AGOL with the new SD.
This method uses 3rd party module: requests
"""
updateURL = '{}/content/users/{}/{}/items/{}/update'.format(self.base_url, self.username,
self.folderID, self.SDitemID)
query_dict = {"filename": fileName,
"type": "Shapefile",
"title": self.serviceName,
"tags": tags,
"description": description,
"f": "json",
'multipart': 'true',
"token": self.token}
details = {'filename': fileName}
add_item_res = self.url_request(updateURL, query_dict, "POST", "", details)
itemPartJSON = self._add_part(fileName, add_item_res['id'], "Shapefile")
if "success" in itemPartJSON:
itemPartID = itemPartJSON['id']
commit_response = self.commit(itemPartID)
# valid states: partial | processing | failed | completed
status = 'processing'
while status == 'processing' or status == 'partial':
status = self.item_status(itemPartID)['status']
time.sleep(1.5)
print("updated SD: {}".format(itemPartID))
return True
else:
print("\n.sd file not uploaded. Check the errors and try again.\n")
print(itemPartJSON)
sys.exit()
def _add_part(self, file_to_upload, item_id, upload_type=None):
""" Add the item to the portal in chunks.
"""
def read_in_chunks(file_object, chunk_size=10000000):
"""Generate file chunks of 10MB"""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
url = '{}/content/users/{}/items/{}/addPart'.format(self.base_url, self.username, item_id)
with open(file_to_upload, 'rb') as f:
for part_num, piece in enumerate(read_in_chunks(f), start=1):
title = os.path.basename(file_to_upload)
files = {"file": {"filename": file_to_upload, "content": piece}}
params = {
'f': "json",
'token': self.token,
'partNum': part_num,
'title': title,
'itemType': 'file',
'type': upload_type
}
request_data, request_headers = self.multipart_request(params, files)
resp = self.url_request(url, request_data, "MULTIPART", request_headers)
return resp
def item_status(self, item_id, jobId=None):
""" Gets the status of an item.
Returns:
The item's status. (partial | processing | failed | completed)
"""
url = '{}/content/users/{}/items/{}/status'.format(self.base_url, self.username, item_id)
parameters = {'token': self.token,
'f': 'json'}
if jobId:
parameters['jobId'] = jobId
return self.url_request(url, parameters)
def commit(self, item_id):
""" Commits an item that was uploaded as multipart
"""
url = '{}/content/users/{}/items/{}/commit'.format(self.base_url, self.username, item_id)
parameters = {'token': self.token,
'f': 'json'}
return self.url_request(url, parameters)
def publish(self, summary, maxRecords):
""" Publish the existing SD on AGOL (it will be turned into a Feature Service)
"""
publishURL = '{}/content/users/{}/publish'.format(self.base_url, self.username)
query_dict = {'itemID': self.SDitemID,
'filetype': 'shapefile',
'overwrite': 'true',
'f': 'json',
'token': self.token,
'publishParameters' : {"name":self.serviceName,
'maxRecordCount':maxRecords,
'description':summary}
}
jsonResponse = self.url_request(publishURL, query_dict, 'POST')
try:
if 'jobId' in jsonResponse['services'][0]:
jobID = jsonResponse['services'][0]['jobId']
# valid states: partial | processing | failed | completed
status = 'processing'
print("Checking the status of publish..")
while status == 'processing' or status == 'partial':
status = self.item_status(self.SDitemID, jobID)['status']
print(" {}".format(status))
time.sleep(2)
if status == 'completed':
print("item finished published")
return jsonResponse['services'][0]['serviceItemId']
if status == 'failed':
raise("Status of publishing returned FAILED.")
except Exception as e:
print("Problem trying to check publish status. Might be further errors.")
print("Returned error Python:\n {}".format(e))
print("Message from publish call:\n {}".format(jsonResponse))
print(" -- quit --")
sys.exit()
def enableSharing(self, newItemID, everyone, orgs, groups):
""" Share an item with everyone, the organization and/or groups
"""
shareURL = '{}/content/users/{}/{}/items/{}/share'.format(self.base_url, self.username,
self.folderID, newItemID)
if groups is None:
groups = ''
query_dict = {'f': 'json',
'everyone': everyone,
'org': orgs,
'groups': groups,
'token': self.token}
jsonResponse = self.url_request(shareURL, query_dict, 'POST')
print("successfully shared...{}...".format(jsonResponse['itemId']))
def url_request(self, in_url, request_parameters, request_type='GET',
additional_headers=None, files=None, repeat=0):
if request_type == 'GET':
req = urllib2.Request('?'.join((in_url, urllib.urlencode(request_parameters))))
elif request_type == 'MULTIPART':
req = urllib2.Request(in_url, request_parameters)
else:
req = urllib2.Request(
in_url, urllib.urlencode(request_parameters), self.headers)
if additional_headers:
for key, value in list(additional_headers.items()):
req.add_header(key, value)
req.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(req)
if response.info().get('Content-Encoding') == 'gzip':
buf = BytesIO(response.read())
with gzip.GzipFile(fileobj=buf) as gzip_file:
response_bytes = gzip_file.read()
else:
response_bytes = response.read()
response_text = response_bytes.decode('UTF-8')
response_json = json.loads(response_text)
if not response_json or "error" in response_json:
rerun = False
if repeat > 0:
repeat -= 1
rerun = True
if rerun:
time.sleep(2)
response_json = self.url_request(
in_url, request_parameters, request_type,
additional_headers, files, repeat)
return response_json
def multipart_request(self, params, files):
""" Uploads files as multipart/form-data. files is a dict and must
contain the required keys "filename" and "content". The "mimetype"
value is optional and if not specified will use mimetypes.guess_type
to determine the type or use type application/octet-stream. params
is a dict containing the parameters to be passed in the HTTP
POST request.
content = open(file_path, "rb").read()
files = {"file": {"filename": "some_file.sd", "content": content}}
params = {"f": "json", "token": token, "type": item_type,
"title": title, "tags": tags, "description": description}
data, headers = multipart_request(params, files)
"""
# Get mix of letters and digits to form boundary.
letters_digits = "".join(string.digits + string.ascii_letters)
boundary = "----WebKitFormBoundary{}".format("".join(random.choice(letters_digits) for i in range(16)))
file_lines = []
# Parse the params and files dicts to build the multipart request.
for name, value in params.iteritems():
file_lines.extend(("--{}".format(boundary),
'Content-Disposition: form-data; name="{}"'.format(name),
"", str(value)))
for name, value in files.items():
if "filename" in value:
filename = value.get("filename")
else:
raise Exception("The filename key is required.")
if "mimetype" in value:
mimetype = value.get("mimetype")
else:
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
if "content" in value:
file_lines.extend(("--{}".format(boundary),
'Content-Disposition: form-data; name="{}"; filename="{}"'.format(name, filename),
"Content-Type: {}".format(mimetype), "",
(value.get("content"))))
else:
raise Exception("The content key is required.")
# Create the end of the form boundary.
file_lines.extend(("--{}--".format(boundary), ""))
request_data = "\r\n".join(file_lines)
request_headers = {"Content-Type": "multipart/form-data; boundary={}".format(boundary),
"Content-Length": str(len(request_data))}
return request_data, request_headers
if __name__ == "__main__":
print("Starting Feature Service publish process from zip (shp files)")
# AGOL Credentials - enter your username and password
inputUsername = 'Enter user name'
inputPswd = 'Enter P@$$W0RD'
# FS values - enter location of .zip file, service name on AGO, AGO folder name, tags, & summary
ZIPFILE = "D:\\Enter_Your\\Directory\\Here\\Your_Zip_File.zip"
serviceName = "Your_Layer_Service_Name"
folderName = "Enter a folder name here on AGO if your data is not in root"
tags = "overwrite, hosted, shapefile"
summary = "This is a hosted shapefile summary"
maxRecords = 1000
# Share FS to: everyone, org, groups - enter sharing details, organization, groups, etc.
shared = True
everyone = 'false'
orgs = 'true'
groups = None # Groups are by ID. Multiple groups comma separated
# initialize AGOLHandler class
agol = AGOLHandler(inputUsername, inputPswd, serviceName, folderName)
# overwrite the existing .SD on arcgis.com
if agol.upload(ZIPFILE, tags, summary):
# publish the sd which was just uploaded
fsID = agol.publish(summary, maxRecords)
# share the item
if shared:
agol.enableSharing(fsID, everyone, orgs, groups)
print("\nfinished.")