Using the script below it seems that I can request the generation of tile packages 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 scrape_tiles(token, input_file, levels):
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=' + str(token['token'])
resp = requests.get(req_url)
resp_cont_dic = json.loads(resp.content)
jobID = resp_cont_dic['jobId']
jobStatus = resp_cont_dic['jobStatus']
print(jobStatus)
while jobStatus == 'esriJobSubmitted' or jobStatus == 'esriJobExecuting':
time.sleep(15)
req_status_url = 'http://tiledbasemaps.arcgis.com/arcgis/rest/services/World_Topo_Map/MapServer/jobs/' \
+ str(jobID)\
+ '?f=json' \
'&token=' + str(token['token'])
status_resp = requests.get(req_status_url)
status_resp_cont = json.loads(status_resp.content)
jobStatus = status_resp_cont['jobStatus']
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=' + str(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__":
arcpy.SignInToPortal_server("USERNAME", "PASSWORD", "")
token = arcpy.GetSigninToken()
input_file = "TPK_Grid_100.csv"
num_levels = 17
#print(token)
scrape_tiles(token, input_file, num_levels)