Skip to content

Commit aca30a2

Browse files
committed
gis: fix user warning when testing geo schelling example
[pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci update
1 parent 330b4d9 commit aca30a2

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

gis/geo_schelling/model.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import random
22
from pathlib import Path
33

4+
import geopandas as gpd
5+
import libpysal
46
import mesa
57
import mesa_geo as mg
68

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

911

12+
def get_largest_connected_components(gdf):
13+
"""Get the largest connected component of a GeoDataFrame."""
14+
# create spatial weights matrix
15+
W = libpysal.weights.Queen.from_dataframe(
16+
gdf, use_index=True, silence_warnings=True
17+
)
18+
# get component labels
19+
gdf["component"] = W.component_labels
20+
# get the largest component
21+
largest_component = gdf["component"].value_counts().idxmax()
22+
# subset the GeoDataFrame
23+
gdf = gdf[gdf["component"] == largest_component]
24+
return gdf
25+
26+
1027
class SchellingAgent(mg.GeoAgent):
1128
"""Schelling segregation agent."""
1229

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

7796
# Set up agents

gis/geo_schelling/server.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def schelling_draw(agent):
3838

3939

4040
happy_element = HappyElement()
41-
map_element = mg.visualization.MapModule(
42-
schelling_draw, [52, 12], 4, tiles=xyz.CartoDB.Positron
43-
)
41+
map_element = mg.visualization.MapModule(schelling_draw, tiles=xyz.CartoDB.Positron)
4442
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
4543
server = mesa.visualization.ModularServer(
4644
GeoSchelling, [map_element, happy_element, happy_chart], "Schelling", model_params

0 commit comments

Comments
 (0)