Package: datasette-tiles
datasette-tilesDatasette plugin for serving MBTiles map tilesInstallationInstall this plugin in the same environment as Datasette.$ datasette install datasette-tilesDemoYou can try this plugin out athttps://datasette-tiles-demo.datasette.io/-/tilesUsageThis plugin scans all database files connected to Datasette to see if any of them are valid MBTiles databases.It can then serve tiles from those databases at the following URL:/-/tiles/db-name/zoom/x/y.pngAn example map for each database demonstrating the configured minimum and maximum zoom for that database can be found at/-/tiles/db-name- this can also be accessed via the table and database action menus for that database.Visit/-/tilesfor an index page of attached valid databases.You can install thedatasette-basemapplugin to get abasemapdefault set of tiles, handling zoom levels 0 to 6 using OpenStreetMap.Tile coordinate systemsThere are two tile coordinate systems in common use for online maps. The first is used by OpenStreetMap and Google Maps, the second is from a specification calledTile Map Service, or TMS.Both systems use three components:z/x/y- wherezis the zoom level,xis the column andyis the row.The difference is in the way theyvalue is counted. OpenStreetMap has y=0 at the top. TMS has y=0 at the bottom.An illustrative example: at zoom level 2 the map is divided into 16 total tiles. The OpenStreetMap scheme numbers them like so:0/0  1/0  2/0  3/0
0/1  1/1  2/1  3/1
0/2  1/2  2/2  3/2
0/3  1/3  2/3  3/3The TMS scheme looks like this:0/3  1/3  2/3  3/3
0/2  1/2  2/2  3/2
0/1  1/1  2/1  3/1
0/0  1/0  2/0  3/0datasette-tilescan serve tiles using either of these standards. For the OpenStreetMap / Google Maps 0-at-the-top system, use the following URL:/-/tiles/database-name/{z}/{x}/{y}.pngFor the TMS 0-at-the-bottom system, use this:/-/tiles-tms/database-name/{z}/{x}/{y}.pngConfiguring a Leaflet tile layerThe following JavaScript will configure aLeaflet TileLayerfor use with this plugin:vartiles=leaflet.tileLayer("/-/tiles/basemap/{z}/{x}/{y}.png",{minZoom:0,maxZoom:6,attribution:"\u00a9 OpenStreetMap contributors"});Tile stacksdatasette-tilescan be configured to serve tiles from multiple attached MBTiles files, searching each database in order for a tile and falling back to the next in line if that tile is not found.For a demo of this in action, visithttps://datasette-tiles-demo.datasette.io/-/tiles-stackand zoom in on Japan. It should start showingStamen's Toner mapof Japan once you get to zoom level 6 and 7.The/-/tiles-stack/{z}/{x}/{y}.pngendpoint provides this feature.If you start Datasette like this:datasette world.mbtiles country.mbtiles city1.mbtiles city2.mbtilesAny requests for a tile from the/-/tiles-stackpath will first check thecity2database, thancity1, thencountry, thenworld.If you have thedatasette-basemapplugin installed it will be given special treatment: thebasemapdatabase will always be the last database checked for a tile.Rather than rely on the order in which databases were attached, you can instead configure an explicit order using thetiles-stack-orderplugin setting. Add the following to yourmetadata.jsonfile:{"plugins":{"datasette-tiles":{"tiles-stack-order":["world","country"]}}}You can then run Datasette like this:datasette -m metadata.json country.mbtiles world.mbtilesThis endpoint serves tiles using the OpenStreetMap / Google Maps coordinate system. To load tiles using the TMS coordinate system use this endpoint instead:/-/tiles-stack-tms/{z}/{x}/{y}.pngDevelopmentTo set up this plugin locally, first checkout the code. Then create a new virtual environment:cd datasette-tiles
python3 -mvenv venv
source venv/bin/activateOr if you are usingpipenv:pipenv shellNow install the dependencies and tests:pip install -e '.[test]'To run the tests:pytest