|
10 | 10 | import pdal |
11 | 11 | import pooch |
12 | 12 | import rasterio as rio |
13 | | -from osgeo import gdal |
14 | 13 | from pyregeon import CRSType, RegionType |
15 | 14 | from pystac_client.item_search import DatetimeLike |
| 15 | +from rasterio import merge |
16 | 16 | from tqdm import tqdm |
17 | 17 |
|
18 | | -from swisstopopy import stac, utils |
| 18 | +from swisstopopy import settings, stac, utils |
19 | 19 |
|
20 | 20 | __all__ = ["get_tree_canopy_raster"] |
21 | 21 |
|
22 | | -DST_OPTIONS = ["TILED:YES"] |
23 | | - |
24 | 22 |
|
25 | 23 | def rasterize_lidar( |
26 | 24 | lidar_filepath: utils.PathType, |
@@ -61,10 +59,10 @@ def get_tree_canopy_raster( |
61 | 59 | dst_tree_val: int = 1, |
62 | 60 | dst_nodata: int = 0, |
63 | 61 | dst_dtype: npt.DTypeLike = "uint32", |
64 | | - lidar_tree_values: Sequence[int] | None = None, |
| 62 | + lidar_tree_values: int | Sequence[int] | None = 3, |
65 | 63 | rasterize_lidar_kwargs: utils.KwargsType = None, |
66 | 64 | pooch_retrieve_kwargs: utils.KwargsType = None, |
67 | | - gdal_warp_kwargs: utils.KwargsType = None, |
| 65 | + rio_merge_kwargs: utils.KwargsType = None, |
68 | 66 | ) -> None: |
69 | 67 | """Get tree canopy raster. |
70 | 68 |
|
@@ -96,9 +94,11 @@ def get_tree_canopy_raster( |
96 | 94 | lidar_tree_values : int or sequence of int, default 3. |
97 | 95 | LiDAR classification values to use for tree canopy. If None, defaults to |
98 | 96 | the "Vegetation" class value of swissSURFACE3D, i.e., 3. |
99 | | - rasterize_lidar_kwargs, pooch_retrieve_kwargs, gdal_warp_kwargs : mapping, optional |
| 97 | + rasterize_lidar_kwargs, pooch_retrieve_kwargs, rio_merge_kwargs : mapping, optional |
100 | 98 | Additional keyword arguments to respectively pass to |
101 | | - `swisstopopy.tree_canopy.rasterize_lidar`, `pooch.retrieve` and `gdal.Warp`. |
| 99 | + `swisstopopy.tree_canopy.rasterize_lidar`, `pooch.retrieve` and |
| 100 | + `rasterio.merge.merge`. If the latter is None, the default values from |
| 101 | + `settings.RIO_MERGE_DST_KWARGS` are used. |
102 | 102 | """ |
103 | 103 | # use the STAC API to get the tree canopy from swissSURFACE3D |
104 | 104 | # TODO: dry with `dem.get_dem_raster`? |
@@ -126,18 +126,19 @@ def get_tree_canopy_raster( |
126 | 126 | output_type="count", |
127 | 127 | data_type="uint32", |
128 | 128 | nodata=dst_nodata, |
129 | | - default_srs=stac.SWISSALTI3D_CRS, |
| 129 | + default_srs=stac.CH_CRS, |
130 | 130 | ) |
131 | 131 | if pooch_retrieve_kwargs is None: |
132 | 132 | pooch_retrieve_kwargs = {} |
133 | 133 |
|
134 | 134 | if isinstance(lidar_tree_values, int): |
135 | 135 | lidar_tree_values = [lidar_tree_values] |
136 | | - if gdal_warp_kwargs is None: |
137 | | - _gdal_warp_kwargs = {} |
| 136 | + |
| 137 | + if rio_merge_kwargs is None: |
| 138 | + _rio_merge_kwargs = {} |
138 | 139 | else: |
139 | | - _gdal_warp_kwargs = gdal_warp_kwargs.copy() |
140 | | - _gdal_warp_kwargs.update(creationOptions=DST_OPTIONS) |
| 140 | + _rio_merge_kwargs = rio_merge_kwargs.copy() |
| 141 | + _rio_merge_kwargs.update(dst_kwds=settings.RIO_MERGE_DST_KWARGS) |
141 | 142 |
|
142 | 143 | img_filepaths = [] |
143 | 144 | with tempfile.TemporaryDirectory() as tmp_dir: |
@@ -165,6 +166,13 @@ def get_tree_canopy_raster( |
165 | 166 | tmp_dir, |
166 | 167 | f"{path.splitext(path.basename(img_filepath))[0]}-counts.tif", |
167 | 168 | ) |
| 169 | + _ = rasterize_lidar( |
| 170 | + las_filepath, |
| 171 | + counts_filepath, |
| 172 | + lidar_tree_values, |
| 173 | + **_rasterize_lidar_kwargs, |
| 174 | + ) |
| 175 | + |
168 | 176 | try: |
169 | 177 | _ = rasterize_lidar( |
170 | 178 | las_filepath, |
@@ -193,5 +201,9 @@ def get_tree_canopy_raster( |
193 | 201 | # add path to list |
194 | 202 | img_filepaths.append(img_filepath) |
195 | 203 |
|
196 | | - # creationOptions=dst_options |
197 | | - _ = gdal.Warp(dst_filepath, img_filepaths, format="GTiff", **_gdal_warp_kwargs) |
| 204 | + # merge tiles into the final raster |
| 205 | + merge.merge( |
| 206 | + img_filepaths, |
| 207 | + dst_path=dst_filepath, |
| 208 | + **_rio_merge_kwargs, |
| 209 | + ) |
0 commit comments