Introduction to TileMatrixSets¶
The goal of this notebook is to give a quick introduction to the new TMS option in rio-tiler.
Requirements¶
To be able to run this notebook you'll need the following requirements:
- rio-tiler~= 5.0
- ipyleaflet
In [1]:
Copied!
# !pip install rio-tiler
# !pip install ipyleaflet
# !pip install rio-tiler
# !pip install ipyleaflet
In [2]:
Copied!
import morecantile
from ipyleaflet import (
Map,
basemaps,
basemap_to_tiles,
TileLayer,
WMSLayer,
GeoJSON,
projections
)
import morecantile
from ipyleaflet import (
Map,
basemaps,
basemap_to_tiles,
TileLayer,
WMSLayer,
GeoJSON,
projections
)
In [3]:
Copied!
# For this DEMO we will use this file
src_path = "https://njogis-imagery.s3.amazonaws.com/2020/cog/I7D16.tif"
# For this DEMO we will use this file
src_path = "https://njogis-imagery.s3.amazonaws.com/2020/cog/I7D16.tif"
Tile Server¶
For this demo, we need to create a minimal tile server.
In [4]:
Copied!
import json
from concurrent import futures
from tornado import web
from tornado import gen
from tornado.httpserver import HTTPServer
from tornado.concurrent import run_on_executor
from rio_tiler.io import Reader
from rio_tiler.errors import TileOutsideBounds
from rio_tiler.profiles import img_profiles
class TileServer:
def __init__(self, src_path):
"""Initialize Tornado app."""
self.server = None
self.app = web.Application([
(r"^/tiles/(\w+)/(\d+)/(\d+)/(\d+)", TileHandler, {"url": src_path}),
])
def start(self):
"""Start tile server."""
self.server = HTTPServer(self.app)
self.server.listen(8080)
def stop(self):
"""Stop tile server."""
if self.server:
self.server.stop()
class TileHandler(web.RequestHandler):
"""Tile requests handler."""
executor = futures.ThreadPoolExecutor(max_workers=16)
def initialize(self, url):
"""Initialize tiles handler."""
self.url = url
@run_on_executor
def _get_tile(self, tms, z, x, y):
try:
with Reader(self.url, tms=morecantile.tms.get(tms)) as cog:
img = cog.tile(x, y, z, indexes=(1,2,3))
except TileOutsideBounds:
raise web.HTTPError(404)
prof = img_profiles.get("PNG", {})
return img.render(img_format="PNG", **prof)
@gen.coroutine
def get(self, tms, z, x, y):
"""Retunrs tile data and header."""
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Methods", "GET")
self.set_header("Content-Type", "image/png")
self.set_header("Cache-Control", "no-store, no-cache, must-revalidate")
res = yield self._get_tile(tms, int(z), int(x), int(y))
self.write(res)
ts = TileServer(src_path)
ts.start()
import json
from concurrent import futures
from tornado import web
from tornado import gen
from tornado.httpserver import HTTPServer
from tornado.concurrent import run_on_executor
from rio_tiler.io import Reader
from rio_tiler.errors import TileOutsideBounds
from rio_tiler.profiles import img_profiles
class TileServer:
def __init__(self, src_path):
"""Initialize Tornado app."""
self.server = None
self.app = web.Application([
(r"^/tiles/(\w+)/(\d+)/(\d+)/(\d+)", TileHandler, {"url": src_path}),
])
def start(self):
"""Start tile server."""
self.server = HTTPServer(self.app)
self.server.listen(8080)
def stop(self):
"""Stop tile server."""
if self.server:
self.server.stop()
class TileHandler(web.RequestHandler):
"""Tile requests handler."""
executor = futures.ThreadPoolExecutor(max_workers=16)
def initialize(self, url):
"""Initialize tiles handler."""
self.url = url
@run_on_executor
def _get_tile(self, tms, z, x, y):
try:
with Reader(self.url, tms=morecantile.tms.get(tms)) as cog:
img = cog.tile(x, y, z, indexes=(1,2,3))
except TileOutsideBounds:
raise web.HTTPError(404)
prof = img_profiles.get("PNG", {})
return img.render(img_format="PNG", **prof)
@gen.coroutine
def get(self, tms, z, x, y):
"""Retunrs tile data and header."""
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Methods", "GET")
self.set_header("Content-Type", "image/png")
self.set_header("Cache-Control", "no-store, no-cache, must-revalidate")
res = yield self._get_tile(tms, int(z), int(x), int(y))
self.write(res)
ts = TileServer(src_path)
ts.start()
List the supported TMS from morecantile
In [11]:
Copied!
print("Supported TMS:")
for name in morecantile.tms.list():
print("-", name)
print("Supported TMS:")
for name in morecantile.tms.list():
print("-", name)
Supported TMS: - CanadianNAD83_LCC - EuropeanETRS89_LAEAQuad - LINZAntarticaMapTilegrid - NZTM2000Quad - UPSAntarcticWGS84Quad - UPSArcticWGS84Quad - UTM31WGS84Quad - WGS1984Quad - WebMercatorQuad - WorldCRS84Quad - WorldMercatorWGS84Quad
WebMercator TMS¶
In [12]:
Copied!
with Reader(src_path) as cog:
info = cog.info()
print(info.model_dump(exclude_none=True))
bounds = info.bounds
center = ((bounds[1] + bounds[3]) / 2, (bounds[0] + bounds[2]) / 2)
m = Map(center=center, zoom=info.minzoom, basemap={})
layer = TileLayer(
url="http://127.0.0.1:8080/tiles/WebMercatorQuad/{z}/{x}/{y}",
min_zoom=info.minzoom,
max_zoom=info.maxzoom,
opacity=1,
)
m.add_layer(layer)
m
with Reader(src_path) as cog:
info = cog.info()
print(info.model_dump(exclude_none=True))
bounds = info.bounds
center = ((bounds[1] + bounds[3]) / 2, (bounds[0] + bounds[2]) / 2)
m = Map(center=center, zoom=info.minzoom, basemap={})
layer = TileLayer(
url="http://127.0.0.1:8080/tiles/WebMercatorQuad/{z}/{x}/{y}",
min_zoom=info.minzoom,
max_zoom=info.maxzoom,
opacity=1,
)
m.add_layer(layer)
m
{'bounds': BoundingBox(left=-74.3095632062702, bottom=40.603994417539994, right=-74.29151245384847, top=40.61775082944064), 'minzoom': 14, 'maxzoom': 19, 'band_metadata': [('b1', {}), ('b2', {}), ('b3', {}), ('b4', {})], 'band_descriptions': [('b1', ''), ('b2', ''), ('b3', ''), ('b4', '')], 'dtype': 'uint16', 'nodata_type': 'None', 'colorinterp': ['red', 'green', 'blue', 'undefined'], 'overviews': [2, 4, 8, 16], 'count': 4, 'driver': 'GTiff', 'width': 5000, 'height': 5000}
Out[12]:
Map(center=[40.610872623490316, -74.30053783005934], controls=(ZoomControl(options=['position', 'zoom_in_text'…
WGS84 TMS¶
In [13]:
Copied!
with Reader(src_path, tms=morecantile.tms.get("WorldCRS84Quad")) as cog:
info = cog.info()
print(info.model_dump(exclude_none=True))
bounds = info.bounds
center = ((bounds[1] + bounds[3]) / 2, (bounds[0] + bounds[2]) / 2)
m = Map(center=center, zoom=info.minzoom, basemap={}, crs=projections.EPSG4326)
layer = TileLayer(
url="http://127.0.0.1:8080/tiles/WorldCRS84Quad/{z}/{x}/{y}",
min_zoom=info.minzoom,
max_zoom=info.maxzoom,
opacity=1,
)
m.add_layer(layer)
m
with Reader(src_path, tms=morecantile.tms.get("WorldCRS84Quad")) as cog:
info = cog.info()
print(info.model_dump(exclude_none=True))
bounds = info.bounds
center = ((bounds[1] + bounds[3]) / 2, (bounds[0] + bounds[2]) / 2)
m = Map(center=center, zoom=info.minzoom, basemap={}, crs=projections.EPSG4326)
layer = TileLayer(
url="http://127.0.0.1:8080/tiles/WorldCRS84Quad/{z}/{x}/{y}",
min_zoom=info.minzoom,
max_zoom=info.maxzoom,
opacity=1,
)
m.add_layer(layer)
m
{'bounds': BoundingBox(left=-74.3095632062702, bottom=40.603994417539994, right=-74.29151245384847, top=40.61775082944064), 'minzoom': 13, 'maxzoom': 18, 'band_metadata': [('b1', {}), ('b2', {}), ('b3', {}), ('b4', {})], 'band_descriptions': [('b1', ''), ('b2', ''), ('b3', ''), ('b4', '')], 'dtype': 'uint16', 'nodata_type': 'None', 'colorinterp': ['red', 'green', 'blue', 'undefined'], 'overviews': [2, 4, 8, 16], 'count': 4, 'driver': 'GTiff', 'width': 5000, 'height': 5000}
Out[13]:
Map(center=[40.610872623490316, -74.30053783005934], controls=(ZoomControl(options=['position', 'zoom_in_text'…
WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4810/2246 (127.0.0.1) 162.19ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4809/2247 (127.0.0.1) 156.98ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4811/2247 (127.0.0.1) 157.02ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4811/2246 (127.0.0.1) 69.55ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4809/2246 (127.0.0.1) 79.31ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4809/2248 (127.0.0.1) 101.73ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4811/2248 (127.0.0.1) 73.75ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4808/2247 (127.0.0.1) 108.00ms WARNING:tornado.access:404 GET /tiles/WorldCRS84Quad/13/4812/2247 (127.0.0.1) 81.00ms
In [14]:
Copied!
ts.stop()
ts.stop()
In [ ]:
Copied!