Skip to content

Commit

Permalink
gis: Use gzip.open opener in RasterLayer.from_file (projectmesa#184)
Browse files Browse the repository at this point in the history
Use pass the new rio_opener argument with gzip.open in RasterLayer.from_file(). This prevents having to do the weird /vsigzip/ stuff.

Note that urban_growth uses the rasterio opener directly, while the other two examples pass it via the mesa_geo.raster_layers.RasterLayer.open_file() method.
  • Loading branch information
EwoutH authored Sep 2, 2024
1 parent 3d29e4c commit dee6363
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion gis/population/population/space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import gzip
import uuid

import geopandas as gpd
Expand Down Expand Up @@ -36,9 +37,10 @@ def __init__(self, crs):
def load_data(self, population_gzip_file, lake_zip_file, world_zip_file, model):
world_size = gpd.GeoDataFrame.from_file(world_zip_file)
raster_layer = RasterLayer.from_file(
f"/vsigzip/{population_gzip_file}",
population_gzip_file,
cell_cls=UgandaCell,
attr_name="population",
rio_opener=gzip.open,
)
raster_layer.crs = world_size.crs
raster_layer.total_bounds = world_size.total_bounds
Expand Down
5 changes: 4 additions & 1 deletion gis/rainfall/rainfall/space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import gzip

import mesa
import mesa_geo as mg
import numpy as np
Expand Down Expand Up @@ -34,9 +36,10 @@ def __init__(self, crs, water_height, model):

def set_elevation_layer(self, elevation_gzip_file, crs):
raster_layer = mg.RasterLayer.from_file(
f"/vsigzip/{elevation_gzip_file}",
elevation_gzip_file,
cell_cls=LakeCell,
attr_name="elevation",
rio_opener=gzip.open,
)
raster_layer.crs = crs
raster_layer.apply_raster(
Expand Down
3 changes: 2 additions & 1 deletion gis/urban_growth/urban_growth/space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import gzip
import random

import mesa
Expand Down Expand Up @@ -96,7 +97,7 @@ def load_datasets(
"land_use": land_use_data,
}
for attribute_name, data_file in data.items():
with rio.open(f"/vsigzip/{data_file}", "r") as dataset:
with rio.open(data_file, "r", opener=gzip.open) as dataset:
values = dataset.read()
self.raster_layer.apply_raster(values, attr_name=attribute_name)

Expand Down

0 comments on commit dee6363

Please sign in to comment.