From a2567297b3f117180a2c6f655cd0277d3db27f12 Mon Sep 17 00:00:00 2001 From: Wang Boyu Date: Fri, 22 Mar 2024 10:51:38 -0400 Subject: [PATCH 1/2] fix: remove old map layers before rendering new layers --- mesa_geo/visualization/templates/js/MapModule.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mesa_geo/visualization/templates/js/MapModule.js b/mesa_geo/visualization/templates/js/MapModule.js index 2a29cc16..1ba222e6 100644 --- a/mesa_geo/visualization/templates/js/MapModule.js +++ b/mesa_geo/visualization/templates/js/MapModule.js @@ -32,13 +32,19 @@ const MapModule = function (view, zoom, map_width, map_height, tiles, scale_opti } } + let mapLayers = [] let hasFitBounds = false this.renderLayers = function (layers) { + mapLayers.forEach(layer => {layer.remove()}) + mapLayers = [] + layers.rasters.forEach(function (layer) { - L.imageOverlay(layer, layers.total_bounds).addTo(Lmap) + const rasterLayer = L.imageOverlay(layer, layers.total_bounds).addTo(Lmap) + mapLayers.push(rasterLayer) }) layers.vectors.forEach(function (layer) { - L.geoJSON(layer).addTo(Lmap) + const vectorLayer = L.geoJSON(layer).addTo(Lmap) + mapLayers.push(vectorLayer) }) if (!hasFitBounds && !customView && layers.total_bounds.length !== 0) { Lmap.fitBounds(layers.total_bounds) @@ -66,6 +72,8 @@ const MapModule = function (view, zoom, map_width, map_height, tiles, scale_opti this.reset = function () { agentLayer.remove() + mapLayers.forEach(layer => {layer.remove()}) + mapLayers = [] } } From c307dbcf8344acf3dc2ad666bf2cddd600cef46e Mon Sep 17 00:00:00 2001 From: Wang Boyu Date: Sat, 23 Mar 2024 11:25:28 -0400 Subject: [PATCH 2/2] fix AttributeError when creating geodataframe from geoagents --- mesa_geo/geospace.py | 4 ++++ tests/test_GeoSpace.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/mesa_geo/geospace.py b/mesa_geo/geospace.py index 83d6ab20..9fc285c6 100644 --- a/mesa_geo/geospace.py +++ b/mesa_geo/geospace.py @@ -458,5 +458,9 @@ def get_agents_as_GeoDataFrame(self, agent_cls=GeoAgent) -> gpd.GeoDataFrame: } agents_list.append(agent_dict) agents_gdf = gpd.GeoDataFrame.from_records(agents_list, index="unique_id") + # workaround for geometry column not being set in `from_records` + # see https://github.com/geopandas/geopandas/issues/3152 + # may be removed when the issue is resolved + agents_gdf.set_geometry("geometry", inplace=True) agents_gdf.crs = crs return agents_gdf diff --git a/tests/test_GeoSpace.py b/tests/test_GeoSpace.py index 9e338d7a..c739aa4a 100644 --- a/tests/test_GeoSpace.py +++ b/tests/test_GeoSpace.py @@ -143,6 +143,10 @@ def test_get_agents_as_GeoDataFrame(self): for agent in self.agents ] agents_gdf = gpd.GeoDataFrame.from_records(agents_list, index="unique_id") + # workaround for geometry column not being set in `from_records` + # see https://github.com/geopandas/geopandas/issues/3152 + # may be removed when the issue is resolved + agents_gdf.set_geometry("geometry", inplace=True) agents_gdf.crs = self.geo_space.crs pd.testing.assert_frame_equal(