Thursday 15 February 2018

arcpy - Problems using REST API to generate Tile Packages from World Topo (For Export)


Using the python script below it seems that I can request the generation of tile packages from an ESRI ArcMap REST service, and then download the resulting tpk file generated by ESRI. But when I try and open the package NOTHING shows up in ArcMap. Can't tell what is happening - can't tell what is going wrong. Any ideas?


import arcpy

import requests
import pandas as pd
import time
import json



def gen_token():
arcpy.SignInToPortal_server("USERNAME", "PASSWORD", "")
token = arcpy.GetSigninToken()

return str(token['token'])


def check_status(jobID, token):
req_status_url = 'http://tiledbasemaps.arcgis.com/arcgis/rest/services/World_Topo_Map/MapServer/jobs/' \
+ jobID \
+ '?f=json' \
'&token=' + token

status_resp = requests.get(req_status_url)

status_resp_cont = json.loads(status_resp.content)
return str(status_resp_cont['jobStatus'])


def scrape_tiles(input_file, levels):

token = gen_token()

tpk_grid_df = pd.read_csv(input_file, header=0)


for index,row in tpk_grid_df.iterrows():

extent = '{"xmin":' + str(row['XMIN']) + \
',"ymin":'+str(row['YMIN']) + \
',"xmax":'+str(row['XMAX']) + \
',"ymax":'+str(row['YMAX']) + \
',"spatialReference" : {"wkid" : 4326}}'

req_url = 'http://tiledbasemaps.arcgis.com/arcgis/rest/services/World_Topo_Map/MapServer/exportTiles?' \
'tilePackage=true' \

'&exportExtent=' + extent + \
'&optimizeTilesForSize=true' \
'&compressionQuality=90' \
'&exportBy=levelId' \
'&levels=' + str(levels) + \
'&f=json' \
'&token=' + token

resp = requests.get(req_url)
resp_cont_dic = json.loads(resp.content)

jobID = str(resp_cont_dic['jobId'])
jobStatus = str(resp_cont_dic['jobStatus'])
print(jobStatus)

while jobStatus == 'esriJobSubmitted' or jobStatus == 'esriJobExecuting':
time.sleep(15)
jobStatus = check_status(jobID, token)

if jobStatus == 'esriJobFailed':
print('esriJobFailed')

break

# GRABBING THE RESULTS
res_url = 'http://tiledbasemaps.arcgis.com/arcgis/rest/services/World_Topo_Map/MapServer/jobs/' \
+ str(jobID) \
+ '/results/out_service_url?f=json' \
'&token=' + token

result_resp = requests.get(res_url)
result_resp_cont_dic = json.loads(result_resp.content)

result_url = result_resp_cont_dic['value'] + '/Layers.tpk'

file_name = 'NH_TP_' + str(levels) + '_' + str(row['ID']) + '.tpk'

r = requests.get(result_url)
with open(file_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush() #commented by recommendation from J.F.Sebastian

f.close()


if __name__ == "__main__":

input_file = "TPK_Grid_100.csv"
num_levels = 17
scrape_tiles(input_file, num_levels)

First couple of lines from the input file:



ID  XMIN    YMIN    XMAX    YMAX
1 -72.573919 42.633528 -72.37901 42.909766
2 -72.37901 42.633528 -72.1841 42.909766


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...