38
-39
-40
-41
+ | def construct_basemap_style(
- name: str = "nice-style", sources: dict = {}, layers: list = []
-) -> dict:
- """Construct a basemap style
-
- Args:
- name (str): The name of the basemap style.
- sources (dict): The sources to be used for the basemap style.
- layers (list): The layers to be used for the basemap style.
- """
- layers = [
- layer.to_dict() if isinstance(layer, Layer) else layer for layer in layers
- ]
- return {"name": name, "version": 8, "sources": sources, "layers": layers}
+51
+52
+53
+54
| def construct_basemap_style(
+ name: str = "nice-style", sources: dict = {}, layers: list = []
+) -> dict:
+ """Construct a basemap style
+
+ Args:
+ name (str): The name of the basemap style.
+ sources (dict): The sources to be used for the basemap style.
+ layers (list): The layers to be used for the basemap style.
+ """
+ layers = [
+ layer.to_dict() if isinstance(layer, Layer) else layer for layer in layers
+ ]
+ return {"name": name, "version": 8, "sources": sources, "layers": layers}
|
diff --git a/api/controls/index.html b/api/controls/index.html
index 0d36ae3..0088089 100644
--- a/api/controls/index.html
+++ b/api/controls/index.html
@@ -1331,7 +1331,7 @@ | class Popup(BaseModel):
+34
| class Popup(MapLibreBaseModel):
"""Popup
Attributes:
@@ -1431,7 +1431,7 @@
| class PopupOptions(BaseModel):
+22
| class PopupOptions(MapLibreBaseModel):
"""Popup options"""
anchor: str = None
@@ -1492,7 +1492,7 @@
- Bases: BaseModel
+ Bases: MapLibreBaseModel
Marker
@@ -1558,7 +1558,7 @@
| class Marker(BaseModel):
+61
| class Marker(MapLibreBaseModel):
"""Marker
Attributes:
@@ -1607,7 +1607,7 @@
- Bases: BaseModel
+ Bases: MapLibreBaseModel
Marker options
@@ -1624,7 +1624,7 @@
| class MarkerOptions(BaseModel):
+47
| class MarkerOptions(MapLibreBaseModel):
"""Marker options"""
anchor: str = None
@@ -1788,17 +1788,13 @@
>>> from maplibre import Map
>>> from maplibre.controls import FullscreenControl, ControlPosition
- >>> map = Map()
->>> map.add_control(FullscreenControl(), ControlPosition.BOTTOM_LEFT)
+ >>> m = Map()
+>>> m.add_control(FullscreenControl(), ControlPosition.BOTTOM_LEFT)
Source code in maplibre/controls.py
- 104
-105
-106
-107
-108
+ | class FullscreenControl(Control):
- """Fullscreen control
-
- Examples:
- >>> from maplibre import Map
- >>> from maplibre.controls import FullscreenControl, ControlPosition
+116
+117
+118
+119
+120
| class FullscreenControl(Control):
+ """Fullscreen control
- >>> map = Map()
- >>> map.add_control(FullscreenControl(), ControlPosition.BOTTOM_LEFT)
- """
+ Examples:
+ >>> from maplibre import Map
+ >>> from maplibre.controls import FullscreenControl, ControlPosition
- # _name: str = ControlType.FULLSCREEN.value
- pass
+ >>> m = Map()
+ >>> m.add_control(FullscreenControl(), ControlPosition.BOTTOM_LEFT)
+ """
+
+ # _name: str = ControlType.FULLSCREEN.value
+ pass
|
@@ -1847,15 +1847,15 @@
Source code in maplibre/controls.py
- | class ScaleControl(Control):
- """Scale control"""
-
- max_width: int = Field(None, serialization_alias="maxWidth")
- unit: Literal["imperial", "metric", "nautical"] = "metric"
+ | class ScaleControl(Control):
+ """Scale control"""
+
+ max_width: int = Field(None, serialization_alias="maxWidth")
+ unit: Literal["imperial", "metric", "nautical"] = "metric"
|
@@ -1900,19 +1900,19 @@
Source code in maplibre/controls.py
- 130
-131
-132
-133
-134
+ | class NavigationControl(Control):
- """Navigation control"""
-
- # _name: str = ControlType.NAVIGATION.value
- show_compass: bool = Field(True, serialization_alias="showCompass")
- show_zoom: bool = Field(True, serialization_alias="showZoom")
- visualize_pitch: bool = Field(False, serialization_alias="visualizePitch")
+136
+137
+138
+139
+140
| class NavigationControl(Control):
+ """Navigation control"""
+
+ # _name: str = ControlType.NAVIGATION.value
+ show_compass: bool = Field(True, serialization_alias="showCompass")
+ show_zoom: bool = Field(True, serialization_alias="showZoom")
+ visualize_pitch: bool = Field(False, serialization_alias="visualizePitch")
|
@@ -1957,23 +1957,23 @@
Source code in maplibre/controls.py
- 119
-120
-121
-122
-123
+ | class GeolocateControl(Control):
- """Geolocate control"""
-
- # _name: str = ControlType.GEOLOCATE.value
- position_options: dict = Field(None, serialization_alias="positionOptions")
- show_accuracy_circle: bool = Field(True, serialization_alias="showAccuracyCircle")
- show_user_heading: bool = Field(False, serialization_alias="showUserHeading")
- show_user_location: bool = Field(True, serialization_alias="showUserLocation")
- track_user_location: bool = Field(False, serialization_alias="trackUserLocation")
+127
+128
+129
+130
+131
| class GeolocateControl(Control):
+ """Geolocate control"""
+
+ # _name: str = ControlType.GEOLOCATE.value
+ position_options: dict = Field(None, serialization_alias="positionOptions")
+ show_accuracy_circle: bool = Field(True, serialization_alias="showAccuracyCircle")
+ show_user_heading: bool = Field(False, serialization_alias="showUserHeading")
+ show_user_location: bool = Field(True, serialization_alias="showUserLocation")
+ track_user_location: bool = Field(False, serialization_alias="trackUserLocation")
|
@@ -2055,25 +2055,25 @@
Source code in maplibre/controls.py
- 169
-170
-171
-172
-173
+ | class InfoBoxControl(Control):
- """InfoBox control
-
- Attributes:
- content (str): Content (HTML or plain text) to be displayed in the info box.
- css_text (str): Optional inline style declaration of the control.
- """
-
- content: str
- css_text: str = Field(None, serialization_alias="cssText")
+178
+179
+180
+181
+182
| class InfoBoxControl(Control):
+ """InfoBox control
+
+ Attributes:
+ content (str): Content (HTML or plain text) to be displayed in the info box.
+ css_text (str): Optional inline style declaration of the control.
+ """
+
+ content: str
+ css_text: str = Field(None, serialization_alias="cssText")
|
@@ -2166,29 +2166,29 @@
Source code in maplibre/controls.py
- 155
-156
-157
-158
-159
+ | class LayerSwitcherControl(Control):
- """Layer switcher control
-
- Attributes:
- layer_ids (list): A list of layer ids to be shown in the layer switcher control.
- theme (Literal["default", "simple"]): The theme of the layer switcher control.
- css_text (str): Optional inline style declaration of the control.
- """
-
- layer_ids: list = Field([], serialization_alias="layerIds")
- theme: Literal["default", "simple"] = "default"
- css_text: str = Field(None, serialization_alias="cssText")
+166
+167
+168
+169
+170
| class LayerSwitcherControl(Control):
+ """Layer switcher control
+
+ Attributes:
+ layer_ids (list): A list of layer ids to be shown in the layer switcher control.
+ theme (Literal["default", "simple"]): The theme of the layer switcher control.
+ css_text (str): Optional inline style declaration of the control.
+ """
+
+ layer_ids: list = Field([], serialization_alias="layerIds")
+ theme: Literal["default", "simple"] = "default"
+ css_text: str = Field(None, serialization_alias="cssText")
|
diff --git a/api/layer/index.html b/api/layer/index.html
index f0b4c9e..e3c1eb0 100644
--- a/api/layer/index.html
+++ b/api/layer/index.html
@@ -1169,7 +1169,7 @@
- Bases: BaseModel
+ Bases: MapLibreBaseModel
Layer properties
@@ -1220,7 +1220,7 @@
|
- The filter expression that is applied to the source of the layer.
+ A filter expression that is applied to the source of the layer.
|
@@ -1231,7 +1231,7 @@
- The layout properties of the layer.
+ The layout properties for the layer.
|
@@ -1264,18 +1264,19 @@
- The paint properties of the layer.
+ The paint properties for the layer.
|
source |
- str | Source
+ str | Source | GeoDataFrame
|
- The name (unique ID) of a source or a source object to be used for the layer.
+ The name (unique ID) of a source, a source object or a GeoDataFrame
+to be used for the layer.
|
@@ -1303,101 +1304,159 @@
Source code in maplibre/layer.py
- | class Layer(BaseModel):
- """Layer properties
-
- Notes:
- See [layers](https://maplibre.org/maplibre-style-spec/layers/) for more details on the
- `paint` and `layout` properties of the layers.
-
- Attributes:
- id (str): **Required.** The unique ID of the layer. Defaults to `str(uuid4())`.
- type (str | LayerType): **Required.** The type of the layer.
- filter (list): The filter expression that is applied to the source of the layer.
- layout (dict): The layout properties of the layer.
- max_zoom (int): The maximum zoom level for the layer.
- min_zoom (int): The minimum zoom level for the layer.
- paint (dict): The paint properties of the layer.
- source (str | Source): The name (unique ID) of a source or a source object to be used for the layer.
- source_layer (str): The layer to use from a vector tile source.
-
- Examples:
- >>> from maplibre.layer import Layer, LayerType
-
- >>> layer = Layer(id="test-layer", type=LayerType.CIRCLE, source="test-source")
- """
-
- id: str = Field(default_factory=lambda: str(uuid4()))
- type: LayerType
- filter: list = None
- layout: dict = None
- max_zoom: int = Field(None, serialization_alias="maxzoom")
- metadata: dict = None
- min_zoom: int = Field(None, serialization_alias="minzoom")
- paint: dict = None
- source: Union[str, Source, dict, None] = None
- source_layer: str = Field(None, serialization_alias="source-layer")
-
- @field_validator("source")
- def validate_source(cls, v):
- if isinstance(v, Source):
- return v.to_dict()
-
- return v
-
- @field_validator("paint", "layout")
- def fix_paint(cls, v):
- if isinstance(v, dict):
- return fix_keys(v)
-
- return v
+ | class Layer(MapLibreBaseModel):
+ """Layer properties
+
+ Notes:
+ See [layers](https://maplibre.org/maplibre-style-spec/layers/) for more details on the
+ `paint` and `layout` properties of the layers.
+
+ Attributes:
+ id (str): **Required.** The unique ID of the layer. Defaults to `str(uuid4())`.
+ type (str | LayerType): **Required.** The type of the layer.
+ filter (list): A filter expression that is applied to the source of the layer.
+ layout (dict): The layout properties for the layer.
+ max_zoom (int): The maximum zoom level for the layer.
+ min_zoom (int): The minimum zoom level for the layer.
+ paint (dict): The paint properties for the layer.
+ source (str | Source | GeoDataFrame): The name (unique ID) of a source, a source object or a GeoDataFrame
+ to be used for the layer.
+ source_layer (str): The layer to use from a vector tile source.
+
+ Examples:
+ >>> from maplibre.layer import Layer, LayerType
+
+ >>> layer = Layer(id="test-layer", type=LayerType.CIRCLE, source="test-source")
+ """
+
+ id: Optional[str] = Field(default_factory=lambda: str(uuid4()))
+ type: LayerType
+ filter: Optional[list] = None
+ layout: Optional[dict] = None
+ max_zoom: Optional[int] = Field(None, serialization_alias="maxzoom")
+ metadata: Optional[dict] = None
+ min_zoom: Optional[int] = Field(None, serialization_alias="minzoom")
+ paint: Optional[dict] = None
+ source: Union[str, Source, dict, GeoDataFrame, None] = None
+ source_layer: Optional[str] = Field(None, serialization_alias="source-layer")
+
+ # TODO: Use model_post_init and set bounds if source is a GeoDataFrame
+ @field_validator("source")
+ def validate_source(cls, v):
+ if GeoDataFrame is not None and isinstance(v, GeoDataFrame):
+ return SimpleFeatures(v).to_source().to_dict()
+
+ if isinstance(v, Source):
+ return v.to_dict()
+
+ return v
+
+ @field_validator("paint", "layout")
+ def fix_paint(cls, v):
+ if isinstance(v, dict):
+ return fix_keys(v)
+
+ return v
+
+ @property
+ def bounds(self) -> tuple | None:
+ try:
+ bounds = get_bounds(self.source["data"])
+ except Exception as e:
+ # print(e)
+ bounds = None
+
+ return bounds
+
+ def set_paint_props(self, **props) -> Layer:
+ if self.paint is None:
+ self.paint = dict()
+
+ self.paint = self.paint | fix_keys(props)
+ return self
+
+ def set_layout_props(self, **props) -> Layer:
+ if self.layout is None:
+ self.layout = dict()
+
+ self.paint = self.paint | fix_keys(props)
+ return self
|
@@ -1511,14 +1570,7 @@
Source code in maplibre/layer.py
- 13
-14
-15
-16
-17
-18
-19
-20
+ | class LayerType(Enum):
- """Rendering type of layer
-
- Attributes:
- CIRCLE: A filled circle.
- FILL: A filled polygon with an optional stroked border.
- FILL_EXTRUSION: An extruded polygon.
- LINE: A stroked line.
- SYMBOL: An icon or a text label.
- RASTER: Raster map textures such as satellite imagery.
- HEATMAP: A heatmap.
- HILLSHADE: A Client-side hillshading visualization based on DEM data.
- BACKGROUND: A background color or pattern.
- """
-
- CIRCLE = "circle"
- FILL = "fill"
- FILL_EXTRUSION = "fill-extrusion"
- LINE = "line"
- SYMBOL = "symbol"
- RASTER = "raster"
- HEATMAP = "heatmap"
- HILLSHADE = "hillshade"
- BACKGROUND = "background"
+36
+37
+38
+39
+40
+41
+42
+43
| class LayerType(Enum):
+ """Rendering type of layer
+
+ Attributes:
+ CIRCLE: A filled circle.
+ FILL: A filled polygon with an optional stroked border.
+ FILL_EXTRUSION: An extruded polygon.
+ LINE: A stroked line.
+ SYMBOL: An icon or a text label.
+ RASTER: Raster map textures such as satellite imagery.
+ HEATMAP: A heatmap.
+ HILLSHADE: A Client-side hillshading visualization based on DEM data.
+ BACKGROUND: A background color or pattern.
+ """
+
+ CIRCLE = "circle"
+ FILL = "fill"
+ FILL_EXTRUSION = "fill-extrusion"
+ LINE = "line"
+ SYMBOL = "symbol"
+ RASTER = "raster"
+ HEATMAP = "heatmap"
+ HILLSHADE = "hillshade"
+ BACKGROUND = "background"
|
diff --git a/api/map/index.html b/api/map/index.html
index c6deaef..a276567 100644
--- a/api/map/index.html
+++ b/api/map/index.html
@@ -663,6 +663,24 @@
+
+
+
+
+
+ fit_bounds
+
+
+
+
+
+
+
+
+ save
+
+
+
@@ -1408,6 +1426,24 @@
+
+
+
+
+
+ fit_bounds
+
+
+
+
+
+
+
+
+ save
+
+
+
@@ -1603,6 +1639,48 @@ |
+
+ sources |
+
+ dict
+ |
+
+
+ Sources to be added to the map. Keys are source IDs.
+
+ |
+
+ None
+ |
+
+
+ layers |
+
+ list
+ |
+
+
+ Layers to be added to the map.
+
+ |
+
+ None
+ |
+
+
+ controls |
+
+ list
+ |
+
+
+ Controls to be added to the map.
+
+ |
+
+ None
+ |
+
**kwargs |
@@ -1623,41 +1701,15 @@
Examples:
>>> from maplibre.map import Map, MapOptions
-
- >>> map_options = MapOptions(center=(9.5, 51.31667), zoom=8)
->>> map = Map(map_options)
->>> dict(map)
-{'mapOptions': {'center': (9.5, 51.31667), 'style': 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json', 'zoom': 8}, 'calls': []}
+>>> map_options = MapOptions(center=(9.5, 51.31667), zoom=8)
+>>> m = Map(map_options)
+>>> dict(m)
+{'mapOptions': {'center': (9.5, 51.31667), 'style': 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json', 'zoom': 8}, 'calls': []}
Source code in maplibre/map.py
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
+ | class Map(object):
- """Map
-
- Args:
- map_options (MapOptions): Map options.
- **kwargs: Keyword arguments that are appended to the `MapOptions` object.
-
- Examples:
- >>> from maplibre.map import Map, MapOptions
-
- >>> map_options = MapOptions(center=(9.5, 51.31667), zoom=8)
- >>> map = Map(map_options)
- >>> dict(map)
- {'mapOptions': {'center': (9.5, 51.31667), 'style': 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json', 'zoom': 8}, 'calls': []}
- """
-
- MESSAGE = "not implemented yet"
-
- def __init__(self, map_options: MapOptions = MapOptions(), **kwargs):
- self.map_options = map_options.to_dict() | kwargs
- self._message_queue = []
-
- def __iter__(self):
- for k, v in self.to_dict().items():
- yield k, v
-
- def to_dict(self) -> dict:
- return {"mapOptions": self.map_options, "calls": self._message_queue}
-
- """
- @property
- def sources(self) -> list:
- return [item["data"] for item in self._calls if item["name"] == "addSource"]
-
- @property
- def layers(self) -> list:
- return [item["data"] for item in self._calls if item["name"] == "addLayer"]
- """
-
- # TODO: Rename to add_map_call
- def add_call_(self, func_name: str, params: list) -> None:
- self._message_queue.append(
- {"name": "applyFunc", "data": {"funcName": func_name, "params": params}}
- )
+348
+349
+350
+351
+352
+353
+354
+355
+356
+357
+358
+359
+360
+361
+362
+363
+364
+365
+366
+367
+368
+369
+370
+371
+372
+373
+374
+375
+376
+377
+378
+379
+380
+381
+382
+383
+384
+385
+386
+387
+388
+389
+390
+391
+392
+393
+394
+395
+396
+397
+398
+399
+400
+401
+402
+403
+404
+405
+406
+407
+408
+409
+410
+411
+412
+413
+414
+415
+416
+417
+418
+419
+420
+421
+422
+423
+424
+425
+426
+427
+428
+429
+430
+431
| class Map(object):
+ """Map
+
+ Args:
+ map_options (MapOptions): Map options.
+ sources (dict): Sources to be added to the map. Keys are source IDs.
+ layers (list): Layers to be added to the map.
+ controls (list): Controls to be added to the map.
+ **kwargs: Keyword arguments that are appended to the `MapOptions` object.
+
+ Examples:
+ >>> from maplibre.map import Map, MapOptions
+ >>> map_options = MapOptions(center=(9.5, 51.31667), zoom=8)
+ >>> m = Map(map_options)
+ >>> dict(m)
+ {'mapOptions': {'center': (9.5, 51.31667), 'style': 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json', 'zoom': 8}, 'calls': []}
+ """
+
+ MESSAGE = "not implemented yet"
- def add_call(self, method_name: str, *args) -> None:
- """Add a method call that is executed on the map instance
-
- Args:
- method_name (str): The name of the map method to be executed.
- *args (any): The arguments to be passed to the map method.
- """
- # TODO: Pass as dict? {"name": method_name, "args": args}
- call = [method_name, args]
- self._message_queue.append(call)
-
- def add_control(
- self,
- control: Control,
- position: [str | ControlPosition] = ControlPosition.TOP_RIGHT,
- ) -> None:
- """Add a control to the map
-
- Args:
- control (Control): The control to be added to the map.
- position (str | ControlPosition): The position of the control.
- """
- self.add_call(
- "addControl",
- control.type,
- control.to_dict(),
- ControlPosition(position).value,
- )
+ def __init__(
+ self,
+ map_options: MapOptions = MapOptions(),
+ sources: dict = None,
+ layers: list = None,
+ controls: list = None,
+ **kwargs,
+ ):
+ self.map_options = (
+ map_options.to_dict() | kwargs
+ ) # MapOptions(**kwargs).to_dict() # need to fix MapWidget, because height is passed as kwarg
+ self._message_queue = []
+ self.add_layers(layers, sources)
+ if controls:
+ for control in controls:
+ self.add_control(control)
+
+ def __iter__(self):
+ for k, v in self.to_dict().items():
+ yield k, v
+
+ def to_dict(self) -> dict:
+ return {"mapOptions": self.map_options, "calls": self._message_queue}
+
+ """
+ @property
+ def sources(self) -> list:
+ return [item["data"] for item in self._calls if item["name"] == "addSource"]
- def add_source(self, id: str, source: [Source | dict]) -> None:
- """Add a source to the map
-
- Args:
- id (str): The unique ID of the source.
- source (Source | dict): The source to be added to the map.
- """
- if isinstance(source, Source):
- source = source.to_dict()
-
- self.add_call("addSource", id, source)
-
- def add_layer(self, layer: [Layer | dict], before_id: str = None) -> None:
- """Add a layer to the map
-
- Args:
- layer (Layer | dict): The Layer to be added to the map.
- before_id (str): The ID of an existing layer to insert the new layer before,
- resulting in the new layer appearing visually beneath the existing layer.
- If `None`, the new layer will appear above all other layers.
- """
- if isinstance(layer, Layer):
- layer = layer.to_dict()
+ @property
+ def layers(self) -> list:
+ return [item["data"] for item in self._calls if item["name"] == "addLayer"]
+ """
+
+ # TODO: Rename to add_map_call
+ """
+ def add_call_(self, func_name: str, params: list) -> None:
+ self._message_queue.append(
+ {"name": "applyFunc", "data": {"funcName": func_name, "params": params}}
+ )
+ """
+
+ def add_call(self, method_name: str, *args) -> None:
+ """Add a method call that is executed on the map instance
+
+ Args:
+ method_name (str): The name of the map method to be executed.
+ *args (any): The arguments to be passed to the map method.
+ """
+ # TODO: Pass as dict? {"name": method_name, "args": args}
+ call = [method_name, args]
+ self._message_queue.append(call)
- self.add_call("addLayer", layer, before_id)
-
- def add_marker(self, marker: Marker) -> None:
- """Add a marker to the map
-
- Args:
- marker (Marker): The marker to be added to the map.
- """
- self.add_call("addMarker", marker.to_dict())
-
- def add_popup(self, layer_id: str, prop: str = None, template: str = None) -> None:
- """Add a popup to the map
-
- Args:
- layer_id (str): The layer to which the popup is added.
- prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
- template (str): A mustache template. If supplied, `prop` is ignored.
- """
- self.add_call("addPopup", layer_id, prop, template)
-
- def add_tooltip(
- self, layer_id: str, prop: str = None, template: str = None
- ) -> None:
- """Add a tooltip to the map
-
- Args:
- layer_id (str): The layer to which the tooltip is added.
- prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
- template (str): A mustache template. If supplied, `prop` is ignored.
-
- Examples:
- >>> map = Map()
- >>> # ...
- >>> map.add_tooltip("test-layer", template="Name: {{ name }}")
- """
- self.add_call("addTooltip", layer_id, prop, template)
+ def add_control(
+ self,
+ control: Control,
+ position: [str | ControlPosition] = None,
+ ) -> None:
+ """Add a control to the map
+
+ Args:
+ control (Control): The control to be added to the map.
+ position (str | ControlPosition): The position of the control.
+ """
+ position = position or control.position
+ self.add_call(
+ "addControl",
+ control.type,
+ control.to_dict(),
+ ControlPosition(position).value,
+ )
+
+ def add_source(self, id: str, source: [Source | dict | GeoDataFrame]) -> None:
+ """Add a source to the map
+
+ Args:
+ id (str): The unique ID of the source.
+ source (Source | dict | GeoDataFrame): The source to be added to the map.
+ """
+ if GeoDataFrame is not None and isinstance(source, GeoDataFrame):
+ source = SimpleFeatures(source).to_source()
+
+ if isinstance(source, Source):
+ source = source.to_dict()
+
+ self.add_call("addSource", id, source)
+
+ def add_layer(self, layer: [Layer | dict], before_id: str = None) -> None:
+ """Add a layer to the map
- def set_filter(self, layer_id: str, filter_: list):
- """Update the filter of a layer
-
- Args:
- layer_id (str): The name of the layer to be updated.
- filter_ (list): The filter expression that is applied to the source of the layer.
- """
- self.add_call("setFilter", layer_id, filter_)
+ Args:
+ layer (Layer | dict): The Layer to be added to the map.
+ before_id (str): The ID of an existing layer to insert the new layer before,
+ resulting in the new layer appearing visually beneath the existing layer.
+ If `None`, the new layer will appear above all other layers.
+ """
+ if isinstance(layer, Layer):
+ layer = layer.to_dict()
- def set_paint_property(self, layer_id: str, prop: str, value: any) -> None:
- """Update the paint property of a layer
-
- Args:
- layer_id (str): The name of the layer to be updated.
- prop (str): The name of the paint property to be updated.
- value (any): The new value of the paint property.
- """
- self.add_call("setPaintProperty", layer_id, prop, value)
-
- def set_layout_property(self, layer_id: str, prop: str, value: any) -> None:
- """Update a layout property of a layer
-
- Args:
- layer_id (str): The name of the layer to be updated.
- prop (str): The name of the layout property to be updated.
- value (any): The new value of the layout property.
- """
- self.add_call("setLayoutProperty", layer_id, prop, value)
-
- def set_data(self, source_id: str, data: dict) -> None:
- """Update the data of a GeoJSON source
-
- Args:
- source_id (str): The name of the source to be updated.
- data (dict): The data of the source.
+ self.add_call("addLayer", layer, before_id)
+
+ 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)
+
+ for layer in layers:
+ self.add_layer(layer)
+
+ def add_marker(self, marker: Marker) -> None:
+ """Add a marker to the map
+
+ Args:
+ marker (Marker): The marker to be added to the map.
+ """
+ self.add_call("addMarker", marker.to_dict())
+
+ def add_popup(self, layer_id: str, prop: str = None, template: str = None) -> None:
+ """Add a popup to the map
+
+ Args:
+ layer_id (str): The layer to which the popup is added.
+ prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
+ template (str): A mustache template. If supplied, `prop` is ignored.
"""
- self.add_call("setSourceData", source_id, data)
+ self.add_call("addPopup", layer_id, prop, template)
- def set_visibility(self, layer_id: str, visible: bool = True) -> None:
- """Update the visibility of a layer
-
- Args:
- layer_id (str): The name of the layer to be updated.
- visible (bool): Whether the layer is visible or not.
- """
- value = "visible" if visible else "none"
- self.add_call("setLayoutProperty", layer_id, "visibility", value)
+ def add_tooltip(
+ self, layer_id: str, prop: str = None, template: str = None
+ ) -> None:
+ """Add a tooltip to the map
+
+ Args:
+ layer_id (str): The layer to which the tooltip is added.
+ prop (str): The property of the source to be displayed. If `None`, all properties are displayed.
+ template (str): A mustache template. If supplied, `prop` is ignored.
- def to_html(self, title: str = "My Awesome Map", **kwargs) -> str:
- """Render to html
-
- Args:
- title (str): The Title of the HTML document.
- **kwargs (Any): Additional keyword arguments that are passed to the template.
- Currently, `style` is the only supported keyword argument.
-
- Examples:
- >>> from maplibre import Map
-
- >>> map = Map()
- >>> with open("/tmp/map.html", "w") as f:
- ... f.write(map.to_html(style="height: 800px;") # doctest: +SKIP
- """
- js_lib = read_internal_file("srcjs", "pywidget.js")
- js_snippet = Template(js_template).render(data=json.dumps(self.to_dict()))
- css = read_internal_file("srcjs", "pywidget.css")
- headers = [f"<style>{css}</style>"]
-
- # Deck.GL headers
- add_deckgl_headers = "addDeckOverlay" in [
- item[0] for item in self._message_queue
- ]
- # TODO: Set version in constants
- deckgl_headers = (
- [
- '<script src="https://unpkg.com/deck.gl@9.0.16/dist.min.js"></script>',
- '<script src="https://unpkg.com/@deck.gl/json@9.0.16/dist.min.js"></script>',
- ]
- if add_deckgl_headers
- else []
- )
-
- # Mapbox Draw headers
- add_mapbox_draw_headers = "addMapboxDraw" in [
- item[0] for item in self._message_queue
- ]
- # TODO: Set version in constants
- mapbox_draw_headers = (
- [
- # "<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.3/mapbox-gl-draw.js'></script>",
- # "<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.3/mapbox-gl-draw.css' type='text/css' />",
- "<script src='https://www.unpkg.com/@mapbox/mapbox-gl-draw@1.4.3/dist/mapbox-gl-draw.js'></script>",
- "<link rel='stylesheet' href='https://www.unpkg.com/@mapbox/mapbox-gl-draw@1.4.3/dist/mapbox-gl-draw.css' type='text/css' />",
- ]
- if add_mapbox_draw_headers
- else []
- )
-
- output = Template(html_template).render(
- js="\n".join([js_lib, js_snippet]),
- title=title,
- headers=headers + deckgl_headers + mapbox_draw_headers,
- **kwargs,
- )
- return output
+ Examples:
+ >>> m = Map()
+ >>> # ...
+ >>> m.add_tooltip("test-layer", template="Name: {{ name }}")
+ """
+ self.add_call("addTooltip", layer_id, prop, template)
+
+ def set_filter(self, layer_id: str, filter_: list):
+ """Update the filter of a layer
+
+ Args:
+ layer_id (str): The name of the layer to be updated.
+ filter_ (list): The filter expression that is applied to the source of the layer.
+ """
+ self.add_call("setFilter", layer_id, filter_)
+
+ def set_paint_property(self, layer_id: str, prop: str, value: any) -> None:
+ """Update the paint property of a layer
+
+ Args:
+ layer_id (str): The name of the layer to be updated.
+ prop (str): The name of the paint property to be updated.
+ value (any): The new value of the paint property.
+ """
+ self.add_call("setPaintProperty", layer_id, prop, value)
+
+ def set_layout_property(self, layer_id: str, prop: str, value: any) -> None:
+ """Update a layout property of a layer
+
+ Args:
+ layer_id (str): The name of the layer to be updated.
+ prop (str): The name of the layout property to be updated.
+ value (any): The new value of the layout property.
+ """
+ self.add_call("setLayoutProperty", layer_id, prop, value)
+
+ def set_data(self, source_id: str, data: dict | GeoDataFrame) -> None:
+ """Update the data of a GeoJSON source
+
+ Args:
+ source_id (str): The name of the source to be updated.
+ data (dict): The data of the source.
+ """
+ if isinstance(data, GeoDataFrame):
+ data = SimpleFeatures(data).to_source().data
+
+ self.add_call("setSourceData", source_id, data)
+
+ def set_visibility(self, layer_id: str, visible: bool = True) -> None:
+ """Update the visibility of a layer
+
+ Args:
+ layer_id (str): The name of the layer to be updated.
+ visible (bool): Whether the layer is visible or not.
+ """
+ value = "visible" if visible else "none"
+ self.add_call("setLayoutProperty", layer_id, "visibility", value)
- # -------------------------
- # Plugins
- # -------------------------
- def add_deck_layers(self, layers: list[dict], tooltip: str | dict = None) -> None:
- """Add Deck.GL layers to the layer stack
-
- Args:
- layers (list[dict]): A list of dictionaries containing the Deck.GL layers to be added.
- tooltip (str | dict): Either a single mustache template string applied to all layers
- or a dictionary where keys are layer ids and values are mustache template strings.
- """
- self.add_call("addDeckOverlay", layers, tooltip)
-
- def set_deck_layers(self, layers: list[dict], tooltip: str | dict = None) -> None:
- """Update Deck.GL layers
-
- Args:
- layers (list[dict]): A list of dictionaries containing the Deck.GL layers to be updated.
- New layers will be added. Missing layers will be removed.
- tooltip (str | dict): Must be set to keep tooltip even if it did not change.
- """
- self.add_call("setDeckLayers", layers, tooltip)
-
- def add_mapbox_draw(
- self,
- options: dict | MapboxDrawOptions = None,
- position: str | ControlPosition = ControlPosition.TOP_LEFT,
- geojson: dict = None,
- ) -> None:
- """Add MapboxDraw controls to the map
-
- Note:
- See [MapboxDraw API Reference](https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md)
- for available options.
-
- Args:
- options (dict | MapboxDrawOptions): MapboxDraw options.
- position (str | ControlPosition): The position of the MapboxDraw controls.
- geojson (dict): A GeoJSON Feature, FeatureCollection or Geometry to be added to the draw layer.
- """
- if isinstance(options, MapboxDrawOptions):
- options = options.to_dict()
-
- self.add_call(
- "addMapboxDraw", options or {}, ControlPosition(position).value, geojson
- )
+ def fit_bounds(
+ self,
+ bounds: tuple | list = None,
+ data: GeoDataFrame = None,
+ animate=False,
+ **kwargs,
+ ) -> None:
+ """Pan and zoom the map to contain its visible area within the specified geographical bounds"""
+ kwargs["animate"] = animate
+ if data is not None:
+ bounds = tuple(data.total_bounds)
+
+ self.add_call("fitBounds", bounds, kwargs)
+
+ def to_html(self, title: str = "My Awesome Map", **kwargs) -> str:
+ """Render to html
+
+ Args:
+ title (str): The Title of the HTML document.
+ **kwargs (Any): Additional keyword arguments that are passed to the template.
+ Currently, `style` is the only supported keyword argument.
+
+ Examples:
+ >>> from maplibre import Map
+ >>> m = Map()
+ >>> with open("/tmp/map.html", "w") as f:
+ ... f.write(m.to_html(style="height: 800px;") # doctest: +SKIP
+ """
+ js_lib = read_internal_file("srcjs", "pywidget.js")
+ js_snippet = Template(js_template).render(data=json.dumps(self.to_dict()))
+ css = read_internal_file("srcjs", "pywidget.css")
+ headers = [f"<style>{css}</style>"]
+
+ # Deck.GL headers
+ add_deckgl_headers = "addDeckOverlay" in [
+ item[0] for item in self._message_queue
+ ]
+ # TODO: Set version in constants
+ deckgl_headers = (
+ [
+ # '<script src="https://unpkg.com/h3-js"></script>',
+ '<script src="https://unpkg.com/h3-js@4.1.0/dist/h3-js.umd.js"></script>',
+ '<script src="https://unpkg.com/deck.gl@9.0.16/dist.min.js"></script>',
+ '<script src="https://unpkg.com/@deck.gl/json@9.0.16/dist.min.js"></script>',
+ ]
+ if add_deckgl_headers
+ else []
+ )
+
+ # Mapbox Draw headers
+ add_mapbox_draw_headers = "addMapboxDraw" in [
+ item[0] for item in self._message_queue
+ ]
+ # TODO: Set version in constants
+ mapbox_draw_headers = (
+ [
+ # "<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.3/mapbox-gl-draw.js'></script>",
+ # "<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.3/mapbox-gl-draw.css' type='text/css' />",
+ "<script src='https://www.unpkg.com/@mapbox/mapbox-gl-draw@1.4.3/dist/mapbox-gl-draw.js'></script>",
+ "<link rel='stylesheet' href='https://www.unpkg.com/@mapbox/mapbox-gl-draw@1.4.3/dist/mapbox-gl-draw.css' type='text/css' />",
+ ]
+ if add_mapbox_draw_headers
+ else []
+ )
+
+ output = Template(html_template).render(
+ js="\n".join([js_lib, js_snippet]),
+ title=title,
+ headers=headers + deckgl_headers + mapbox_draw_headers,
+ **kwargs,
+ )
+ return output
+
+ def save(self, filename: str = None, preview=True, **kwargs):
+ """Save the map to an HTML file"""
+ return save_map(self, filename, preview, **kwargs)
+
+ # -------------------------
+ # Plugins
+ # -------------------------
+ def add_deck_layers(
+ self, layers: list[dict | "pydeck.Layer"], tooltip: str | dict = None
+ ) -> None:
+ """Add Deck.GL layers to the layer stack
+
+ Args:
+ layers (list[dict | "pydeck.Layer"]): A list of dictionaries containing the Deck.GL layers to be added.
+ tooltip (str | dict): Either a single mustache template string applied to all layers
+ or a dictionary where keys are layer ids and values are mustache template strings.
+ """
+ layers = parse_deck_layers(layers)
+ self.add_call("addDeckOverlay", layers, tooltip)
+
+ def set_deck_layers(
+ self, layers: list[dict | "pydeck.Layer"], tooltip: str | dict = None
+ ) -> None:
+ """Update Deck.GL layers
+
+ Args:
+ layers (list[dict | "pydeck.Layer"]): A list of dictionaries containing the Deck.GL layers to be updated.
+ New layers will be added. Missing layers will be removed.
+ tooltip (str | dict): Must be set to keep tooltip even if it did not change.
+ """
+ layers = parse_deck_layers(layers)
+ self.add_call("setDeckLayers", layers, tooltip)
+
+ def add_mapbox_draw(
+ self,
+ options: dict | MapboxDrawOptions = None,
+ position: str | ControlPosition = ControlPosition.TOP_LEFT,
+ geojson: dict = None,
+ ) -> None:
+ """Add MapboxDraw controls to the map
+
+ Note:
+ See [MapboxDraw API Reference](https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md)
+ for available options.
+
+ Args:
+ options (dict | MapboxDrawOptions): MapboxDraw options.
+ position (str | ControlPosition): The position of the MapboxDraw controls.
+ geojson (dict): A GeoJSON Feature, FeatureCollection or Geometry to be added to the draw layer.
+ """
+ if isinstance(options, MapboxDrawOptions):
+ options = options.to_dict()
+
+ self.add_call(
+ "addMapboxDraw", options or {}, ControlPosition(position).value, geojson
+ )
|
@@ -2281,25 +2474,25 @@
Source code in maplibre/map.py
- | def add_call(self, method_name: str, *args) -> None:
- """Add a method call that is executed on the map instance
-
- Args:
- method_name (str): The name of the map method to be executed.
- *args (any): The arguments to be passed to the map method.
- """
- # TODO: Pass as dict? {"name": method_name, "args": args}
- call = [method_name, args]
- self._message_queue.append(call)
+ | def add_call(self, method_name: str, *args) -> None:
+ """Add a method call that is executed on the map instance
+
+ Args:
+ method_name (str): The name of the map method to be executed.
+ *args (any): The arguments to be passed to the map method.
+ """
+ # TODO: Pass as dict? {"name": method_name, "args": args}
+ call = [method_name, args]
+ self._message_queue.append(call)
|
@@ -2312,7 +2505,7 @@
- add_control(control, position=ControlPosition.TOP_RIGHT)
+ add_control(control, position=None)
@@ -2359,7 +2552,7 @@
|
- TOP_RIGHT
+ None
|
@@ -2367,39 +2560,41 @@
Source code in maplibre/map.py
- | def add_control(
- self,
- control: Control,
- position: [str | ControlPosition] = ControlPosition.TOP_RIGHT,
-) -> None:
- """Add a control to the map
-
- Args:
- control (Control): The control to be added to the map.
- position (str | ControlPosition): The position of the control.
- """
- self.add_call(
- "addControl",
- control.type,
- control.to_dict(),
- ControlPosition(position).value,
- )
+ | def add_control(
+ self,
+ control: Control,
+ position: [str | ControlPosition] = None,
+) -> None:
+ """Add a control to the map
+
+ Args:
+ control (Control): The control to be added to the map.
+ position (str | ControlPosition): The position of the control.
+ """
+ position = position or control.position
+ self.add_call(
+ "addControl",
+ control.type,
+ control.to_dict(),
+ ControlPosition(position).value,
+ )
|
@@ -2437,7 +2632,7 @@
layers |
- list[dict]
+ list[dict | 'pydeck.Layer']
|
@@ -2468,23 +2663,29 @@
Source code in maplibre/map.py
- | def add_deck_layers(self, layers: list[dict], tooltip: str | dict = None) -> None:
- """Add Deck.GL layers to the layer stack
-
- Args:
- layers (list[dict]): A list of dictionaries containing the Deck.GL layers to be added.
- tooltip (str | dict): Either a single mustache template string applied to all layers
- or a dictionary where keys are layer ids and values are mustache template strings.
- """
- self.add_call("addDeckOverlay", layers, tooltip)
+ | def add_deck_layers(
+ self, layers: list[dict | "pydeck.Layer"], tooltip: str | dict = None
+) -> None:
+ """Add Deck.GL layers to the layer stack
+
+ Args:
+ layers (list[dict | "pydeck.Layer"]): A list of dictionaries containing the Deck.GL layers to be added.
+ tooltip (str | dict): Either a single mustache template string applied to all layers
+ or a dictionary where keys are layer ids and values are mustache template strings.
+ """
+ layers = parse_deck_layers(layers)
+ self.add_call("addDeckOverlay", layers, tooltip)
|
@@ -2554,31 +2755,31 @@
Source code in maplibre/map.py
- | def add_layer(self, layer: [Layer | dict], before_id: str = None) -> None:
- """Add a layer to the map
-
- Args:
- layer (Layer | dict): The Layer to be added to the map.
- before_id (str): The ID of an existing layer to insert the new layer before,
- resulting in the new layer appearing visually beneath the existing layer.
- If `None`, the new layer will appear above all other layers.
- """
- if isinstance(layer, Layer):
- layer = layer.to_dict()
-
- self.add_call("addLayer", layer, before_id)
+ | def add_layer(self, layer: [Layer | dict], before_id: str = None) -> None:
+ """Add a layer to the map
+
+ Args:
+ layer (Layer | dict): The Layer to be added to the map.
+ before_id (str): The ID of an existing layer to insert the new layer before,
+ resulting in the new layer appearing visually beneath the existing layer.
+ If `None`, the new layer will appear above all other layers.
+ """
+ if isinstance(layer, Layer):
+ layer = layer.to_dict()
+
+ self.add_call("addLayer", layer, before_id)
|
@@ -2665,51 +2866,51 @@
Source code in maplibre/map.py
- | def add_mapbox_draw(
- self,
- options: dict | MapboxDrawOptions = None,
- position: str | ControlPosition = ControlPosition.TOP_LEFT,
- geojson: dict = None,
-) -> None:
- """Add MapboxDraw controls to the map
-
- Note:
- See [MapboxDraw API Reference](https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md)
- for available options.
-
- Args:
- options (dict | MapboxDrawOptions): MapboxDraw options.
- position (str | ControlPosition): The position of the MapboxDraw controls.
- geojson (dict): A GeoJSON Feature, FeatureCollection or Geometry to be added to the draw layer.
- """
- if isinstance(options, MapboxDrawOptions):
- options = options.to_dict()
-
- self.add_call(
- "addMapboxDraw", options or {}, ControlPosition(position).value, geojson
- )
+ | def add_mapbox_draw(
+ self,
+ options: dict | MapboxDrawOptions = None,
+ position: str | ControlPosition = ControlPosition.TOP_LEFT,
+ geojson: dict = None,
+) -> None:
+ """Add MapboxDraw controls to the map
+
+ Note:
+ See [MapboxDraw API Reference](https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md)
+ for available options.
+
+ Args:
+ options (dict | MapboxDrawOptions): MapboxDraw options.
+ position (str | ControlPosition): The position of the MapboxDraw controls.
+ geojson (dict): A GeoJSON Feature, FeatureCollection or Geometry to be added to the draw layer.
+ """
+ if isinstance(options, MapboxDrawOptions):
+ options = options.to_dict()
+
+ self.add_call(
+ "addMapboxDraw", options or {}, ControlPosition(position).value, geojson
+ )
|
@@ -2763,19 +2964,19 @@
Source code in maplibre/map.py
- | def add_marker(self, marker: Marker) -> None:
- """Add a marker to the map
-
- Args:
- marker (Marker): The marker to be added to the map.
- """
- self.add_call("addMarker", marker.to_dict())
+ | def add_marker(self, marker: Marker) -> None:
+ """Add a marker to the map
+
+ Args:
+ marker (Marker): The marker to be added to the map.
+ """
+ self.add_call("addMarker", marker.to_dict())
|
@@ -2857,23 +3058,23 @@ |
|
|
|
| |
|
|
|
|
|
|
|
|
|