// A map tile cache: which tiles hold a place, which to fetch to keep a city for offline use, and where to fetch them.
//
//   quillo run map_tiles.qwe      then open http://localhost:8080/tiles

use "map_tiles.qwer"

// The Brandenburg Gate, as in OpenStreetMap's own example of tile numbers.
let GATE = { lat: 52.51628011262304, lon: 13.37771496361961 }

route GET /tiles {
    let tile = map_tile_at(GATE.lat, GATE.lon, 17)?
    {
        gate: tile,
        bounds: map_tile_bounds(17, tile.x, tile.y)?,
        pixel: map_tile_pixel(GATE.lat, GATE.lon, 17, 256)?,
        bing: map_tile_from_quadkey("213")?,
        osm: map_tile_url("https://tile.openstreetmap.org/{z}/{x}/{y}.png", 17, tile.x, tile.y, [])?,
        tms: map_tile_url("https://{s}.tiles.example/{z}/{x}/{-y}.png", 3, 4, 2, ["a", "b", "c"])?,
        family: map_tile_family(1, 1, 0)?,
    }
}

// The tiles to keep central Berlin offline, zooms 10 to 13.
route GET /offline {
    let tiles = map_tiles_cover([13.30, 52.48, 13.45, 52.55], 10, 13, 500)?
    { count: len(tiles), first: first(tiles), too_many: map_tiles_cover([-10.0, 35.0, 30.0, 60.0], 0, 12, 1000).error }
}
