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: remove old map layers before rendering new layers #194

Merged
merged 2 commits into from
Mar 24, 2024
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
4 changes: 4 additions & 0 deletions mesa_geo/geospace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions mesa_geo/visualization/templates/js/MapModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/test_GeoSpace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
rht marked this conversation as resolved.
Show resolved Hide resolved
agents_gdf.crs = self.geo_space.crs

pd.testing.assert_frame_equal(
Expand Down
Loading