Wms to geotiff

Export GeoTIFF from WMS
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.
Uncomment the following line to install leafmap if needed.
| # %pip install "leafmap[maplibre]" owslib rasterio
|
| import leafmap.common as common
import leafmap.maplibregl as leafmap
|
Explore Available WMS Layers
First, let's explore the available layers from the New Jersey Natural 2015 imagery WMS service.
| url = "https://img.nj.gov/imagerywms/Natural2015"
layers = common.get_wms_layers(url)
print(f"Available layers: {layers}")
|
Visualize the WMS Layer on a Map
Let's visualize the WMS layer on an interactive map to help identify the area of interest.
| m = leafmap.Map(center=[-74.5447, 40.6892], zoom=10, style="positron")
m.add_wms_layer(
url, layers="Natural2015", name="NJ Natural 2015", before_id=m.first_symbol_layer_id
)
m
|
Export GeoTIFF from WMS
Now let's export a GeoTIFF from the WMS service. We'll specify:
- url: The WMS service URL
- layers: The layer(s) to request
- bbox: Bounding box as [minx, miny, maxx, maxy] in EPSG:4326
- output: The output file path
- scale: The resolution in degrees per pixel (for EPSG:4326)
Example 1: Export using scale parameter
1
2
3
4
5
6
7
8
9
10
11
12 | # Define bounding box [minx, miny, maxx, maxy] - an area in New Jersey
bbox = [-74.1834231, 40.6934163, -74.168899, 40.6996788]
# Export GeoTIFF with scale (degrees per pixel)
output = "nj_natural_2015_scale.tif"
common.wms_to_geotiff(
url=url,
layers="Natural2015",
bbox=bbox,
output=output,
scale=0.00001, # ~1 meters per pixel at this latitude
)
|
Example 2: Export using explicit width and height
| # Export GeoTIFF with explicit dimensions
output2 = "nj_natural_2015_dims.tif"
common.wms_to_geotiff(
url=url,
layers="Natural2015",
bbox=bbox,
output=output2,
width=1452,
height=626,
)
|
Visualize the Exported GeoTIFF
Let's visualize the exported GeoTIFF on the map to verify the export.
| m2 = leafmap.Map(style="positron")
m2.add_raster(output, layer_name="Exported GeoTIFF")
m2
|
Export with Different CRS
You can also export in different coordinate reference systems. Here's an example using EPSG:3857 (Web Mercator).
1
2
3
4
5
6
7
8
9
10
11
12
13 | # You can provide bbox in EPSG:4326 and output in EPSG:3857
# The function will automatically transform the coordinates
output3 = "nj_natural_2015_3857.tif"
common.wms_to_geotiff(
url=url,
layers="Natural2015",
bbox=bbox, # bbox in EPSG:4326
output=output3,
scale=1, # 1 meters per pixel for projected CRS
bbox_crs="EPSG:4326", # Input bbox CRS (default)
output_crs="EPSG:3857", # Output CRS
quiet=True,
)
|
| m3 = leafmap.Map(style="positron")
m3.add_raster(output3, layer_name="Exported GeoTIFF")
m3
|