-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_dem_for_scene.py
62 lines (52 loc) · 2.02 KB
/
get_dem_for_scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from dem_stitcher import stitch_dem
from pathlib import Path
import rasterio as rio
from shapely.geometry import Polygon
import tomli
from pyroSAR import identify
from utils import transform_scene_extent
REPO_DIR = Path(__file__).resolve().parent
with open(REPO_DIR.joinpath("config.toml"), "rb") as f:
config_dict = tomli.load(f)
# Set up paths
data_dir = Path(config_dict["paths"]["data"])
dem_dir = data_dir.joinpath("dem")
scene_path = Path(config_dict["scene"])
# If scene exists, extract metadata
if scene_path.exists():
scene_id_pyrosar = identify(scene_path)
scene_name = scene_path.stem
# Extract scene bounds
scene_polygon = Polygon(scene_id_pyrosar.meta["coordinates"])
scene_bounds = scene_polygon.bounds
# if we are at high latitudes we need to correct the bounds due to the skewed box shape
if (scene_bounds[1] < -50) or (scene_bounds[3] < -50):
# Southern Hemisphere
print(f'Adjusting scene bounds due to warping at high latitude (Southern Hemisphere)')
scene_polygon = transform_scene_extent(scene_polygon, 4326, 3031)
scene_bounds = scene_polygon.bounds
#logging.info(f'Adjusted scene bounds : {scene_bounds}')
if (scene_bounds[1] > 50) or (scene_bounds[3] > 50):
# Northern Hemisphere
print(f'Adjusting scene bounds due to warping at high latitude (Northern Hemisphere)')
scene_polygon = transform_scene_extent(scene_bounds, 4326, 3995)
scene_bounds = scene_polygon.bounds
#logging.info(f'Adjusted scene bounds : {scene_bounds}')
# Buffer scene boundaries
buffer = 0.1
scene_bounds_buffered = scene_polygon.buffer(buffer).bounds #buffered
dem_filename = scene_name + '_dem.tif'
dem_file = dem_dir.joinpath(dem_filename)
dem_data, dem_meta = stitch_dem(
scene_bounds_buffered,
dem_name='glo_30',
dst_ellipsoidal_height=True,
dst_area_or_point='Point',
merge_nodata_value=0,
fill_to_bounds=True,
)
print(f'saving dem to {dem_file}')
with rio.open(dem_file, 'w', **dem_meta) as ds:
ds.write(dem_data, 1)
ds.update_tags(AREA_OR_POINT='Point')
del dem_data