|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "[](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/wms_to_geotiff.ipynb)\n", |
| 8 | + "[](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n", |
| 9 | + "\n", |
| 10 | + "**Export GeoTIFF from WMS**\n", |
| 11 | + "\n", |
| 12 | + "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", |
| 13 | + "\n", |
| 14 | + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed.\n" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "# %pip install \"leafmap[maplibre]\" owslib rasterio" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "import leafmap.common as common\n", |
| 33 | + "import leafmap.maplibregl as leafmap" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "## Explore Available WMS Layers\n", |
| 41 | + "\n", |
| 42 | + "First, let's explore the available layers from the New Jersey Natural 2015 imagery WMS service.\n" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "url = \"https://img.nj.gov/imagerywms/Natural2015\"\n", |
| 52 | + "layers = common.get_wms_layers(url)\n", |
| 53 | + "print(f\"Available layers: {layers}\")" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "## Visualize the WMS Layer on a Map\n", |
| 61 | + "\n", |
| 62 | + "Let's visualize the WMS layer on an interactive map to help identify the area of interest.\n" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "m = leafmap.Map(center=[-74.5447, 40.6892], zoom=10, style=\"positron\")\n", |
| 72 | + "m.add_wms_layer(\n", |
| 73 | + " url, layers=\"Natural2015\", name=\"NJ Natural 2015\", before_id=m.first_symbol_layer_id\n", |
| 74 | + ")\n", |
| 75 | + "m" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "metadata": {}, |
| 81 | + "source": [ |
| 82 | + "## Export GeoTIFF from WMS\n", |
| 83 | + "\n", |
| 84 | + "Now let's export a GeoTIFF from the WMS service. We'll specify:\n", |
| 85 | + "- `url`: The WMS service URL\n", |
| 86 | + "- `layers`: The layer(s) to request\n", |
| 87 | + "- `bbox`: Bounding box as [minx, miny, maxx, maxy] in EPSG:4326\n", |
| 88 | + "- `output`: The output file path\n", |
| 89 | + "- `scale`: The resolution in degrees per pixel (for EPSG:4326)\n", |
| 90 | + "\n", |
| 91 | + "### Example 1: Export using scale parameter\n" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "# Define bounding box [minx, miny, maxx, maxy] - an area in New Jersey\n", |
| 101 | + "bbox = [-74.1834231, 40.6934163, -74.168899, 40.6996788]\n", |
| 102 | + "\n", |
| 103 | + "# Export GeoTIFF with scale (degrees per pixel)\n", |
| 104 | + "output = \"nj_natural_2015_scale.tif\"\n", |
| 105 | + "common.wms_to_geotiff(\n", |
| 106 | + " url=url,\n", |
| 107 | + " layers=\"Natural2015\",\n", |
| 108 | + " bbox=bbox,\n", |
| 109 | + " output=output,\n", |
| 110 | + " scale=0.00001, # ~1 meters per pixel at this latitude\n", |
| 111 | + ")" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "### Example 2: Export using explicit width and height\n" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "# Export GeoTIFF with explicit dimensions\n", |
| 128 | + "output2 = \"nj_natural_2015_dims.tif\"\n", |
| 129 | + "common.wms_to_geotiff(\n", |
| 130 | + " url=url,\n", |
| 131 | + " layers=\"Natural2015\",\n", |
| 132 | + " bbox=bbox,\n", |
| 133 | + " output=output2,\n", |
| 134 | + " width=1452,\n", |
| 135 | + " height=626,\n", |
| 136 | + ")" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "## Visualize the Exported GeoTIFF\n", |
| 144 | + "\n", |
| 145 | + "Let's visualize the exported GeoTIFF on the map to verify the export.\n" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "m2 = leafmap.Map(style=\"positron\")\n", |
| 155 | + "m2.add_raster(output, layer_name=\"Exported GeoTIFF\")\n", |
| 156 | + "m2" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "markdown", |
| 161 | + "metadata": {}, |
| 162 | + "source": [ |
| 163 | + "## Export with Different CRS\n", |
| 164 | + "\n", |
| 165 | + "You can also export in different coordinate reference systems. Here's an example using EPSG:3857 (Web Mercator).\n" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": null, |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [], |
| 173 | + "source": [ |
| 174 | + "# You can provide bbox in EPSG:4326 and output in EPSG:3857\n", |
| 175 | + "# The function will automatically transform the coordinates\n", |
| 176 | + "output3 = \"nj_natural_2015_3857.tif\"\n", |
| 177 | + "common.wms_to_geotiff(\n", |
| 178 | + " url=url,\n", |
| 179 | + " layers=\"Natural2015\",\n", |
| 180 | + " bbox=bbox, # bbox in EPSG:4326\n", |
| 181 | + " output=output3,\n", |
| 182 | + " scale=1, # 1 meters per pixel for projected CRS\n", |
| 183 | + " bbox_crs=\"EPSG:4326\", # Input bbox CRS (default)\n", |
| 184 | + " output_crs=\"EPSG:3857\", # Output CRS\n", |
| 185 | + " quiet=True,\n", |
| 186 | + ")" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": null, |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [], |
| 194 | + "source": [ |
| 195 | + "m3 = leafmap.Map(style=\"positron\")\n", |
| 196 | + "m3.add_raster(output3, layer_name=\"Exported GeoTIFF\")\n", |
| 197 | + "m3" |
| 198 | + ] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "kernelspec": { |
| 203 | + "display_name": "leafmap", |
| 204 | + "language": "python", |
| 205 | + "name": "python3" |
| 206 | + }, |
| 207 | + "language_info": { |
| 208 | + "codemirror_mode": { |
| 209 | + "name": "ipython", |
| 210 | + "version": 3 |
| 211 | + }, |
| 212 | + "file_extension": ".py", |
| 213 | + "mimetype": "text/x-python", |
| 214 | + "name": "python", |
| 215 | + "nbconvert_exporter": "python", |
| 216 | + "pygments_lexer": "ipython3", |
| 217 | + "version": "3.12.12" |
| 218 | + } |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 2 |
| 222 | +} |
0 commit comments