Skip to content

Commit fa2ae09

Browse files
committed
Debug save_vrt
Crop to original size when saving to VRT
1 parent df9e5e5 commit fa2ae09

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

examples/tiled_processing/tiled_multiprocess.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
import numpy as np
1515
import rasterio
16+
from geopandas import GeoDataFrame
1617
from osgeo import gdal
1718
from rasterio.merge import merge
1819
from rasterio.windows import from_bounds
20+
from shapely.geometry import box
1921

2022
import grid_tools as gt
2123
import rvt.blend
@@ -44,6 +46,7 @@ def run_main(list_tifs, vis_types, blend_types, save_float=False, save_vrt=False
4446
tile_size = 2000
4547
else:
4648
tile_size = None
49+
raster_bounds = src.bounds
4750

4851
if tile_size:
4952
# # (3) To filter we need polygon covering valid data
@@ -62,6 +65,10 @@ def run_main(list_tifs, vis_types, blend_types, save_float=False, save_vrt=False
6265
# save_gpkg=False
6366
# )
6467

68+
# Crop outer tiles to the extents of raster (IMPORTANT so output size is same as input when tiling!)
69+
raster_bbox = box(*raster_bounds)
70+
tiles_extents = GeoDataFrame(geometry=tiles_extents.geometry.intersection(raster_bbox), crs=tiles_extents.crs)
71+
6572
# Add extents column: Extents are (L, B, R, T).
6673
tiles_extents["extents"] = tiles_extents.bounds.apply(lambda x: (x.minx, x.miny, x.maxx, x.maxy), axis=1)
6774

@@ -1287,6 +1294,9 @@ def get_required_arrays(vis_types, blend_types):
12871294
req_arrays["opns_1"] = True
12881295
req_arrays["neg_opns_1"] = True
12891296

1297+
if "new_blend" in blend_types:
1298+
req_arrays["slrm_1"] = True
1299+
12901300
return req_arrays
12911301

12921302

@@ -1493,6 +1503,7 @@ def get_raster_vrt(vrt_path, extents, buffer):
14931503
vrt_nodata = vrt.nodata
14941504
vrt_transform = vrt.transform
14951505
vrt_crs = vrt.crs
1506+
nodata_val = vrt.nodata
14961507

14971508
# ADD BUFFER TO EXTENTS (LBRT) - transform pixels to meters!
14981509
buffer_m = buffer * vrt_res[0]
@@ -1511,8 +1522,13 @@ def get_raster_vrt(vrt_path, extents, buffer):
15111522
# boundless - if window falls out of bounds, read it and fill with NaNs
15121523
win_array = vrt.read(window=buff_window, boundless=True)
15131524

1525+
# Deal with nodata
1526+
if nodata_val is not None and not np.isnan(nodata_val):
1527+
# Ensure array is float to support np.nan
1528+
win_array = win_array.astype("float32", copy=False)
1529+
win_array[win_array == nodata_val] = np.nan
1530+
15141531
# Save transform object of both extents (original and buffered)
1515-
buff_transform = vrt.window_transform(buff_window)
15161532
orig_transform = vrt.window_transform(orig_window)
15171533

15181534
# For raster with only one band, remove first axis from the array (RVT requirement)
@@ -1522,7 +1538,7 @@ def get_raster_vrt(vrt_path, extents, buffer):
15221538
# Prepare output metadata profile
15231539
out_profile = {
15241540
'driver': 'GTiff',
1525-
'nodata': None,
1541+
'nodata': np.nan,
15261542
'width': win_array.shape[1] - 2 * buffer,
15271543
'height': win_array.shape[0] - 2 * buffer,
15281544
'count': 1,
@@ -1535,7 +1551,6 @@ def get_raster_vrt(vrt_path, extents, buffer):
15351551
"array": win_array,
15361552
"resolution": vrt_res,
15371553
"no_data": vrt_nodata,
1538-
"buff_transform": buff_transform,
15391554
"orig_transform": orig_transform,
15401555
"crs": vrt_crs,
15411556
"profile": out_profile

0 commit comments

Comments
 (0)