|
| 1 | +"""Download USGS 3DEP 1-meter DEM tiles from S3 via the TNM Access API. |
| 2 | +
|
| 3 | +Uses the USGS TNM (The National Map) Access API to discover which 1-meter |
| 4 | +DEM tiles cover the requested bounding box, then fetches them as Cloud |
| 5 | +Optimized GeoTIFFs (COGs) from S3 using GDAL's /vsicurl/ virtual filesystem. |
| 6 | +
|
| 7 | +Source tiles are in NAD83/UTM (varying zones) with NAVD88 heights, but without |
| 8 | +compound vertical CRS metadata. Horizontal reprojection to EPSG:4326 is always |
| 9 | +performed. When keep_egm=False (the default), a second GDAL warp converts |
| 10 | +NAVD88 heights to WGS84 ellipsoidal heights. |
| 11 | +
|
| 12 | +Coverage: US only -- not all areas have 1m lidar data available. |
| 13 | +
|
| 14 | +References: |
| 15 | + https://www.usgs.gov/3d-elevation-program |
| 16 | + https://tnmaccess.nationalmap.gov/api/v1/products |
| 17 | +""" |
| 18 | + |
| 19 | +import logging |
| 20 | +import os |
| 21 | +import tempfile |
| 22 | + |
| 23 | +import requests |
| 24 | + |
| 25 | +from sardem import utils |
| 26 | + |
| 27 | +logger = logging.getLogger("sardem") |
| 28 | +utils.set_logger_handler(logger) |
| 29 | + |
| 30 | +TNM_API_URL = "https://tnmaccess.nationalmap.gov/api/v1/products" |
| 31 | +TNM_DATASET = "Digital Elevation Model (DEM) 1 meter" |
| 32 | +TNM_MAX_ITEMS = 200 |
| 33 | + |
| 34 | + |
| 35 | +def download_and_stitch( |
| 36 | + output_name, |
| 37 | + bbox, |
| 38 | + keep_egm=False, |
| 39 | + xrate=1, |
| 40 | + yrate=1, |
| 41 | + output_format="GTiff", |
| 42 | + output_type="float32", |
| 43 | +): |
| 44 | + """Download USGS 3DEP 1m DEM tiles and mosaic them with gdal.Warp. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + output_name : str |
| 49 | + Path for the output DEM file. |
| 50 | + bbox : tuple |
| 51 | + (left, bottom, right, top) in decimal degrees. |
| 52 | + keep_egm : bool |
| 53 | + If True, keep NAVD88 geoid heights. If False (default), convert to |
| 54 | + WGS84 ellipsoidal heights. |
| 55 | + xrate : int |
| 56 | + Upsample factor in x direction (ignored for 1m data). |
| 57 | + yrate : int |
| 58 | + Upsample factor in y direction (ignored for 1m data). |
| 59 | + output_format : str |
| 60 | + GDAL output format (default GTiff). |
| 61 | + output_type : str |
| 62 | + Output pixel type (default float32). |
| 63 | + """ |
| 64 | + from osgeo import gdal |
| 65 | + |
| 66 | + gdal.UseExceptions() |
| 67 | + |
| 68 | + if xrate > 1 or yrate > 1: |
| 69 | + logger.warning( |
| 70 | + "xrate/yrate upsampling is ignored for 3DEP_1M (data is already" |
| 71 | + " ~1m resolution). xrate=%d, yrate=%d.", |
| 72 | + xrate, |
| 73 | + yrate, |
| 74 | + ) |
| 75 | + |
| 76 | + tile_urls = _find_tile_urls(bbox) |
| 77 | + vsicurl_paths = ["/vsicurl/" + url for url in tile_urls] |
| 78 | + logger.info("Found %d tile(s) covering the bounding box", len(vsicurl_paths)) |
| 79 | + |
| 80 | + out_type = gdal.GetDataTypeByName(output_type.title()) |
| 81 | + |
| 82 | + # Source tiles are in NAD83/UTM (various zones). Do NOT override srcSRS -- |
| 83 | + # GDAL must read the CRS from each file's metadata for correct reprojection. |
| 84 | + reproject_opts = dict( |
| 85 | + format=output_format, |
| 86 | + outputBounds=list(bbox), |
| 87 | + dstSRS="EPSG:4326", |
| 88 | + outputType=out_type, |
| 89 | + resampleAlg="nearest", |
| 90 | + srcNodata=-999999, |
| 91 | + multithread=True, |
| 92 | + warpMemoryLimit=5000, |
| 93 | + warpOptions=["NUM_THREADS=4"], |
| 94 | + ) |
| 95 | + |
| 96 | + if keep_egm: |
| 97 | + logger.info("Creating %s (keeping NAVD88 heights)", output_name) |
| 98 | + reproject_opts["callback"] = gdal.TermProgress |
| 99 | + gdal.Warp( |
| 100 | + output_name, vsicurl_paths, options=gdal.WarpOptions(**reproject_opts) |
| 101 | + ) |
| 102 | + else: |
| 103 | + # Two-step warp: |
| 104 | + # 1) Reproject from UTM to EPSG:4326 (horizontal only, preserves NAVD88 Z) |
| 105 | + # 2) Convert NAVD88 heights to WGS84 ellipsoidal using compound CRS. |
| 106 | + # After step 1 the file is in geographic coords, so |
| 107 | + # srcSRS="EPSG:4269+5703" (NAD83 + NAVD88) is correct. |
| 108 | + fd, tmp_path = tempfile.mkstemp(suffix=".tif") |
| 109 | + os.close(fd) |
| 110 | + try: |
| 111 | + logger.info("Reprojecting tiles to EPSG:4326...") |
| 112 | + reproject_opts["callback"] = gdal.TermProgress |
| 113 | + gdal.Warp( |
| 114 | + tmp_path, vsicurl_paths, options=gdal.WarpOptions(**reproject_opts) |
| 115 | + ) |
| 116 | + |
| 117 | + logger.info("Converting NAVD88 heights to WGS84 ellipsoidal...") |
| 118 | + vert_opts = dict( |
| 119 | + format=output_format, |
| 120 | + srcSRS="EPSG:4269+5703", |
| 121 | + dstSRS="EPSG:4326", |
| 122 | + outputType=out_type, |
| 123 | + multithread=True, |
| 124 | + callback=gdal.TermProgress, |
| 125 | + ) |
| 126 | + gdal.Warp( |
| 127 | + output_name, tmp_path, options=gdal.WarpOptions(**vert_opts) |
| 128 | + ) |
| 129 | + finally: |
| 130 | + if os.path.exists(tmp_path): |
| 131 | + os.remove(tmp_path) |
| 132 | + |
| 133 | + |
| 134 | +def _find_tile_urls(bbox): |
| 135 | + """Query TNM Access API for 1m DEM tiles covering bbox. |
| 136 | +
|
| 137 | + Returns list of S3 download URLs, sorted oldest-first so |
| 138 | + newest tiles take priority in gdal.Warp overlap resolution. |
| 139 | +
|
| 140 | + Parameters |
| 141 | + ---------- |
| 142 | + bbox : tuple |
| 143 | + (left, bottom, right, top) in decimal degrees. |
| 144 | +
|
| 145 | + Returns |
| 146 | + ------- |
| 147 | + list[str] |
| 148 | + S3 URLs to COG tiles. |
| 149 | +
|
| 150 | + Raises |
| 151 | + ------ |
| 152 | + RuntimeError |
| 153 | + If no tiles are found for the requested area. |
| 154 | + """ |
| 155 | + left, bottom, right, top = bbox |
| 156 | + params = { |
| 157 | + "datasets": TNM_DATASET, |
| 158 | + "bbox": "{},{},{},{}".format(left, bottom, right, top), |
| 159 | + "max": TNM_MAX_ITEMS, |
| 160 | + "outputFormat": "JSON", |
| 161 | + } |
| 162 | + |
| 163 | + logger.info("Querying TNM API for 1m DEM tiles...") |
| 164 | + response = requests.get(TNM_API_URL, params=params, timeout=60) |
| 165 | + response.raise_for_status() |
| 166 | + |
| 167 | + data = response.json() |
| 168 | + items = data.get("items", []) |
| 169 | + if not items: |
| 170 | + raise RuntimeError( |
| 171 | + "No 3DEP 1m DEM tiles found for bbox {}. " |
| 172 | + "Not all US areas have 1m coverage.".format(bbox) |
| 173 | + ) |
| 174 | + |
| 175 | + if len(items) >= TNM_MAX_ITEMS: |
| 176 | + logger.warning( |
| 177 | + "TNM API returned the maximum %d items. The bounding box may be" |
| 178 | + " too large to fetch all tiles in one request.", |
| 179 | + TNM_MAX_ITEMS, |
| 180 | + ) |
| 181 | + |
| 182 | + # Sort by dateCreated ascending (oldest first) so that when gdal.Warp |
| 183 | + # processes them in order, newer tiles overwrite older ones in overlaps |
| 184 | + items.sort(key=lambda item: item.get("dateCreated", "")) |
| 185 | + |
| 186 | + urls = [item["downloadURL"] for item in items] |
| 187 | + logger.info("Found %d tiles from TNM API", len(urls)) |
| 188 | + return urls |
0 commit comments