Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/maplibre/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ Add an external Web Map Service raster layer to the map using addSource's tiles

[![](https://i.imgur.com/itFOq8z.png)](https://leafmap.org/maplibre/wms_source)

## Export GeoTIFF from WMS

Export a GeoTIFF image from a Web Map Service (WMS) within a specified bounding box and scale using the `wms_to_geotiff` function.

[![](https://github.com/user-attachments/assets/01c24828-4535-4352-8398-894955a6ca90)](https://leafmap.org/maplibre/wms_to_geotiff)

## Fit to the bounds of a LineString

Get the bounds of a LineString.
Expand Down
222 changes: 222 additions & 0 deletions docs/maplibre/wms_to_geotiff.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/wms_to_geotiff.ipynb)\n",
"[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
"\n",
"**Export GeoTIFF from WMS**\n",
"\n",
"This notebook demonstrates how to export a GeoTIFF image from a Web Map Service (WMS) within a specified bounding box and scale using the `wms_to_geotiff` function.\n",
"\n",
"Uncomment the following line to install [leafmap](https://leafmap.org) if needed.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install \"leafmap[maplibre]\" owslib rasterio"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import leafmap.common as common\n",
"import leafmap.maplibregl as leafmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Explore Available WMS Layers\n",
"\n",
"First, let's explore the available layers from the New Jersey Natural 2015 imagery WMS service.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = \"https://img.nj.gov/imagerywms/Natural2015\"\n",
"layers = common.get_wms_layers(url)\n",
"print(f\"Available layers: {layers}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize the WMS Layer on a Map\n",
"\n",
"Let's visualize the WMS layer on an interactive map to help identify the area of interest.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[-74.5447, 40.6892], zoom=10, style=\"positron\")\n",
"m.add_wms_layer(\n",
" url, layers=\"Natural2015\", name=\"NJ Natural 2015\", before_id=m.first_symbol_layer_id\n",
")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export GeoTIFF from WMS\n",
"\n",
"Now let's export a GeoTIFF from the WMS service. We'll specify:\n",
"- `url`: The WMS service URL\n",
"- `layers`: The layer(s) to request\n",
"- `bbox`: Bounding box as [minx, miny, maxx, maxy] in EPSG:4326\n",
"- `output`: The output file path\n",
"- `scale`: The resolution in degrees per pixel (for EPSG:4326)\n",
"\n",
"### Example 1: Export using scale parameter\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define bounding box [minx, miny, maxx, maxy] - an area in New Jersey\n",
"bbox = [-74.1834231, 40.6934163, -74.168899, 40.6996788]\n",
"\n",
"# Export GeoTIFF with scale (degrees per pixel)\n",
"output = \"nj_natural_2015_scale.tif\"\n",
"common.wms_to_geotiff(\n",
" url=url,\n",
" layers=\"Natural2015\",\n",
" bbox=bbox,\n",
" output=output,\n",
" scale=0.00001, # ~1 meters per pixel at this latitude\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 2: Export using explicit width and height\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Export GeoTIFF with explicit dimensions\n",
"output2 = \"nj_natural_2015_dims.tif\"\n",
"common.wms_to_geotiff(\n",
" url=url,\n",
" layers=\"Natural2015\",\n",
" bbox=bbox,\n",
" output=output2,\n",
" width=1452,\n",
" height=626,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize the Exported GeoTIFF\n",
"\n",
"Let's visualize the exported GeoTIFF on the map to verify the export.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m2 = leafmap.Map(style=\"positron\")\n",
"m2.add_raster(output, layer_name=\"Exported GeoTIFF\")\n",
"m2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export with Different CRS\n",
"\n",
"You can also export in different coordinate reference systems. Here's an example using EPSG:3857 (Web Mercator).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# You can provide bbox in EPSG:4326 and output in EPSG:3857\n",
"# The function will automatically transform the coordinates\n",
"output3 = \"nj_natural_2015_3857.tif\"\n",
"common.wms_to_geotiff(\n",
" url=url,\n",
" layers=\"Natural2015\",\n",
" bbox=bbox, # bbox in EPSG:4326\n",
" output=output3,\n",
" scale=1, # 1 meters per pixel for projected CRS\n",
" bbox_crs=\"EPSG:4326\", # Input bbox CRS (default)\n",
" output_crs=\"EPSG:3857\", # Output CRS\n",
" quiet=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m3 = leafmap.Map(style=\"positron\")\n",
"m3.add_raster(output3, layer_name=\"Exported GeoTIFF\")\n",
"m3"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "leafmap",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading