Skip to content

Commit

Permalink
gis: use vsigzip handle to read gzip files using rasterio
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu authored and rht committed Sep 10, 2023
1 parent 998ed61 commit 6f3e077
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
16 changes: 8 additions & 8 deletions gis/population/population/space.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import gzip
import uuid

import geopandas as gpd
Expand Down Expand Up @@ -35,13 +34,14 @@ def __init__(self, crs):

def load_data(self, population_gzip_file, lake_zip_file, world_zip_file):
world_size = gpd.GeoDataFrame.from_file(world_zip_file)
with gzip.open(population_gzip_file, "rb") as population_file:
raster_layer = RasterLayer.from_file(
population_file, cell_cls=UgandaCell, attr_name="population"
)
raster_layer.crs = world_size.crs
raster_layer.total_bounds = world_size.total_bounds
self.add_layer(raster_layer)
raster_layer = RasterLayer.from_file(
f"/vsigzip/{population_gzip_file}",
cell_cls=UgandaCell,
attr_name="population",
)
raster_layer.crs = world_size.crs
raster_layer.total_bounds = world_size.total_bounds
self.add_layer(raster_layer)
self.lake = gpd.GeoDataFrame.from_file(lake_zip_file).geometry[0]
self.add_agents(GeoAgent(uuid.uuid4().int, None, self.lake, self.crs))

Expand Down
11 changes: 4 additions & 7 deletions gis/rainfall/rainfall/space.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import gzip

import mesa
import mesa_geo as mg
import numpy as np
Expand Down Expand Up @@ -33,11 +31,10 @@ def __init__(self, crs, water_height):
self.outflow = 0

def set_elevation_layer(self, elevation_gzip_file, crs):
with gzip.open(elevation_gzip_file, "rb") as elevation_file:
raster_layer = mg.RasterLayer.from_file(
elevation_file, cell_cls=LakeCell, attr_name="elevation"
)
raster_layer.crs = crs
raster_layer = mg.RasterLayer.from_file(
f"/vsigzip/{elevation_gzip_file}", cell_cls=LakeCell, attr_name="elevation"
)
raster_layer.crs = crs
raster_layer.apply_raster(
data=np.zeros(shape=(1, raster_layer.height, raster_layer.width)),
attr_name="water_level",
Expand Down
3 changes: 1 addition & 2 deletions gis/urban_growth/urban_growth/space.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import gzip
import random

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

Expand Down

0 comments on commit 6f3e077

Please sign in to comment.