Skip to content

Commit

Permalink
Expose rasterio's opener argument in Rasterlayer.from_file This commi…
Browse files Browse the repository at this point in the history
…t allows passing Rasterio's new opener argument from the RasterLayer.from_file() method. This allows for easier reading of gzip files, among others. (#237)
  • Loading branch information
EwoutH authored Sep 1, 2024
1 parent 93f04f6 commit fc4028b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions mesa_geo/raster_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies = [
"geopandas",
"libpysal",
"rtree",
"rasterio",
"rasterio>=1.4b1",
"shapely",
"pyproj",
"folium",
Expand Down

0 comments on commit fc4028b

Please sign in to comment.