<jupyter_start><jupyter_text>Elevation rasters from the National National Map (USGS)The [3D Elevation Program (3DEP)](https://www.usgs.gov/core-science-systems/ngp/3dep/about-3dep-products-services) is the source of the elevation products used by the National Map, which is maintained by the USGS. This is a nation-wide elevation data set provided as a web service through the [ArcGIS REST API Image Server](https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer) with a 1m spatial resolution.This API allows the user to perform many differnt server-side computations for topographic analysis such as aspect, slope, histograms of the entire image, etc. I chose to use the exportImage service to access and download a DEM (.tiff) of the Lower Brazos River in Texas.<jupyter_code>import xarray as xr
import hvplot.xarray
import datashader
import cartopy.crs as ccrs
import geoviews as gv
import matplotlib.pyplot as plt
import cmocean.cm as cmo<jupyter_output><empty_output><jupyter_text>Using the REST APIFor this API, I created a query URL specifying the area of interest (AOI), coordinate reference system (CRS) for the AOI, size of the image in pixels, and CRS for the image. You can specify many more parameters, but I chose to leave them blank. I started with a coarse spatial resolution (100m) to troubleshoot my code, but when I used a finer resolution, the API failed without any feedback. This is strange because the API specifies a max spatial resolution of 1m and a max imagesize of 8000x8000. The API fails at a spatial resolution of 60m with an image size of 2736x4447 px, so I went with the largest file I could download at 70m resolution, 2345x3812 px, 35.76MB.   REST API Parameters<jupyter_code>bbox = [789113, 4127884, 953283, 4394745] # [xmin, ymin, xmax, ymax]
crs = 32140
res = 75     # width of a pixel in meters
funcs = ["",                                # raw elevation values
         "{'rasterFunction':'Grayscale'}",  # relative elevation values
         "{'rasterFunction':'Aspect'}",     # Direction of faces
         "{'rasterFunction':'Slope'}"]      # Slope<jupyter_output><empty_output><jupyter_text>Calcualte image size and build url<jupyter_code>def build_url(res, bbox, crs, func):
    
    x_px = int((bbox[2] - bbox[0]) / res)   # calc image size based on desired resolution
    y_px = int((bbox[3] - bbox[1]) / res)
    
    server = 'https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/'
    query = 'exportImage?bbox={xmin}%2C{ymin}%2C{xmax}%2C{ymax}&bboxSR={crs}&size={x_px}%2C{y_px}&imageSR={crs}&time=&format=tiff&pixelType=F32&noData=&noDataInterpretation=esriNoDataMatchAny&interpolation=+RSP_BilinearInterpolation&compression=&compressionQuality=&bandIds=&mosaicRule=&renderingRule={func}&f=image' \
    .format(xmin=bbox[0], ymin=bbox[1], xmax=bbox[2], ymax=bbox[3], crs=crs, x_px=x_px, y_px=y_px, func=func)
    
    url = server + query

    return(url, (x_px, y_px))
tiff_url = build_url(res, bbox, crs, funcs[0])
print(tiff_url[0])  # url for image - click this link to download the image
print(tiff_url[1])  # dimensions for image
from dask.distributed import Client

client = Client()
client
%%time
dem = xr.open_rasterio(tiff_url[0])
# # this takes my computer a while to load 
# %%time
# dem.hvplot.image(width = 600,
#                  height = 600,
#                  crs = ccrs.epsg(crs),
#                  rasterize=True)
dem_values = dem.isel(band=0).values
plt.imshow(dem_values)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10036858519988659, 'fraction_numerical': 0.02863623476041962, 'mean_word_length': 4.436055469953775, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '664343', 'license': 'MIT', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1138, 'n_words': 370, 'path': 'HW1/Brazos_Elevation/TNM_Elevation.ipynb', 'repo_name': 'adv-py-geos/adv-py-geos', 'size': '3527'}