Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions rasters/raster_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)