Skip to content

Instantly share code, notes, and snippets.

@nzwildcode
Created April 29, 2023 01:16
Show Gist options
  • Save nzwildcode/199ffd5821341e94931b49da2ab8cffd to your computer and use it in GitHub Desktop.
Save nzwildcode/199ffd5821341e94931b49da2ab8cffd to your computer and use it in GitHub Desktop.
bardtest
import os
import requests
from flask import Flask, render_template, send_file
import sqlite3
import io
app = Flask(__name__)
@app.route("/")
def index():
# Create a connection to the MBTiles database.
with sqlite3.connect("nztopomap-nz-full.mbtiles") as connection:
# Get the list of zoom levels.
zoom_levels = connection.execute("SELECT zoom_level FROM zoom_levels").fetchall()
# Create a list of tiles for each zoom level.
tiles = []
for zoom_level in zoom_levels:
tiles.append(connection.execute("SELECT tile_data FROM tiles WHERE zoom_level = ?", (zoom_level,)).fetchall())
# Return a template with the tiles.
return render_template("index.html", tiles=tiles)
@app.route("/tiles/<zoom_level>/<x>/<y>.png")
def get_tile(zoom_level, x, y):
# Create a connection to the MBTiles database.
with sqlite3.connect("nztopomap-nz-full.mbtiles") as connection:
# Get the tile data.
tile_data = connection.execute("SELECT tile_data FROM tiles WHERE zoom_level = ? AND x = ? AND y = ?", (zoom_level, x, y)).fetchone()[0]
# Return the tile data as a PNG file.
return send_file(io.BytesIO(tile_data), mimetype="image/png")
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment