From df86a41ffb1bb01ff2d26114712605434c562baf Mon Sep 17 00:00:00 2001 From: "Gregory H. Halverson" Date: Thu, 24 Jul 2025 15:55:00 -0700 Subject: [PATCH] geolocation windows --- rasters/raster_geolocation.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rasters/raster_geolocation.py b/rasters/raster_geolocation.py index 99c48a2..a66a881 100644 --- a/rasters/raster_geolocation.py +++ b/rasters/raster_geolocation.py @@ -5,6 +5,7 @@ import numpy as np from scipy.ndimage import zoom from scipy.spatial import cKDTree +from rasterio.windows import Window from .CRS import WGS84 from .raster_geometry import RasterGeometry @@ -338,3 +339,22 @@ def to_dict(self, output_dict: Dict = None, write_geolocation_arrays: bool = Fal output_dict['longitude'] = lon return output_dict + + def window(self, geometry) -> Window: + """ + Returns a rasterio.windows.Window covering the target geometry. + Equivalent to RasterGrid.window, but for geolocation arrays. + """ + + mask = self.index(geometry) + rows, cols = np.where(mask) + + if rows.size == 0 or cols.size == 0: + raise ValueError("No points found within the target geometry.") + + row_off = int(rows.min()) + col_off = int(cols.min()) + height = int(rows.max() - rows.min() + 1) + width = int(cols.max() - cols.min() + 1) + + return Window(col_off=col_off, row_off=row_off, width=width, height=height) \ No newline at end of file