Skip to main content
added 104 characters in body
Source Link
traggatmot
  • 2.1k
  • 1
  • 24
  • 45
import arcpy
import requests
import pandas as pd
import time
import json



def scrape_tilesgen_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=' + str(token['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)
 
            req_status_urljobStatus = 'http://tiledbasemaps.arcgis.com/arcgis/rest/services/World_Topo_Map/MapServer/jobs/' \
                             + strcheck_status(jobID)\
                             + '?f=json' \
                               '&token=' + str(token['token'])

            status_resp = requests.get(req_status_url)
            status_resp_cont =, json.loads(status_resp.contenttoken)
            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'])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)
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)
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)
Source Link
traggatmot
  • 2.1k
  • 1
  • 24
  • 45

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 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)

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