|
1 | | -import os |
2 | 1 | from collections.abc import Callable |
3 | 2 | from pathlib import Path |
| 3 | +from tempfile import NamedTemporaryFile |
4 | 4 |
|
5 | | -import backoff |
6 | 5 | import numpy as np |
7 | 6 | import shapely |
8 | 7 | import shapely.ops |
9 | 8 | import shapely.wkt |
| 9 | +from hyp3lib.util import GDALConfigManager |
10 | 10 | from osgeo import gdal, osr |
11 | 11 | from shapely.geometry import LinearRing, Polygon, box |
12 | 12 |
|
@@ -49,7 +49,7 @@ def polygon_from_bounds(bounds: tuple[float, float, float, float]) -> Polygon: |
49 | 49 | return poly |
50 | 50 |
|
51 | 51 |
|
52 | | -def check_antimeridean(poly: Polygon) -> list[Polygon]: |
| 52 | +def split_antimeridean(poly: Polygon) -> list[Polygon]: |
53 | 53 | """Check if the provided polygon crosses the antimeridian and split it if it does.""" |
54 | 54 | x_min, _, x_max, _ = poly.bounds |
55 | 55 |
|
@@ -92,14 +92,13 @@ def snap_coord(val: float, snap: float, offset: float, round_func: Callable) -> |
92 | 92 | return round_func(float(val - offset) / snap) * snap + offset |
93 | 93 |
|
94 | 94 |
|
95 | | -@backoff.on_exception(backoff.expo, Exception, max_time=600, max_value=32) |
96 | 95 | def translate_dem(vrt_filename: str, output_path: str, bounds: tuple[float, float, float, float]) -> None: |
97 | | - """Translate the OPERA DEM from S3 to a region matching the provided boundaries. |
| 96 | + """Write a local subset of the OPERA DEM for a region matching the provided bounds. |
98 | 97 |
|
99 | 98 | Params: |
100 | 99 | vrt_filename: Path to the input VRT file |
101 | 100 | output_path: Path to the translated output GTiff file |
102 | | - bounds: tuple of (x_min, x_max, y_min, y_max) |
| 101 | + bounds: Bounding box in the form of (lon_min, lat_min, lon_max, lat_max) |
103 | 102 | """ |
104 | 103 | ds = gdal.Open(vrt_filename, gdal.GA_ReadOnly) |
105 | 104 |
|
@@ -159,21 +158,23 @@ def download_opera_dem_for_footprint(outfile: Path, bounds: tuple[float, float, |
159 | 158 | """Download a DEM from the specified S3 bucket. |
160 | 159 |
|
161 | 160 | Params: |
162 | | - polys: List of shapely polygons. |
163 | 161 | outfile: Path to the where the output DEM file is to be staged. |
| 162 | + bounds: Bounding box in the form of (lon_min, lat_min, lon_max, lat_max). |
164 | 163 | """ |
165 | 164 | poly = polygon_from_bounds(bounds) |
166 | | - polys = check_antimeridean(poly) |
| 165 | + polys = split_antimeridean(poly) |
167 | 166 | dem_list = [] |
168 | 167 |
|
169 | | - os.environ['GDAL_HTTP_COOKIEJAR'] = 'cookies.txt' |
170 | | - os.environ['GDAL_HTTP_COOKIEFILE'] = 'cookies.txt' |
171 | | - os.environ['GDAL_DISABLE_READDIR_ON_OPEN'] = 'EMPTY_DIR' |
172 | | - |
173 | | - vrt_filename = '/vsicurl/https://nisar.asf.earthdatacloud.nasa.gov/STATIC/DEM/v1.2/EPSG4326/EPSG4326.vrt' |
174 | | - for idx, poly in enumerate(polys): |
175 | | - output_path = f'{outfile.stem}_{idx}.tif' |
176 | | - dem_list.append(output_path) |
177 | | - translate_dem(vrt_filename, output_path, bounds) |
178 | | - |
179 | | - gdal.BuildVRT(str(outfile), dem_list) |
| 168 | + with NamedTemporaryFile(suffix='.txt') as cookie_file: |
| 169 | + with GDALConfigManager( |
| 170 | + GDAL_HTTP_COOKIEJAR=cookie_file.name, |
| 171 | + GDAL_HTTP_COOKIEFILE=cookie_file.name, |
| 172 | + GDAL_DISABLE_READDIR_ON_OPEN='EMPTY_DIR', |
| 173 | + ): |
| 174 | + vrt_filename = '/vsicurl/https://nisar.asf.earthdatacloud.nasa.gov/STATIC/DEM/v1.2/EPSG4326/EPSG4326.vrt' |
| 175 | + for idx, poly in enumerate(polys): |
| 176 | + output_path = f'{outfile.stem}_{idx}.tif' |
| 177 | + dem_list.append(output_path) |
| 178 | + translate_dem(vrt_filename, output_path, bounds) |
| 179 | + |
| 180 | + gdal.BuildVRT(str(outfile), dem_list) |
0 commit comments