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

gis: fix user warning when testing geo schelling example #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion gis/geo_schelling/model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import random
from pathlib import Path

import geopandas as gpd
import libpysal
import mesa
import mesa_geo as mg

script_directory = Path(__file__).resolve().parent


def get_largest_connected_components(gdf):
"""Get the largest connected component of a GeoDataFrame."""
# create spatial weights matrix
W = libpysal.weights.Queen.from_dataframe(
gdf, use_index=True, silence_warnings=True
)
# get component labels
gdf["component"] = W.component_labels
# get the largest component
largest_component = gdf["component"].value_counts().idxmax()
# subset the GeoDataFrame
gdf = gdf[gdf["component"] == largest_component]
return gdf


class SchellingAgent(mg.GeoAgent):
"""Schelling segregation agent."""

Expand Down Expand Up @@ -71,7 +88,9 @@ def __init__(self, density=0.6, minority_pc=0.2, export_data=False):
# Set up the grid with patches for every NUTS region
ac = mg.AgentCreator(SchellingAgent, model=self)
data_path = script_directory / "data/nuts_rg_60M_2013_lvl_2.geojson"
agents = ac.from_file(filename=data_path)
agents_gdf = gpd.read_file(data_path)
agents_gdf = get_largest_connected_components(agents_gdf)
agents = ac.from_GeoDataFrame(agents_gdf, unique_id="index")
self.space.add_agents(agents)

# Set up agents
Expand Down
4 changes: 1 addition & 3 deletions gis/geo_schelling/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def schelling_draw(agent):


happy_element = HappyElement()
map_element = mg.visualization.MapModule(
schelling_draw, [52, 12], 4, tiles=xyz.CartoDB.Positron
)
map_element = mg.visualization.MapModule(schelling_draw, tiles=xyz.CartoDB.Positron)
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
server = mesa.visualization.ModularServer(
GeoSchelling, [map_element, happy_element, happy_chart], "Schelling", model_params
Expand Down
Loading