Skip to content

Commit 6f3e077

Browse files
wang-boyurht
authored andcommitted
gis: use vsigzip handle to read gzip files using rasterio
ref: https://gdal.org/user/virtual_file_systems.html#vsigzip-gzipped-file
1 parent 998ed61 commit 6f3e077

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

gis/population/population/space.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import gzip
43
import uuid
54

65
import geopandas as gpd
@@ -35,13 +34,14 @@ def __init__(self, crs):
3534

3635
def load_data(self, population_gzip_file, lake_zip_file, world_zip_file):
3736
world_size = gpd.GeoDataFrame.from_file(world_zip_file)
38-
with gzip.open(population_gzip_file, "rb") as population_file:
39-
raster_layer = RasterLayer.from_file(
40-
population_file, cell_cls=UgandaCell, attr_name="population"
41-
)
42-
raster_layer.crs = world_size.crs
43-
raster_layer.total_bounds = world_size.total_bounds
44-
self.add_layer(raster_layer)
37+
raster_layer = RasterLayer.from_file(
38+
f"/vsigzip/{population_gzip_file}",
39+
cell_cls=UgandaCell,
40+
attr_name="population",
41+
)
42+
raster_layer.crs = world_size.crs
43+
raster_layer.total_bounds = world_size.total_bounds
44+
self.add_layer(raster_layer)
4545
self.lake = gpd.GeoDataFrame.from_file(lake_zip_file).geometry[0]
4646
self.add_agents(GeoAgent(uuid.uuid4().int, None, self.lake, self.crs))
4747

gis/rainfall/rainfall/space.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import gzip
4-
53
import mesa
64
import mesa_geo as mg
75
import numpy as np
@@ -33,11 +31,10 @@ def __init__(self, crs, water_height):
3331
self.outflow = 0
3432

3533
def set_elevation_layer(self, elevation_gzip_file, crs):
36-
with gzip.open(elevation_gzip_file, "rb") as elevation_file:
37-
raster_layer = mg.RasterLayer.from_file(
38-
elevation_file, cell_cls=LakeCell, attr_name="elevation"
39-
)
40-
raster_layer.crs = crs
34+
raster_layer = mg.RasterLayer.from_file(
35+
f"/vsigzip/{elevation_gzip_file}", cell_cls=LakeCell, attr_name="elevation"
36+
)
37+
raster_layer.crs = crs
4138
raster_layer.apply_raster(
4239
data=np.zeros(shape=(1, raster_layer.height, raster_layer.width)),
4340
attr_name="water_level",

gis/urban_growth/urban_growth/space.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import gzip
43
import random
54

65
import mesa
@@ -96,7 +95,7 @@ def load_datasets(
9695
"land_use": land_use_data,
9796
}
9897
for attribute_name, data_file in data.items():
99-
with gzip.open(data_file, "rb") as f, rio.open(f, "r") as dataset:
98+
with rio.open(f"/vsigzip/{data_file}", "r") as dataset:
10099
values = dataset.read()
101100
self.raster_layer.apply_raster(values, attr_name=attribute_name)
102101

0 commit comments

Comments
 (0)