Skip to main content

🌐 Local Tile Server for Geospatial Rasters

codecov PyPI pypi-activity

Need to visualize a rather large (gigabytes) raster you have locally? This is for you.

A Flask application for serving tiles from large raster files in the Slippy Maps standard (i.e., /zoom/x/y.png)

tile-diagram

🌟 Highlights

  • Create a local tile server for large geospatial images
  • View local raster files with ipyleaflet
  • Extract regions of interest (ROIs) interactively
  • Use the example datasets to generate Digital Elevation Models
  • Visualize rasters with the included CesiumJS web viewer

ℹ️ Overview

Under the hood, this uses large_image to launch a tile server in a background thread which will serve raster imagery to a tile viewer (see ipyleaflet examples below). This tile server can efficiently deliver varying levels of detail of your raster imagery to your viewer; it helps to have pre-tiled, Cloud Optimized GeoTIFFs (COG), but no wories if not as large_image will tile and cache for you when opening the raster.

There is an included, standalone web viewer leveraging CesiumJS and GeoJS. You can use the web viewer to select and extract regions of interest from rasters.

Disclaimer: I put this together over a weekend and I'm definitely going to change a few things moving forward to make it more stable/robust. This means that things will most likely break between minor releases (I use the major.minor.patch versioning scheme).

⬇️ Installation

Install from PyPI: https://pypi.org/project/localtileserver/

pip install localtileserver

📝 A Brief Note on Installing GDAL

GDAL can be a pain in the 🍑 to install, and you may want to handle GDAL before installing localtileserver.

If on linux, I highly recommend using the large_image_wheels from Kitware.

pip install --find-links=https://girder.github.io/large_image_wheels --no-cache GDAL

Otherwise, I recommend using conda:

conda install -c conda-forge GDAL

💭 Feedback

Please share your thoughts and questions on the Discussions board. If you would like to report any bugs or make feature requests, please open an issue.

If filing a bug report, please share a scooby Report:

import localtileserver
print(localtileserver.Report())

🚀 Usage

🍃 ipyleaflet Tile Layers

The TileClient class is a nifty tool to launch a tile server as a background thread to serve image tiles from any raster file on your local file system. Additionally, it can be used in conjunction with the get_leaflet_tile_layer utility to create an ipyleaflet.TileLayer for interactive visualization in a Jupyter notebook. Here is an example:

from localtileserver import get_leaflet_tile_layer, TileClient
from ipyleaflet import Map

# First, create a tile server from local raster file
tile_client = TileClient('~/Desktop/TC_NG_SFBay_US_Geo.tif')

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client)

# Create ipyleaflet map, add tile layer, and display
m = Map(center=tile_client.center())
m.add_layer(t)
m

ipyleaflet

🥓 Two Rasters at Once

from localtileserver import get_leaflet_tile_layer
from ipyleaflet import Map, ScaleControl, FullScreenControl, SplitMapControl

# Create 2 tile layers from 2 separate raster files
l = get_leaflet_tile_layer('~/Desktop/TC_NG_SFBay_US_Geo.tif',
                           band=1, palette='matplotlib.Viridis_20', vmin=50, vmax=200)
r = get_leaflet_tile_layer('~/Desktop/small.tif',
                           band=2, palette='matplotlib.Plasma_6', vmin=0, vmax=150)

# Make the ipyleaflet map
m = Map(center=(37.7249511580583, -122.27230466902257), zoom=9)
control = SplitMapControl(left_layer=l, right_layer=r)
m.add_control(control)
m.add_control(ScaleControl(position='bottomleft'))
m.add_control(FullScreenControl())
m

ipyleaflet-double

🎯 Using ipyleaflet for ROI Extraction

I have included the get_leaflet_roi_controls utility to create some leaflet UI controls for extracting regions of interest from a tile client. You can use it as follows and then draw a polygon and click the "Extract ROI" button.

The outputs are save in your working directory by default (next to the Jupyter notebook).

from localtileserver import get_leaflet_tile_layer, get_leaflet_roi_controls
from localtileserver import TileClient
from ipyleaflet import Map

# First, create a tile server from local raster file
tile_client = TileClient('~/Desktop/TC_NG_SFBay_US_Geo.tif')

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client)

# Create ipyleaflet controls to extract an ROI
draw_control, roi_control = get_leaflet_roi_controls(tile_client)

# Create ipyleaflet map, add layers, add controls, and display
m = Map(center=(37.7249511580583, -122.27230466902257), zoom=9)
m.add_layer(t)
m.add_control(draw_control)
m.add_control(roi_control)
m

ipyleaflet-draw-roi

🗺️ Example Datasets

A few example datasets are included with localtileserver. A particularly useful one has global elevation data which you can use to create high resolution Digital Elevation Models (DEMs) of a local region.

from localtileserver import get_leaflet_tile_layer, get_leaflet_roi_controls, examples
from ipyleaflet import Map

# Load example tile layer from publicly available DEM source
tile_client = examples.get_elevation()

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client,
                           band=1, vmin=-500, vmax=5000,
                           palette='matplotlib.Plasma_6',
                           opacity=0.75)

# Create ipyleaflet controls to extract an ROI
draw_control, roi_control = get_leaflet_roi_controls(tile_client)

m = Map(zoom=2)
m.add_layer(t)
m.add_control(draw_control)
m.add_control(roi_control)
m

elevation

Then you can follow the same routine as described above to extract an ROI.

I zoomed in over Golden, Colorado and drew a polygon of the extent of the DEM I would like to create:

golden

And perform the extraction:

roi_path = '...'  # Look in your working directory

r = get_leaflet_tile_layer(roi_path, band=1,
                           palette='matplotlib.Plasma_6', opacity=0.75)

m2 = Map(
        center=(39.763427033262175, -105.20614908076823),
        zoom=12,
       )
m2.add_layer(r)
m2

golden-dem

Here is another example with the Virtual Earth satellite imagery

from localtileserver import get_leaflet_tile_layer, examples
from ipyleaflet import Map

# Load example tile layer from publicly available imagery
tile_client = examples.get_virtual_earth()

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client, opacity=1)

m = Map(center=(39.751343612695145, -105.22181306125279), zoom=18)
m.add_layer(t)
m

kafadar

🖥️ Local Web Application

Launch the tileserver from the commandline to use the included web application where you can view the raster and extract regions of interest.

python -m localtileserver path/to/raster.tif

cesium-viewer

You can use the web viewer to extract regions of interest:

webviewer-roi

You can also launch the web viewer with any of the available example datasets:

python -m localtileserver dem

Available choices are:

  • dem or elevation: global elevation dataset
  • blue_marble: Blue Marble satellite imagery
  • virtual_earth: Microsoft's satellite/aerial imagery
  • arcgis: ArcGIS World Street Map
  • bahamas: Sample raster over the Bahamas

Usage Notes

  • get_leaflet_tile_layer accepts either an existing TileClient or a path from which to create a TileClient under the hood.
  • The color palette choices come from palettable.

Release files for localtileserver 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for localtileserver 0.3.1
File Size Uploaded
localtileserver-0.3.1.tar.gz 862.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for localtileserver 0.3.1
File Interpreter ABI Platform
localtileserver-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size:1.7 MB

Release files / localtileserver-0.3.1.tar.gz

Download URL localtileserver-0.3.1.tar.gz
Size 862.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f01b1962458278fc36502adc53d7db067f19fe68a31c793614f6946439ec4c70
BLAKE2b-256 checksum
How to use checksums
cf3de5743d05095522140a3936a438bc62dbdf7729b15ca88c03a16ac52e272c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / localtileserver-0.3.1-py3-none-any.whl

Download URL localtileserver-0.3.1-py3-none-any.whl
Size 867.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76377d7218cd93c8c42c32da302be466b0f761b256ae953975f3310f4d4d1ab0
BLAKE2b-256 checksum
How to use checksums
ff491f5981db4c1dda51c9f1011d4a94ebffd58096b5e689b4b688fb1ad24f72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

1.0.0

2 release files

0.11.0

2 release files

0.10.6

2 release files

0.10.5

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.11

2 release files

0.5.10

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.14

2 release files

0.3.13

2 release files

0.3.12

2 release files

0.3.11

2 release files

0.3.10

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page