Created
October 20, 2014 20:38
-
-
Save amarinelli/b6a95ddd30264c936d68 to your computer and use it in GitHub Desktop.
Manage AGS Service scale levels and edit the service using REST API to include new max/min scales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
==Inputs== (starting at line 95) | |
server --> AGS machine name - (string) | |
port --> AGS port - (string) | |
adminUser --> administrator or publisher user - (string) | |
adminPass --> administrator or publisher password - (string) | |
service --> name of service starting with folder (if applicable) <name>.<type> notation - (string) | |
full_service_path --> path to the .ags connection file and the service folder and <name>.<type> notation - (string) | |
scales --> semicolon delimited list of scale values - (string) | |
--> If you remove scales from an existing cache, it will permanently delete all existing cached tiles within that level of detail. | |
''' | |
import urllib | |
import urllib2 | |
import json | |
import httplib | |
import arcpy | |
def gentoken(server, port, adminUser, adminPass, expiration=60): | |
#Re-usable function to get a token required for Admin changes | |
query_dict = {'username': adminUser, | |
'password': adminPass, | |
'expiration': str(expiration), | |
'client': 'requestip'} | |
query_string = urllib.urlencode(query_dict) | |
url = "http://{}:{}/arcgis/admin/generateToken".format(server, port) | |
token = json.loads(urllib.urlopen(url + "?f=json", query_string).read()) | |
if "token" not in token: | |
arcpy.AddError(token['messages']) | |
quit() | |
else: | |
return token['token'] | |
def format_service(response, maxScale, minScale): | |
# Function to edit the service json | |
# Result to be used when editing the service | |
# Edit service as JSON and return JSON string | |
json_repsonse = json.loads(response) | |
json_repsonse["properties"]["maxScale"] = maxScale | |
json_repsonse["properties"]["minScale"] = minScale | |
return json.dumps(json_repsonse) | |
def getservice(server, port, service, token): | |
''' Function to get the service json so that it can be edited. | |
service = A service must be in the <name>.<type> notation | |
''' | |
service = urllib.quote(service.encode('utf8')) | |
op_service_url = "http://{}:{}/arcgis/admin/services/{}?token={}&f=json".format(server, port, service, token) | |
result = urllib2.urlopen(op_service_url, ' ').read() | |
return result | |
def editservice(server, port, service, token, response, maxScale, minScale): | |
''' Function to edit a service json. | |
service = A service must be in the <name>.<type> notation | |
''' | |
service = urllib.quote(service.encode('utf8')) | |
# call edit service function to perform changes | |
edit_response = format_service(response, maxScale, minScale) | |
params = urllib.urlencode({'service': edit_response, "token": token, "f": "json"}) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} | |
conn = httplib.HTTPConnection("{}:{}".format(server, port)) | |
conn.request("POST", "/arcgis/admin/services/{}/edit".format(service), params, headers) | |
resp = conn.getresponse() | |
#print resp.read() | |
if 'success' in resp.read(): | |
print str(service) + " === edited successfully" | |
else: | |
print resp.read() | |
conn.close() | |
return | |
if __name__ == "__main__": | |
# Gather inputs | |
# Make changes here | |
server = "cutsp62" | |
port = "6080" | |
adminUser = "username" | |
adminPass = "password" | |
service = "Case156454/CacheMap_test.MapServer" | |
full_service_path = r'GIS Servers\arcgis on cutsp62_6080 (admin)\Case156454\CacheMap_test.MapServer' | |
scales = "288895;577790;1155581;2311162;4622324" | |
# if scales list is sorted LOWEST to HIGHEST | |
scale_list = scales.split(";") | |
maxScale = scale_list[0] | |
minScale = scale_list[len(scale_list)-1] | |
# if not sorted, comment above lines and assign them manually | |
# maxScale = '288895' | |
# minScale = '4622324' | |
# Edit Map Service Scales using GP tool | |
arcpy.ManageMapServerCacheScales_server(full_service_path, scales) | |
# Get AGS token | |
token = gentoken(server, port, adminUser, adminPass) | |
# Get full service JSON | |
response = getservice(server, port, service, token) | |
# Edit service JSON and send request to server | |
editservice(server, port, service, token, response, maxScale, minScale) | |
print "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment