14. Localtileserver#

Open In Colab

14.1. Introduction#

localtileserver is a Python package that enables efficient visualization of large raster datasets, such as GeoTIFFs, using tiled web maps. It can be used in Jupyter notebooks with ipyleaflet or folium, from the command line in a web browser, and with remote Cloud Optimized GeoTIFFs (COGs).

14.1.1. Learning Objectives#

By the end of this lecture, you will:

  • Understand what localtileserver is and its use cases.

  • Learn how to serve raster data as map tiles.

  • Explore integration with folium and ipyleaflet.

  • Customize and optimize raster visualization.

14.2. 1. Installing and Importing localtileserver#

To install localtileserver, run:

pip install localtileserver

Then, import the required modules:

from localtileserver import (
    TileClient,
    get_leaflet_tile_layer,
    get_folium_tile_layer,
    examples,
)

14.3. 2. Serving a Local Raster File#

To visualize a GeoTIFF, use the TileClient:

client = TileClient("path/to/geo.tif")
print(client.url)  # Prints the local tile service URL

Alternatively, you can use example data:

client = examples.get_san_francisco()
client

14.4. 3. Retrieving Tiles and Thumbnails#

To retrieve a single tile (z, x, y):

client.tile(10, 163, 395)

To generate a thumbnail preview:

client.thumbnail()

14.5. 4. Integrating with ipyleaflet#

To display raster data on an ipyleaflet map:

from ipyleaflet import Map

client = examples.get_elevation()
tile_layer = get_leaflet_tile_layer(
    client, indexes=1, vmin=-5000, vmax=5000, opacity=0.65
)

m = Map(zoom=3)
m.add(tile_layer)
m

14.6. 5. Integrating with Folium#

To display raster data on a folium map:

from folium import Map

client = examples.get_oam2()
tile_layer = get_folium_tile_layer(client)

m = Map(location=client.center(), zoom_start=16)
m.add_child(tile_layer)
m

14.7. 6. Customizing Tile Rendering#

You can apply colormaps and adjust transparency:

client = TileClient("/path/to/your/raster.tif", colormap="viridis")

Supported colormaps include any matplotlib colormap.

14.8. 7. Serving Remote Raster Data#

To serve Cloud Optimized GeoTIFFs (COGs):

client = TileClient("https://example.com/path/to/cog.tif")

14.9. 8. Stopping the Server#

Once finished, stop the tile server to free up resources:

client.shutdown()

14.10. Summary#

  • localtileserver serves raster data as interactive tile layers.

  • It integrates seamlessly with folium and ipyleaflet.

  • Supports local and remote raster files, including COGs.

  • Allows customization with colormaps and resampling methods.

14.11. Further Reading#


14.11.1. Assignment#

  1. Install localtileserver and serve a local raster file.

  2. Visualize the raster data in both folium and ipyleaflet.

  3. Apply a colormap to customize the visualization.

  4. Submit your Jupyter Notebook with the implementation.

14.11.2. Feedback#

If you encounter issues or have feature requests, please open a discussion or issue on the localtileserver GitHub page.

For debugging, generate a system report:

import localtileserver

print(localtileserver.Report())

Happy Mapping! 🌍