Skip to content

Commit

Permalink
bring back __geo_interface__ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu committed Apr 3, 2022
1 parent f595397 commit 3c454ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
20 changes: 20 additions & 0 deletions mesa_geo/geoagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import warnings

import geopandas as gpd
import pyproj
from mesa import Agent
from shapely.ops import transform
from shapely.geometry import mapping
from shapely.geometry.base import BaseGeometry


Expand All @@ -27,10 +30,27 @@ def __init__(self, unique_id, model, shape):
self.shape = shape
self._geom = self.shape._geom

def get_transformed_geometry(self, transformer):
"""
Return the transformed geometry given a transformer.
"""
return transform(transformer.transform, self.shape)

def step(self):
"""Advance one step."""
pass

def __geo_interface__(self):
"""Return a GeoJSON Feature.
Removes shape from attributes.
"""
properties = dict(vars(self))
properties["model"] = str(self.model)
shape = properties.pop("shape")
shape = transform(self.model.space.Transformer.transform, shape)

return {"type": "Feature", "geometry": mapping(shape), "properties": properties}


class AgentCreator:
"""Create GeoAgents from files, GeoDataFrames, GeoJSON or Shapely objects."""
Expand Down
17 changes: 13 additions & 4 deletions mesa_geo/geospace.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pyproj
from libpysal import weights
from rtree import index
from shapely.geometry import Point, mapping
from shapely.geometry import Point
from shapely.prepared import prep
from shapely.ops import transform

from mesa_geo.geoagent import GeoAgent

Expand Down Expand Up @@ -38,7 +37,9 @@ def __init__(self, crs="epsg:3857"):
"""
self.crs = pyproj.CRS(crs)
self.WGS84 = pyproj.CRS("epsg:4326")
self.Transformer = pyproj.Transformer.from_crs(self.crs, self.WGS84, always_xy=True)
self.Transformer = pyproj.Transformer.from_crs(
self.crs, self.WGS84, always_xy=True
)

self.bbox = None
self._neighborhood = None
Expand Down Expand Up @@ -94,7 +95,9 @@ def get_intersecting_agents(self, agent, other_agents=None):
intersecting_agents = self.get_relation(agent, "intersects")
return intersecting_agents

def get_neighbors_within_distance(self, agent, distance, center=False, relation="intersects"):
def get_neighbors_within_distance(
self, agent, distance, center=False, relation="intersects"
):
"""Return a list of agents within `distance` of `agent`.
Distance is measured as a buffer around the agent's shape,
Expand Down Expand Up @@ -166,3 +169,9 @@ def update_bbox(self, bbox=None):
@property
def agents(self):
return list(self.idx.agents.values())

@property
def __geo_interface__(self):
"""Return a GeoJSON FeatureCollection."""
features = [a.__geo_interface__() for a in self.agents]
return {"type": "FeatureCollection", "features": features}
25 changes: 16 additions & 9 deletions mesa_geo/visualization/MapModule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from shapely.geometry import mapping
from shapely.ops import transform

from mesa_geo.visualization.ModularVisualization import VisualizationElement

Expand All @@ -10,7 +9,9 @@ class MapModule(VisualizationElement):
package_includes = ["leaflet.js", "LeafletMap.js"]
local_includes = []

def __init__(self, portrayal_method=None, view=[0, 0], zoom=10, map_height=500, map_width=500):
def __init__(
self, portrayal_method=None, view=[0, 0], zoom=10, map_height=500, map_width=500
):
self.portrayal_method = portrayal_method
self.map_height = map_height
self.map_width = map_width
Expand All @@ -20,11 +21,17 @@ def __init__(self, portrayal_method=None, view=[0, 0], zoom=10, map_height=500,
self.js_code = "elements.push(" + new_element + ");"

def render(self, model):
feature_collection = {"type": "FeatureCollection",
"features": [
{"type": "Feature",
"geometry": mapping(transform(model.space.Transformer.transform, agent.shape)),
"properties": self.portrayal_method(agent) if self.portrayal_method else {}}
for agent in model.space.agents
]}
feature_collection = {"type": "FeatureCollection", "features": []}
for agent in model.space.agents:
transformed_geometry = agent.get_transformed_geometry(
model.space.Transformer
)
properties = self.portrayal_method(agent) if self.portrayal_method else {}
feature_collection["features"].append(
{
"type": "Feature",
"geometry": mapping(transformed_geometry),
"properties": properties,
}
)
return feature_collection

0 comments on commit 3c454ab

Please sign in to comment.