<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Download Queried Raster from ArcGIS REST in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1295403#M67782</link>
    <description>&lt;P&gt;You're close and a couple things for it-&amp;nbsp; bbox should just be a string, and the return format (f) should be image, and lastly, you'll need to tell it to save it (response.content) to disk.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

url = r"https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?"

params = {"f": "image",
          "bbox": '-2.00375070672E7,-1689391.823360186,2.0037507532527123E7,1.88090019719E7',
          "imageSR": 102005,
          "format": "tiff",
          "pixelType": "S16",
          "noDataInterpretation": "esriNoDataMatchAny",
          "interpolation": "RSP_BilinearInterpolation"}

response = requests.get(url=url, params=params, allow_redirects=True)

with open(r'C:\...\Documents\ArcGIS\outtest_s16.tif', mode='wb') as localfile:
    localfile.write(response.content)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;gives me this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JeffK_0-1685719896894.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/72277i10792A03BB98397E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JeffK_0-1685719896894.png" alt="JeffK_0-1685719896894.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Jun 2023 15:33:30 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2023-06-02T15:33:30Z</dc:date>
    <item>
      <title>Download Queried Raster from ArcGIS REST</title>
      <link>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1293773#M67732</link>
      <description>&lt;P&gt;I am trying to download/export a raster based on a query of USGS's 3DEP Image Service. The endpoint (if that's the correct terminology) is here:&lt;/P&gt;&lt;P&gt;&lt;A title="USGS 3DEP Image Service" href="https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-2.00375070672E7,-1689391.823360186,2.0037507532527123E7,1.88090019719E7" target="_self"&gt;https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-2.00375070672E7,-1689391.823360186,2.0037507532527123E7,1.88090019719E7&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what this looks like using the HTML interface. Clicking "Export Image (GET)" initiates a download of the raster as a .tif file, exactly as I need.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VinceE_0-1685224621192.png" style="width: 448px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/71859i36058F7386D12E93/image-dimensions/448x317?v=v2" width="448" height="317" role="button" title="VinceE_0-1685224621192.png" alt="VinceE_0-1685224621192.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I would like to be able to do this using Python/ArcPy exclusively (updating the parameters as needed), as part of a larger script. I have had a shockingly difficult time Googling this or finding any resources about how to do this, so I'm missing some very basic concepts here.&lt;/P&gt;&lt;P&gt;The code below returns... something, but I don't know how to interact with whatever it is. I just need the actual raster data (a tif? a numpy array? anything?) to do some other geoprocessing with.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

url = \
    r"https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?"

params = {"f": "html",
          "bbox": [-2.00375070672E7, -1689391.823360186, 2.0037507532527123E7, 1.88090019719E7],
          "imageSR": 102005,
          "format": "tiff",
          "pixelType": "S16",
          "noDataInterpretation": "esriNoDataMatchAny",
          "interpolation": "RSP_BilinearInterpolation"}

response = requests.get(url=url, params=params, allow_redirects=True)
print(response.text)

# Something like this...
# queried_DigitalElevationModel = response.getData()?
# arcpy.Raster(queried_DigitalElevationModel).save("localfilepath or memory")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 May 2023 22:09:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1293773#M67732</guid>
      <dc:creator>VinceE</dc:creator>
      <dc:date>2023-05-27T22:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Download Queried Raster from ArcGIS REST</title>
      <link>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1295403#M67782</link>
      <description>&lt;P&gt;You're close and a couple things for it-&amp;nbsp; bbox should just be a string, and the return format (f) should be image, and lastly, you'll need to tell it to save it (response.content) to disk.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

url = r"https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?"

params = {"f": "image",
          "bbox": '-2.00375070672E7,-1689391.823360186,2.0037507532527123E7,1.88090019719E7',
          "imageSR": 102005,
          "format": "tiff",
          "pixelType": "S16",
          "noDataInterpretation": "esriNoDataMatchAny",
          "interpolation": "RSP_BilinearInterpolation"}

response = requests.get(url=url, params=params, allow_redirects=True)

with open(r'C:\...\Documents\ArcGIS\outtest_s16.tif', mode='wb') as localfile:
    localfile.write(response.content)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;gives me this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JeffK_0-1685719896894.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/72277i10792A03BB98397E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JeffK_0-1685719896894.png" alt="JeffK_0-1685719896894.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:33:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1295403#M67782</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-06-02T15:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: Download Queried Raster from ArcGIS REST</title>
      <link>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1311298#M68243</link>
      <description>&lt;P&gt;Thanks for the assistance&amp;nbsp;@Anonymous User, I was able to get this working. I have a follow up question that I'm hoping you may have some insight into.&lt;/P&gt;&lt;P&gt;My modified code is below. The extent string is computed beforehand, but seems to work as expected, based on the polyline geometry being read via a SearchCursor.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def save_raster(name, output_directory, ext_string):
    """"""
    url = r"https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?"

    params = {"f": "image",
              "bbox": ext_string,
              "bboxSR": 102005,
              "imageSR": 102005,
              "format": "tiff",
              "pixelType": "F32",
              "size": "600,600",
              "noDataInterpretation": "esriNoDataMatchAny",
              "interpolation": "RSP_BilinearInterpolation"}

    response = requests.get(url=url, params=params, allow_redirects=True)

    name = "".join([char if char in ascii_letters else "" for char in name])
    fullpath = pathlib.Path(output_directory, f"{name}.tif")
    with open(fullpath, mode="wb") as image_file:
        image_file.write(response.content)

    arcpy.management.CalculateStatistics(str(fullpath))&lt;/LI-CODE&gt;&lt;P&gt;The question I have is about specifying pixel size, which oddly enough does not seem to be an option based on the documentation here:&lt;BR /&gt;&lt;A href="https://developers.arcgis.com/rest/services-reference/enterprise/export-image.htm" target="_blank"&gt;https://developers.arcgis.com/rest/services-reference/enterprise/export-image.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Below my pixels are roughly 10x10 (meters) because I've specified a size of 600,600.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VinceE_0-1690239813605.png" style="width: 795px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76180iABB185DDC74372EA/image-dimensions/795x316?v=v2" width="795" height="316" role="button" title="VinceE_0-1690239813605.png" alt="VinceE_0-1690239813605.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here my pixels are much, much larger because the size I've specified is 30x30, width/height.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VinceE_1-1690240140184.png" style="width: 780px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76181iB6DEADF8C5DC62F6/image-dimensions/780x314?v=v2" width="780" height="314" role="button" title="VinceE_1-1690240140184.png" alt="VinceE_1-1690240140184.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Using the dimensions of the polyline extent (extent.XMax - extent.XMin) and the intended pixel resulution (30 meters, for example), some math can be performed to get the "&lt;SPAN&gt;size=&amp;lt;width&amp;gt;,&amp;lt;height&amp;gt;" that yields approximately the intended pixel resolution, but it's pretty cumbersome compared to just passing the resolution as a parameter.&lt;BR /&gt;&lt;BR /&gt;It's very odd to me that the call takes a pixel count (when is this property relevant?) instead of the actual pixel resolution, which is one of the more important properties of a raster dataset...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 23:45:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/download-queried-raster-from-arcgis-rest/m-p/1311298#M68243</guid>
      <dc:creator>VinceE</dc:creator>
      <dc:date>2023-07-24T23:45:21Z</dc:date>
    </item>
  </channel>
</rss>

