Skip to content

Commit

Permalink
Improve maplibre add_wms_layer
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Jul 25, 2024
1 parent cab3763 commit e7b24fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/maplibre/wms_source.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@
"source": [
"![](https://i.imgur.com/itFOq8z.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[-74.5447, 40.6892], zoom=8, style=\"streets\")\n",
"url = \"https://img.nj.gov/imagerywms/Natural2015\"\n",
"layers = \"Natural2015\"\n",
"m.add_wms_layer(url, layers=layers, before_id=\"aeroway_fill\")\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[-100.307965, 46.98692], zoom=13, pitch=45, style=\"3d-hybrid\")\n",
"url = \"https://fwspublicservices.wim.usgs.gov/wetlandsmapservice/services/Wetlands/MapServer/WMSServer\"\n",
"m.add_wms_layer(url, layers=\"1\", name=\"NWI\", opacity=0.6)\n",
"m.add_layer_control(bg_layers=True)\n",
"m.add_legend(builtin_legend=\"NWI\", title=\"Wetland Type\")\n",
"m"
]
}
],
"metadata": {
Expand Down
7 changes: 7 additions & 0 deletions leafmap/maplibregl.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ def add_tile_layer(
def add_wms_layer(
self,
url: str,
layers: str,
format: str = "image/png",
name: str = "WMS Layer",
attribution: str = "",
opacity: float = 1.0,
Expand All @@ -831,6 +833,8 @@ def add_wms_layer(
Args:
url (str): The URL of the tile layer.
layers (str): The layers to include in the WMS request.
format (str, optional): The format of the tiles in the layer.
name (str, optional): The name to use for the layer. Defaults to
'WMS Layer'.
attribution (str, optional): The attribution to use for the layer.
Expand All @@ -849,6 +853,9 @@ def add_wms_layer(
Returns:
None
"""

url = f"{url.strip()}?service=WMS&request=GetMap&layers={layers}&styles=&format={format.replace('/', '%2F')}&transparent=true&version=1.1.1&height=256&width=256&srs=EPSG%3A3857&bbox={{bbox-epsg-3857}}"

self.add_tile_layer(
url,
name=name,
Expand Down

0 comments on commit e7b24fb

Please sign in to comment.