classMarker(BaseModel):
-"""Marker
-
- Attributes:
- lng_lat (tuple, list): **Required.** The longitude and latitude of the marker.
- popup (Popup, dict): A Popup that is displayed when a user clicks on the marker.
- options (MarkerOptions, dict): Marker options.
- """
-
- lng_lat:Union[tuple,list]=Field(None,serialization_alias="lngLat")
- popup:Union[Popup,dict]=None
- options:Union[MarkerOptions,dict]={}
-
classLayer(BaseModel):
-"""Layer properties
-
- Notes:
- See [layers](https://maplibre.org/maplibre-style-spec/layers/) for more details.
-
- 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 pymaplibregl.layer import Layer, LayerType
-
- >>> layer = Layer(id="test-layer", type=LayerType.CIRCLE, source="test-source")
- """
-
- id:str=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")
- defvalidate_source(cls,v):
- ifisinstance(v,Source):
- returnv.to_dict()
-
- returnv
-
- @field_validator("paint","layout")
- deffix_paint(cls,v):
- ifisinstance(v,dict):
- returnfix_keys(v)
-
- returnv
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- LayerType
-
-
-
-
-
-
-
- Bases: str, 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.
classLayerType(str,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"
-
classMap(object):
-"""Map
-
- Args:
- map_options (MapOptions): Map options.
- **kwargs: Keyword arguments that are appended to the `MapOptions` object.
-
- Examples:
- >>> from pymaplibregl.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._calls=[]
-
- def__iter__(self):
- fork,vinself.to_dict().items():
- yieldk,v
-
- defto_dict(self)->dict:
- return{"mapOptions":self._map_options,"calls":self._calls}
-
- @property
- defsources(self)->list:
- return[item["data"]foriteminself._callsifitem["name"]=="addSource"]
-
- @property
- deflayers(self)->list:
- return[item["data"]foriteminself._callsifitem["name"]=="addLayer"]
-
- # TODO: Rename to add_map_call
- defadd_call(self,func_name:str,params:list)->None:
- self._calls.append(
- {"name":"applyFunc","data":{"funcName":func_name,"params":params}}
- )
-
- defadd_control(
- self,
- control:Control,
- position:[str|ControlPosition]=ControlPosition.TOP_RIGHT,
- )->None:
-"""Add a control to the map"""
- data={
- "type":control.type,
- "options":control.to_dict(),
- "position":ControlPosition(position).value,
- }
- self._calls.append({"name":"addControl","data":data})
-
- defadd_source(self,id:str,source:[Source|dict])->None:
-"""Add a source to the map"""
- ifisinstance(source,Source):
- source=source.to_dict()
-
- self._calls.append({"name":"addSource","data":{"id":id,"source":source}})
-
- defadd_layer(self,layer:[Layer|dict])->None:
-"""Add a layer to the map
-
- Args:
- layer (Layer | dict): The Layer to be added to the map.
- """
- ifisinstance(layer,Layer):
- layer=layer.to_dict()
-
- self._calls.append({"name":"addLayer","data":layer})
-
- defadd_marker(self,marker:Marker)->None:
-"""Add a marker to the map"""
- self._calls.append({"name":"addMarker","data":marker.to_dict()})
-
- defadd_popup(self,layer_id:str,prop:str)->None:
-"""Add a popup to the map"""
- self._calls.append(
- {"name":"addPopup","data":{"layerId":layer_id,"property":prop}}
- )
-
- defset_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_])
-
- defset_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])
-
- defset_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])
-
- defto_html(self,output_dir:str=None,**kwargs)->str:
- js_lib=read_internal_file("srcjs","index.js")
- js_snippet=Template(js_template).render(data=json.dumps(self.to_dict()))
- output=Template(html_template).render(
- js="\n".join([js_lib,js_snippet]),**kwargs
- )
- ifoutput_dir=="skip":
- returnoutput
-
- file_name=os.path.join(get_output_dir(output_dir),"index.html")
- withopen(file_name,"w")asf:
- f.write(output)
-
- returnfile_name
-
defadd_layer(self,layer:[Layer|dict])->None:
-"""Add a layer to the map
-
- Args:
- layer (Layer | dict): The Layer to be added to the map.
- """
- ifisinstance(layer,Layer):
- layer=layer.to_dict()
-
- self._calls.append({"name":"addLayer","data":layer})
-
-
-
-
-
-
-
-
-
-
-
-
-
- add_marker(marker)
-
-
-
-
-
-
-
Add a marker to the map
-
-
- Source code in pymaplibregl/map.py
-
135
-136
-137
defadd_marker(self,marker:Marker)->None:
-"""Add a marker to the map"""
- self._calls.append({"name":"addMarker","data":marker.to_dict()})
-
-
-
-
-
-
-
-
-
-
-
-
-
- add_popup(layer_id,prop)
-
-
-
-
-
-
-
Add a popup to the map
-
-
- Source code in pymaplibregl/map.py
-
139
-140
-141
-142
-143
defadd_popup(self,layer_id:str,prop:str)->None:
-"""Add a popup to the map"""
- self._calls.append(
- {"name":"addPopup","data":{"layerId":layer_id,"property":prop}}
- )
-
-
-
-
-
-
-
-
-
-
-
-
-
- add_source(id,source)
-
-
-
-
-
-
-
Add a source to the map
-
-
- Source code in pymaplibregl/map.py
-
117
-118
-119
-120
-121
-122
defadd_source(self,id:str,source:[Source|dict])->None:
-"""Add a source to the map"""
- ifisinstance(source,Source):
- source=source.to_dict()
-
- self._calls.append({"name":"addSource","data":{"id":id,"source":source}})
-
-
-
-
-
-
-
-
-
-
-
-
-
- set_filter(layer_id,filter_)
-
-
-
-
-
-
-
Update the filter of a layer
-
-
-
-
Parameters:
-
-
- 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.
-
-
-
-
-
- Source code in pymaplibregl/map.py
-
145
-146
-147
-148
-149
-150
-151
-152
defset_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_])
-
-
-
-
-
-
-
-
-
-
-
-
-
- set_layout_property(layer_id,prop,value)
-
-
-
-
-
-
-
Update a layout property of a layer
-
-
-
-
Parameters:
-
-
- 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.
-
-
-
-
-
- Source code in pymaplibregl/map.py
-
164
-165
-166
-167
-168
-169
-170
-171
-172
defset_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])
-
-
-
-
-
-
-
-
-
-
-
-
-
- set_paint_property(layer_id,prop,value)
-
-
-
-
-
-
-
Update the paint property of a layer
-
-
-
-
Parameters:
-
-
- 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.
-
-
-
-
-
- Source code in pymaplibregl/map.py
-
154
-155
-156
-157
-158
-159
-160
-161
-162
defset_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])
-
"},{"location":"api/controls/","title":"Markers and controls","text":""},{"location":"api/controls/#pymaplibregl.controls","title":"pymaplibregl.controls","text":"
Markers and controls
See also https://docs.mapbox.com/mapbox-gl-js/api/markers/
Required. The longitude and latitude of the marker.
popup(Popup, dict)
A Popup that is displayed when a user clicks on the marker.
options(MarkerOptions, dict)
Marker options.
Source code in pymaplibregl/controls.py
class Marker(BaseModel):\n \"\"\"Marker\n\n Attributes:\n lng_lat (tuple, list): **Required.** The longitude and latitude of the marker.\n popup (Popup, dict): A Popup that is displayed when a user clicks on the marker.\n options (MarkerOptions, dict): Marker options.\n \"\"\"\n\n lng_lat: Union[tuple, list] = Field(None, serialization_alias=\"lngLat\")\n popup: Union[Popup, dict] = None\n options: Union[MarkerOptions, dict] = {}\n
class Popup(BaseModel):\n \"\"\"Popup\n\n Attributes:\n text: The Text of the popup.\n options (PopupOptions | dict): Popup options.\n \"\"\"\n\n text: str\n options: Union[PopupOptions, dict] = {}\n
class Layer(BaseModel):\n \"\"\"Layer properties\n\n Notes:\n See [layers](https://maplibre.org/maplibre-style-spec/layers/) for more details.\n\n Attributes:\n id (str): **Required.** The unique ID of the layer. Defaults to `str(uuid4())`.\n type (str | LayerType): **Required.** The type of the layer.\n filter (list): The filter expression that is applied to the source of the layer.\n layout (dict): The layout properties of the layer.\n max_zoom (int): The maximum zoom level for the layer.\n min_zoom (int): The minimum zoom level for the layer.\n paint (dict): The paint properties of the layer.\n source (str | Source): The name (unique ID) of a source or a source object to be used for the layer.\n source_layer (str): The layer to use from a vector tile source.\n\n Examples:\n >>> from pymaplibregl.layer import Layer, LayerType\n\n >>> layer = Layer(id=\"test-layer\", type=LayerType.CIRCLE, source=\"test-source\")\n \"\"\"\n\n id: str = str(uuid4())\n type: LayerType\n filter: list = None\n layout: dict = None\n max_zoom: int = Field(None, serialization_alias=\"maxzoom\")\n metadata: dict = None\n min_zoom: int = Field(None, serialization_alias=\"minzoom\")\n paint: dict = None\n source: Union[str, Source, dict, None] = None\n source_layer: str = Field(None, serialization_alias=\"source-layer\")\n\n @field_validator(\"source\")\n def validate_source(cls, v):\n if isinstance(v, Source):\n return v.to_dict()\n\n return v\n\n @field_validator(\"paint\", \"layout\")\n def fix_paint(cls, v):\n if isinstance(v, dict):\n return fix_keys(v)\n\n return v\n
A Client-side hillshading visualization based on DEM data.
BACKGROUND \u2013
A background color or pattern.
Source code in pymaplibregl/layer.py
class LayerType(Enum):\n \"\"\"Rendering type of layer\n\n Attributes:\n CIRCLE: A filled circle.\n FILL: A filled polygon with an optional stroked border.\n FILL_EXTRUSION: An extruded polygon.\n LINE: A stroked line.\n SYMBOL: An icon or a text label.\n RASTER: Raster map textures such as satellite imagery.\n HEATMAP: A heatmap.\n HILLSHADE: A Client-side hillshading visualization based on DEM data.\n BACKGROUND: A background color or pattern.\n \"\"\"\n\n CIRCLE = \"circle\"\n FILL = \"fill\"\n FILL_EXTRUSION = \"fill-extrusion\"\n LINE = \"line\"\n SYMBOL = \"symbol\"\n RASTER = \"raster\"\n HEATMAP = \"heatmap\"\n HILLSHADE = \"hillshade\"\n BACKGROUND = \"background\"\n
class Map(object):\n \"\"\"Map\n\n Args:\n map_options (MapOptions): Map options.\n **kwargs: Keyword arguments that are appended to the `MapOptions` object.\n\n Examples:\n >>> from pymaplibregl.map import Map, MapOptions\n\n >>> map_options = MapOptions(center=(9.5, 51.31667), zoom=8)\n >>> map = Map(map_options)\n >>> dict(map)\n {'mapOptions': {'center': (9.5, 51.31667), 'style': 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json', 'zoom': 8}, 'calls': []}\n \"\"\"\n\n MESSAGE = \"not implemented yet\"\n\n def __init__(self, map_options: MapOptions = MapOptions(), **kwargs):\n self._map_options = map_options.to_dict() | kwargs\n self._calls = []\n\n def __iter__(self):\n for k, v in self.to_dict().items():\n yield k, v\n\n def to_dict(self) -> dict:\n return {\"mapOptions\": self._map_options, \"calls\": self._calls}\n\n @property\n def sources(self) -> list:\n return [item[\"data\"] for item in self._calls if item[\"name\"] == \"addSource\"]\n\n @property\n def layers(self) -> list:\n return [item[\"data\"] for item in self._calls if item[\"name\"] == \"addLayer\"]\n\n # TODO: Rename to add_map_call\n def add_call(self, func_name: str, params: list) -> None:\n self._calls.append(\n {\"name\": \"applyFunc\", \"data\": {\"funcName\": func_name, \"params\": params}}\n )\n\n def add_control(\n self,\n control: Control,\n position: [str | ControlPosition] = ControlPosition.TOP_RIGHT,\n ) -> None:\n \"\"\"Add a control to the map\n\n Args:\n control (Control): The control to be add to the map.\n position (str | ControlPosition): The position of the control.\n \"\"\"\n data = {\n \"type\": control.type,\n \"options\": control.to_dict(),\n \"position\": ControlPosition(position).value,\n }\n self._calls.append({\"name\": \"addControl\", \"data\": data})\n\n def add_source(self, id: str, source: [Source | dict]) -> None:\n \"\"\"Add a source to the map\"\"\"\n if isinstance(source, Source):\n source = source.to_dict()\n\n self._calls.append({\"name\": \"addSource\", \"data\": {\"id\": id, \"source\": source}})\n\n def add_layer(self, layer: [Layer | dict]) -> None:\n \"\"\"Add a layer to the map\n\n Args:\n layer (Layer | dict): The Layer to be added to the map.\n \"\"\"\n if isinstance(layer, Layer):\n layer = layer.to_dict()\n\n self._calls.append({\"name\": \"addLayer\", \"data\": layer})\n\n def add_marker(self, marker: Marker) -> None:\n \"\"\"Add a marker to the map\n\n Args:\n marker (Marker): The marker to be added to the map.\n \"\"\"\n self._calls.append({\"name\": \"addMarker\", \"data\": marker.to_dict()})\n\n def add_popup(self, layer_id: str, prop: str) -> None:\n \"\"\"Add a popup to the map\"\"\"\n self._calls.append(\n {\"name\": \"addPopup\", \"data\": {\"layerId\": layer_id, \"property\": prop}}\n )\n\n def set_filter(self, layer_id: str, filter_: list):\n \"\"\"Update the filter of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n filter_ (list): The filter expression that is applied to the source of the layer.\n \"\"\"\n self.add_call(\"setFilter\", [layer_id, filter_])\n\n def set_paint_property(self, layer_id: str, prop: str, value: any) -> None:\n \"\"\"Update the paint property of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n prop (str): The name of the paint property to be updated.\n value (any): The new value of the paint property.\n \"\"\"\n self.add_call(\"setPaintProperty\", [layer_id, prop, value])\n\n def set_layout_property(self, layer_id: str, prop: str, value: any) -> None:\n \"\"\"Update a layout property of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n prop (str): The name of the layout property to be updated.\n value (any): The new value of the layout property.\n \"\"\"\n self.add_call(\"setLayoutProperty\", [layer_id, prop, value])\n\n def to_html(self, **kwargs) -> str:\n \"\"\"Render to html\n\n Args:\n **kwargs (Any): Additional keyword arguments that are passed to the template.\n Currently, `style` is the only supported keyword argument.\n\n Examples:\n >>> from pymaplibregl import Map\n\n >>> map = Map()\n >>> with open(\"/tmp/map.html\", \"w\") as f:\n ... f.write(map.to_html(style=\"height: 800px;\")\n \"\"\"\n js_lib = read_internal_file(\"srcjs\", \"index.js\")\n js_snippet = Template(js_template).render(data=json.dumps(self.to_dict()))\n output = Template(html_template).render(\n js=\"\\n\".join([js_lib, js_snippet]), **kwargs\n )\n return output\n
def add_control(\n self,\n control: Control,\n position: [str | ControlPosition] = ControlPosition.TOP_RIGHT,\n) -> None:\n \"\"\"Add a control to the map\n\n Args:\n control (Control): The control to be add to the map.\n position (str | ControlPosition): The position of the control.\n \"\"\"\n data = {\n \"type\": control.type,\n \"options\": control.to_dict(),\n \"position\": ControlPosition(position).value,\n }\n self._calls.append({\"name\": \"addControl\", \"data\": data})\n
def add_layer(self, layer: [Layer | dict]) -> None:\n \"\"\"Add a layer to the map\n\n Args:\n layer (Layer | dict): The Layer to be added to the map.\n \"\"\"\n if isinstance(layer, Layer):\n layer = layer.to_dict()\n\n self._calls.append({\"name\": \"addLayer\", \"data\": layer})\n
def add_marker(self, marker: Marker) -> None:\n \"\"\"Add a marker to the map\n\n Args:\n marker (Marker): The marker to be added to the map.\n \"\"\"\n self._calls.append({\"name\": \"addMarker\", \"data\": marker.to_dict()})\n
The filter expression that is applied to the source of the layer.
required Source code in pymaplibregl/map.py
def set_filter(self, layer_id: str, filter_: list):\n \"\"\"Update the filter of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n filter_ (list): The filter expression that is applied to the source of the layer.\n \"\"\"\n self.add_call(\"setFilter\", [layer_id, filter_])\n
def set_layout_property(self, layer_id: str, prop: str, value: any) -> None:\n \"\"\"Update a layout property of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n prop (str): The name of the layout property to be updated.\n value (any): The new value of the layout property.\n \"\"\"\n self.add_call(\"setLayoutProperty\", [layer_id, prop, value])\n
def set_paint_property(self, layer_id: str, prop: str, value: any) -> None:\n \"\"\"Update the paint property of a layer\n\n Args:\n layer_id (str): The name of the layer to be updated.\n prop (str): The name of the paint property to be updated.\n value (any): The new value of the paint property.\n \"\"\"\n self.add_call(\"setPaintProperty\", [layer_id, prop, value])\n