From 9b4f6cf3582491cff5eec6ef3050be93d096dac0 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Sun, 21 Jul 2024 19:31:36 +0200 Subject: [PATCH] Add layers and sources parameter to Map --- _experimental/add_geopandas_source.py | 7 +++++-- maplibre/map.py | 13 +++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/_experimental/add_geopandas_source.py b/_experimental/add_geopandas_source.py index 8a591caa..fa2c355c 100644 --- a/_experimental/add_geopandas_source.py +++ b/_experimental/add_geopandas_source.py @@ -7,8 +7,11 @@ data = read_file(path) -m = Map(MapOptions(style=Carto.POSITRON, bounds=data.total_bounds)) +m = Map( + MapOptions(style=Carto.POSITRON, bounds=data.total_bounds), + layers=[Layer(type=LayerType.LINE, source=data)], +) # m.add_source("states", data) # m.add_layer(Layer(type=LayerType.LINE, source="states")) -m.add_layer(Layer(type=LayerType.LINE, source=data)) +# m.add_layer(Layer(type=LayerType.LINE, source=data)) m.save("/tmp/py-maplibre-express.html") diff --git a/maplibre/map.py b/maplibre/map.py index 7f01bf35..f0fe5386 100644 --- a/maplibre/map.py +++ b/maplibre/map.py @@ -83,9 +83,16 @@ class Map(object): MESSAGE = "not implemented yet" - def __init__(self, map_options: MapOptions = MapOptions(), **kwargs): + def __init__( + self, + map_options: MapOptions = MapOptions(), + sources: dict = None, + layers: list = None, + **kwargs, + ): self.map_options = map_options.to_dict() | kwargs self._message_queue = [] + self.add_layers(layers, sources) def __iter__(self): for k, v in self.to_dict().items(): @@ -170,7 +177,9 @@ def add_layer(self, layer: [Layer | dict], before_id: str = None) -> None: self.add_call("addLayer", layer, before_id) - def add_layers(self, layers: list, sources: dict = None): + def add_layers(self, layers: list = None, sources: dict = None): + layers = layers or [] + sources = sources or dict() for source_id, source in sources.items(): self.add_source(source_id, source)