Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data paths and model passing in GIS examples #177

Merged
merged 11 commits into from
Aug 26, 2024
16 changes: 12 additions & 4 deletions gis/population/population/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import os
import random
import uuid

Expand All @@ -9,6 +10,8 @@

from .space import UgandaArea

script_directory = os.path.dirname(os.path.abspath(__file__))
rht marked this conversation as resolved.
Show resolved Hide resolved


class Person(mg.GeoAgent):
MOBILITY_RANGE_X = 0.0
Expand Down Expand Up @@ -52,13 +55,18 @@ def step(self):
class Population(mesa.Model):
def __init__(
self,
population_gzip_file="data/popu.asc.gz",
lake_zip_file="data/lake.zip",
world_zip_file="data/clip.zip",
population_gzip_file="../data/popu.asc.gz",
lake_zip_file="../data/lake.zip",
world_zip_file="../data/clip.zip",
):
super().__init__()
self.space = UgandaArea(crs="epsg:4326")
self.space.load_data(population_gzip_file, lake_zip_file, world_zip_file)
self.space.load_data(
os.path.normpath(os.path.join(script_directory, population_gzip_file)),
os.path.join(script_directory, lake_zip_file),
os.path.join(script_directory, world_zip_file),
model=self,
)
pixel_size_x, pixel_size_y = self.space.population_layer.resolution
Person.MOBILITY_RANGE_X = pixel_size_x / 2.0
Person.MOBILITY_RANGE_Y = pixel_size_y / 2.0
Expand Down
8 changes: 5 additions & 3 deletions gis/population/population/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class UgandaCell(Cell):

def __init__(
self,
model,
pos: mesa.space.Coordinate | None = None,
indices: mesa.space.Coordinate | None = None,
):
super().__init__(pos, indices)
super().__init__(model, pos, indices)
self.population = None

def step(self):
Expand All @@ -32,18 +33,19 @@ class UgandaArea(GeoSpace):
def __init__(self, crs):
super().__init__(crs=crs)

def load_data(self, population_gzip_file, lake_zip_file, world_zip_file):
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}",
cell_cls=UgandaCell,
attr_name="population",
model=model,
)
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))
self.add_agents(GeoAgent(uuid.uuid4().int, model, self.lake, self.crs))

@property
def population_layer(self):
Expand Down
8 changes: 6 additions & 2 deletions gis/rainfall/rainfall/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid

import mesa
Expand All @@ -7,6 +8,8 @@

from .space import CraterLake

script_directory = os.path.dirname(os.path.abspath(__file__))


class RaindropAgent(mg.GeoAgent):
def __init__(self, unique_id, model, pos):
Expand Down Expand Up @@ -63,7 +66,7 @@ def __init__(self, rain_rate=500, water_height=5, export_data=False, num_steps=2
self.export_data = export_data
self.num_steps = num_steps

self.space = CraterLake(crs="epsg:4326", water_height=water_height)
self.space = CraterLake(crs="epsg:4326", water_height=water_height, model=self)
self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
{
Expand All @@ -73,7 +76,8 @@ def __init__(self, rain_rate=500, water_height=5, export_data=False, num_steps=2
}
)

self.space.set_elevation_layer("data/elevation.asc.gz", crs="epsg:4326")
data_path = os.path.join(script_directory, "../data/elevation.asc.gz")
self.space.set_elevation_layer(data_path, crs="epsg:4326")

@property
def contained(self):
Expand Down
11 changes: 8 additions & 3 deletions gis/rainfall/rainfall/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class LakeCell(mg.Cell):

def __init__(
self,
model,
pos: mesa.space.Coordinate | None = None,
indices: mesa.space.Coordinate | None = None,
):
super().__init__(pos, indices)
super().__init__(model, pos, indices)
self.elevation = None
self.water_level = None
self.water_level_normalized = None
Expand All @@ -25,14 +26,18 @@ def step(self):


class CraterLake(mg.GeoSpace):
def __init__(self, crs, water_height):
def __init__(self, crs, water_height, model):
super().__init__(crs=crs)
self.model = model
self.water_height = water_height
self.outflow = 0

def set_elevation_layer(self, elevation_gzip_file, crs):
raster_layer = mg.RasterLayer.from_file(
f"/vsigzip/{elevation_gzip_file}", cell_cls=LakeCell, attr_name="elevation"
f"/vsigzip/{elevation_gzip_file}",
cell_cls=LakeCell,
attr_name="elevation",
model=self.model,
)
raster_layer.crs = crs
raster_layer.apply_raster(
Expand Down
18 changes: 13 additions & 5 deletions gis/urban_growth/urban_growth/model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os

import mesa
import numpy as np

from .space import City

script_directory = os.path.dirname(os.path.abspath(__file__))


class UrbanGrowth(mesa.Model):
def __init__(
Expand Down Expand Up @@ -64,14 +68,18 @@ def _load_data(self) -> None:
width=self.world_width,
height=self.world_height,
crs="epsg:3857",
model=self,
total_bounds=[-901575.0, 1442925.0, -885645.0, 1454745.0],
)

labels = ["urban", "slope", "road1", "excluded", "landuse"]
# data_path = os.path.join(script_directory, f"data/{label}_santafe.asc.gz")

self.space.load_datasets(
urban_data="data/urban_santafe.asc.gz",
slope_data="data/slope_santafe.asc.gz",
road_data="data/road1_santafe.asc.gz",
excluded_data="data/excluded_santafe.asc.gz",
land_use_data="data/landuse_santafe.asc.gz",
*(
os.path.join(script_directory, f"../data/{label}_santafe.asc.gz")
for label in labels
)
)

def _check_suitability(self) -> None:
Expand Down
7 changes: 4 additions & 3 deletions gis/urban_growth/urban_growth/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class UrbanCell(mg.Cell):

def __init__(
self,
model: mesa.Model | None = None,
pos: mesa.space.Coordinate | None = None,
indices: mesa.space.Coordinate | None = None,
):
super().__init__(pos, indices)
super().__init__(model, pos, indices)
self.urban = None
self.slope = None
self.road_1 = None
Expand Down Expand Up @@ -78,10 +79,10 @@ def _edge_growth(self) -> None:


class City(mg.GeoSpace):
def __init__(self, width, height, crs, total_bounds):
def __init__(self, width, height, crs, total_bounds, model):
super().__init__(crs=crs)
self.add_layer(
mg.RasterLayer(width, height, crs, total_bounds, cell_cls=UrbanCell)
mg.RasterLayer(width, height, crs, total_bounds, model, cell_cls=UrbanCell)
)

def load_datasets(
Expand Down
Loading