From 99eeec3f5d788e98c2473a4a0e5be923800408f5 Mon Sep 17 00:00:00 2001 From: Wang Boyu Date: Thu, 24 Nov 2022 15:53:24 -0500 Subject: [PATCH] add scale to Leaflet map --- examples/urban_growth/urban_growth/server.py | 1 + .../visualization/modules/MapVisualization.py | 16 ++++++++++++---- mesa_geo/visualization/templates/js/MapModule.js | 5 ++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/examples/urban_growth/urban_growth/server.py b/examples/urban_growth/urban_growth/server.py index 84f242fc..5edfd6e0 100644 --- a/examples/urban_growth/urban_growth/server.py +++ b/examples/urban_growth/urban_growth/server.py @@ -46,6 +46,7 @@ def render(self, model): zoom=12.1, map_height=394, map_width=531, + scale_options={"imperial": False}, ) urbanized_text = UrbanizedText() urbanized_chart = mesa.visualization.ChartModule( diff --git a/mesa_geo/visualization/modules/MapVisualization.py b/mesa_geo/visualization/modules/MapVisualization.py index 0bef2d9d..b3a8795a 100644 --- a/mesa_geo/visualization/modules/MapVisualization.py +++ b/mesa_geo/visualization/modules/MapVisualization.py @@ -54,6 +54,7 @@ def __init__( zoom=None, map_width=500, map_height=500, + scale_options=None, ): """ Create a new MapModule. @@ -68,14 +69,21 @@ def __init__( of the space. Default is None. :param map_width: The width of the map in pixels. Default is 500. :param map_height: The height of the map in pixels. Default is 500. + :param scale_options: A dictionary of options for the map scale. Default is None (no map scale). + The available options can be found at: https://leafletjs.com/reference.html#control-scale-option """ self.portrayal_method = portrayal_method self._crs = "epsg:4326" - if view is None and zoom is None: - new_element = f"new MapModule(null, null, {map_width}, {map_height})" - else: - new_element = f"new MapModule({view}, {zoom}, {map_width}, {map_height})" + view_js = "null" if view is None else view + zoom_js = "null" if zoom is None else zoom + scale_options_js = ( + "null" + if scale_options is None + else str(scale_options).replace("True", "true").replace("False", "false") + ) + + new_element = f"new MapModule({view_js}, {zoom_js}, {map_width}, {map_height}, {scale_options_js})" self.js_code = f"elements.push({new_element});" def render(self, model): diff --git a/mesa_geo/visualization/templates/js/MapModule.js b/mesa_geo/visualization/templates/js/MapModule.js index 4aa0d49a..0b08faa1 100644 --- a/mesa_geo/visualization/templates/js/MapModule.js +++ b/mesa_geo/visualization/templates/js/MapModule.js @@ -1,4 +1,4 @@ -const MapModule = function (view, zoom, map_width, map_height) { +const MapModule = function (view, zoom, map_width, map_height, scale_options) { // Create the map tag const map_tag = document.createElement("div"); map_tag.style.width = map_width + "px"; @@ -16,6 +16,9 @@ const MapModule = function (view, zoom, map_width, map_height) { if (customView) { Lmap.setView(view, zoom) } + if (scale_options !== null) { + L.control.scale(scale_options).addTo(Lmap) + } let agentLayer = L.geoJSON().addTo(Lmap) // create the OSM tile layer with correct attribution