Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

cannot compute affected area from large mask #246

Closed
JinIgarashi opened this issue Mar 4, 2025 · 0 comments · Fixed by #247
Closed

cannot compute affected area from large mask #246

JinIgarashi opened this issue Mar 4, 2025 · 0 comments · Fixed by #247
Assignees
Labels
bug Something isn't working

Comments

@JinIgarashi
Copy link
Contributor

this is related to RWI (#62).

currently it uses mask raster by mask layer with rasterio. the function is implemented at below.

def clip_raster_with_mask(self, source, mask, output_path):
"""
Clips the raster data at 'source' using the mask raster at 'mask' and saves the result to 'output_path'.
:param source: Path to the source raster file.
:param mask: Path to the mask raster file (e.g., project.raster_mask).
:param output_path: File path to save the clipped raster.
"""
# Open the mask raster and read the first band
with rasterio.open(mask) as mask_src:
mask_data = mask_src.read(1)
# Define the mask region as areas where the value is greater than 0 (adjust condition as needed)
binary_mask = mask_data > 0
# Extract polygons from the mask raster based on the binary_mask
mask_shapes = []
for geom, value in rasterio.features.shapes(mask_data, mask=binary_mask, transform=mask_src.transform):
# If the value equals 1, use that region as a mask
if value == 1:
mask_shapes.append(shape(geom))
# Open the main raster and clip it using the extracted polygons
with rasterio.open(source) as src:
# Convert the polygon geometries to dict format using mapping()
out_image, out_transform = rasterio.mask.mask(src, [mapping(geom) for geom in mask_shapes], crop=True)
out_meta = src.meta.copy()
# Update the metadata
out_meta.update({
"driver": "COG",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform,
"compress": 'zstd',
"predictor": 2
})
# Save the clipped raster to the output file
with rasterio.open(output_path, "w", **out_meta) as dest:
dest.write(out_image)

the code fails if raster data is large area. need to find better way to mask raster.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant