Skip to content

Commit

Permalink
update examples to create RasterLayer from file
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu authored and rht committed Jul 27, 2022
1 parent bab4758 commit 00cfc35
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
16 changes: 8 additions & 8 deletions examples/rainfall/rainfall/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import mesa
import numpy as np
import rasterio as rio

from mesa_geo import Cell, RasterLayer
from mesa_geo.geospace import GeoSpace
Expand Down Expand Up @@ -34,13 +33,14 @@ def __init__(self, crs, water_height):

def set_elevation_layer(self, elevation_gzip_file, crs):
with gzip.open(elevation_gzip_file, "rb") as elevation_file:
with rio.open(elevation_file, "r") as dataset:
values = dataset.read()
_, height, width = values.shape
total_bounds = dataset.bounds
raster_layer = RasterLayer(width, height, crs, total_bounds, cell_cls=LakeCell)
raster_layer.apply_raster(data=values, name="elevation")
raster_layer.apply_raster(data=np.zeros(values.shape), name="water_level")
raster_layer = RasterLayer.from_file(
elevation_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",
)
super().add_layer(raster_layer)

@property
Expand Down
19 changes: 5 additions & 14 deletions examples/uganda/uganda/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import geopandas as gpd
import mesa
import rasterio as rio

from mesa_geo.geoagent import GeoAgent
from mesa_geo.geospace import GeoSpace
Expand Down Expand Up @@ -38,20 +37,12 @@ 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:
with rio.open(population_file, "r") as population_data:
values = population_data.read()
_, height, width = values.shape
self.add_layer(
RasterLayer(
width,
height,
world_size.crs,
world_size.total_bounds,
cell_cls=UgandaCell,
raster_layer = RasterLayer.from_file(
population_file, cell_cls=UgandaCell, attr_name="population"
)
)
self.population_layer.apply_raster(values, 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
2 changes: 1 addition & 1 deletion examples/urban_growth/urban_growth/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def load_datasets(
with gzip.open(data_file, "rb") as f:
with rio.open(f, "r") as dataset:
values = dataset.read()
self.raster_layer.apply_raster(values, name=attribute_name)
self.raster_layer.apply_raster(values, attr_name=attribute_name)

for cell in self.raster_layer:
cell.urban = True if cell.urban == 2 else False
Expand Down

0 comments on commit 00cfc35

Please sign in to comment.