From fc4028b52896a320b8c5d8328578c5fa33df6fed Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Sun, 1 Sep 2024 14:01:03 +0200 Subject: [PATCH] Expose rasterio's opener argument in Rasterlayer.from_file This commit allows passing Rasterio's new opener argument from the RasterLayer.from_file() method. This allows for easier reading of gzip files, among others. (#237) --- mesa_geo/raster_layers.py | 11 ++++++++--- pyproject.toml | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mesa_geo/raster_layers.py b/mesa_geo/raster_layers.py index 98406b6c..a39c7b15 100644 --- a/mesa_geo/raster_layers.py +++ b/mesa_geo/raster_layers.py @@ -9,7 +9,7 @@ import itertools import math import uuid -from collections.abc import Iterable, Iterator, Sequence +from collections.abc import Callable, Iterable, Iterator, Sequence from typing import Any, cast, overload import numpy as np @@ -533,7 +533,11 @@ def to_image(self, colormap) -> ImageLayer: @classmethod def from_file( - cls, raster_file: str, cell_cls: type[Cell] = Cell, attr_name: str | None = None + cls, + raster_file: str, + cell_cls: type[Cell] = Cell, + attr_name: str | None = None, + rio_opener: Callable | None = None, ) -> RasterLayer: """ Creates a RasterLayer from a raster file. @@ -542,9 +546,10 @@ def from_file( :param Type[Cell] cell_cls: The class of the cells in the layer. :param str | None attr_name: The name of the attribute to use for the cell values. If None, a random name will be generated. Default is None. + :param Callable | None rio_opener: A callable passed to Rasterio open() function. """ - with rio.open(raster_file, "r") as dataset: + with rio.open(raster_file, "r", opener=rio_opener) as dataset: values = dataset.read() _, height, width = values.shape total_bounds = [ diff --git a/pyproject.toml b/pyproject.toml index 0c0f1751..df22e10b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ dependencies = [ "geopandas", "libpysal", "rtree", - "rasterio", + "rasterio>=1.4b1", "shapely", "pyproj", "folium",