Handling a ROI when loading raster data #642
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves GlacioHack/xdem#602
Description
This PR introduces the functionality for handling a Region of Interest (ROI) when loading raster data. Specifically, it ensures that rasters can be loaded with a specific subset of their data, as defined by a ROI. This functionality is available during the raster initialization. The ROI can be either pixel-based or georeferenced.
Changes
roi
parameter in theRaster
class constructor:roi
parameter is introduced to specify the region of interest when loading a raster. It is a dictionary with the keys:x
,y
,w
, andh
if pixel-based (where (x
,y
) top-left corner coordinates and (w
,h
) the dimensions (width, height) in pixels).left
,bottom
,right
,top
and optionalcrs
(defaults to EPSG:4326) if georeferenced, representing the bounding box of the region to load.Raster
class to crop the raster data during initialization. If provided, the data will be sliced accordingly, while also adjusting the transform and metadata to match the cropped area.convert_roi
method:_load_rio
function modification:_load_rio
function now supports the roi parameter to load a specific subset of raster data directly from the disk.Window
for reading the data in the specified region, ensuring that only the relevant part of the raster is loaded into memory.from_array
method now supports an optionalroi
parameter. This allows users to define a ROI when creating a raster from an in-memory numpy array.Tests
test_raster_with_roi
: This test verifies the correct behavior when loading raster data with a specified ROI. It checks that the raster is properly cropped and ensures the output data matches expectations when applying either a georeferenced or pixel-based ROI. This ensures the convert_roi method functions as intended in real-world use cases.test_init
andtest_from_array
: These tests have been extended to validate behavior when a ROI is provided but the raster is initialized without a file path. The tests ensure that the ROI is correctly handled when working with arrays directly, confirming the method's compatibility with non-file-based rasters.Documentation
crop
in the raster documentation has been updated with the possibility to pass a ROI in parameter and crop the raster during initialization.Example