imageserver exportImage versus MakeImageServerLayer python

544
0
05-29-2020 11:34 AM
JoeBriggs1
New Contributor II

I'm using python and trying to download and create a layer using the exportImage functionality here:3DEPElevation (ImageServer) .  I find that I'm able to download a clipped raster from exportImage much faster than using MakeImageServerLayer_management. ExportImage takes less than 15 seconds, where MakeImageServerLayer takes 45+ seconds.  So long story short I'd like to use exportImage if possible. 

However, sometimes it works great, but sometimes intermittently, I'm receiving a 400 status code when I try to download the image using exportImage.  The request to exportImage returns an href link in the json response, but when I try to download and write it to file, the request fails with a 400. When I use retries, the file will sometimes download on subsequent attempts, but sometimes it will never download.  Even when I copy the link into a browser. 

Can anyone offer an explanation, insight, and/or solution?

My requests config is something like this:

import requests, arcpy, datetime
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

retry_strategy = Retry(
    total=4,
    status_forcelist=[400, 429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS"],
    backoff_factor=1
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

And my imageServer code is something like this:

exportURL = r"https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?"

clipPoly=r'C:\test.gdb\test_poly'

tempDir=r'C:\temp'

featExtentJSON=arcpy.Describe(clipPoly).extent.JSON

exportParams = {'bbox':featExtentJSON,
                'bboxSR':'',
                'size':'',
                'imageSR':'',
                'time':'',
                'format':'tiff',
                'pixelType':'F32',
                'noData':'',
                'noDataInterpretation':'esriNoDataMatchAny',
                'interpolation':'RSP_BilinearInterpolation',
                'compression':'',
                'compressionQuality':'',
                'bandIds':'',
                'mosaicRule':{"mosaicMethod" : "esriMosaicLockRaster","lockRasterIds" : 4968},
                'renderingRule':'',
                'f':'pjson'
                }

def createExport(url, exParams):
    exportResponse = http.get(url, params=exParams)
    if exportResponse.status_code==200:
        fileURL=exportResponse.json()["href"]
    return fileURL

def downloadImage():
    fileURL = createExport(exportURL, exportParams)
    if fileURL.find('/'):
        fileName=fileURL.rsplit('/', 1)[1]       
        r = http.get(fileURL, allow_redirects=False)
        if r.status_code==200:
            with open(tempDir+'\\'+fileName,'wb') as f:
                f.write(r.content)
    return fileName

downloadImage()

0 Kudos
0 Replies