{"spec_id":"map-tile-background","library":"makie","language":"julia","code":"# anyplot.ai\n# map-tile-background: Map with Tile Background\n# Library: makie 0.24.16 | Julia 1.13.4\n# Quality: 87/100 | Updated: 2026-11-27\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Downloads\nusing PNGFiles\n\nRandom.seed!(42)\n\n# Theme tokens\nconst THEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n# Imprint sequential ramp for marks (colored points on the page)\nconst ANYPLOT_SEQ = THEME == \"light\" ?\n    cgrad([colorant\"#009E73\", colorant\"#024582\", colorant\"#351E45\"], [0.0, 0.687, 1.0]) :\n    cgrad([colorant\"#6F7E20\", colorant\"#009E73\", colorant\"#CDD9FF\"], [0.0, 0.21, 1.0])\n\n# OpenStreetMap tiles; the dark theme inverts their lightness (keyless dark\n# basemaps now serve watermarked tiles)\nconst TILE_URL    = \"https://tile.openstreetmap.org/{z}/{x}/{y}.png\"\nconst TILE_CREDIT = \"© OpenStreetMap contributors\"\n\n# City data: major tourist destinations with annual visitor counts (millions)\nconst city_names = [\n    \"Paris\", \"London\", \"New York\", \"Tokyo\", \"Barcelona\",\n    \"Rome\", \"Dubai\", \"Istanbul\", \"Amsterdam\", \"Singapore\",\n    \"Sydney\", \"Bangkok\", \"Mumbai\", \"Berlin\", \"Prague\",\n    \"Vienna\", \"Budapest\", \"Lisbon\", \"Copenhagen\", \"Stockholm\",\n]\nconst lons = Float64[\n    2.35, -0.12, -74.0, 139.7, 2.17,\n    12.5, 55.3, 28.98, 4.9, 103.8,\n    151.2, 100.5, 72.88, 13.41, 14.42,\n    16.37, 19.04, -9.14, 12.57, 18.07,\n]\nconst lats = Float64[\n    48.85, 51.51, 40.71, 35.68, 41.39,\n    41.9, 25.2, 41.01, 52.37, 1.35,\n    -33.87, 13.75, 19.08, 52.52, 50.08,\n    48.21, 47.5, 38.72, 55.68, 59.33,\n]\nconst visitors = Float64[\n    34.0, 31.0, 13.6, 12.9, 9.0,\n    9.7, 16.7, 14.0, 8.6, 19.1,\n    3.8, 22.8, 7.0, 5.3, 4.4,\n    6.8, 4.4, 4.1, 2.7, 2.9,\n]\n\n# Convert latitudes to Mercator Y for correct Web Mercator projection\ny_merc       = log.(tan.(lats .* π ./ 180) .+ 1 ./ cos.(lats .* π ./ 180))\nv_min        = minimum(visitors)\nv_max        = maximum(visitors)\nv_norm       = (visitors .- v_min) ./ (v_max - v_min)\nmarker_sizes = 10.0 .+ 28.0 .* v_norm\n\n# Padded axis extent (12 % padding in each direction)\nlon_pad      = (maximum(lons)   - minimum(lons))   * 0.12\nmerc_pad     = (maximum(y_merc) - minimum(y_merc)) * 0.12\nax_lon_min   = minimum(lons)   - lon_pad\nax_lon_max   = maximum(lons)   + lon_pad\nax_merc_min  = minimum(y_merc) - merc_pad\nax_merc_max  = maximum(y_merc) + merc_pad\npad_lat_north = atan(sinh(ax_merc_max)) * 180 / π\npad_lat_south = atan(sinh(ax_merc_min)) * 180 / π\n\n# Tile grid parameters\nconst ZOOM    = 3\nconst TILE_PX = 256\n\nn_zoom = 1 << ZOOM   # tiles per axis (EPSG:3857 slippy-map convention)\ntx_min = clamp(floor(Int, (ax_lon_min + 180) / 360 * n_zoom), 0, n_zoom - 1)\ntx_max = clamp(floor(Int, (ax_lon_max + 180) / 360 * n_zoom), 0, n_zoom - 1)\n# north edge → smaller tile y index, south edge → larger\nty_min = clamp(floor(Int, (π - log(tan(pad_lat_north * π / 180) + 1 / cos(pad_lat_north * π / 180))) / (2π) * n_zoom), 0, n_zoom - 1)\nty_max = clamp(floor(Int, (π - log(tan(pad_lat_south * π / 180) + 1 / cos(pad_lat_south * π / 180))) / (2π) * n_zoom), 0, n_zoom - 1)\n\nn_tx = tx_max - tx_min + 1\nn_ty = ty_max - ty_min + 1\n\n# Stitched raster: [pixel_x, pixel_y] where x = west→east, y = north→south\nstitched = fill(RGBA{Float32}(0.87f0, 0.87f0, 0.87f0, 1.0f0), TILE_PX * n_tx, TILE_PX * n_ty)\n\nmktempdir() do tmpdir\n    for iy in 0:(n_ty - 1), ix in 0:(n_tx - 1)\n        tx  = tx_min + ix\n        ty  = ty_min + iy\n        url = replace(TILE_URL,\n            \"{z}\" => string(ZOOM), \"{x}\" => string(tx), \"{y}\" => string(ty))\n        fpath = joinpath(tmpdir, \"$(tx)_$(ty).png\")\n        try\n            Downloads.download(url, fpath;\n                headers = [\"User-Agent\" =>\n                    \"anyplot.ai static visualization; contact@anyplot.ai\"])\n            tile_mat = PNGFiles.load(fpath)   # size = (height, width) = [row, col] = [y, x]\n            # Transpose to [x, y] layout expected by the stitched array\n            tile_rgba = RGBA{Float32}.(tile_mat)\n            if THEME == \"dark\"   # invert lightness into dark greys\n                lum = 0.299f0 .* red.(tile_rgba) .+ 0.587f0 .* green.(tile_rgba) .+ 0.114f0 .* blue.(tile_rgba)\n                g = 0.07f0 .+ 0.30f0 .* (1f0 .- lum)\n                tile_rgba = RGBA{Float32}.(g, g, g .* 1.04f0, 1f0)\n            end\n            tile_t = permutedims(tile_rgba, (2, 1))\n            stitched[ix*TILE_PX+1:(ix+1)*TILE_PX, iy*TILE_PX+1:(iy+1)*TILE_PX] = tile_t\n        catch e\n            @warn \"Tile ($tx, $ty) unavailable\" exception = e\n        end\n    end\nend\n\n# Geographic bounds of the full stitched raster\nimg_lon_west   = tx_min / n_zoom * 360 - 180\nimg_lon_east   = (tx_max + 1) / n_zoom * 360 - 180\nimg_merc_north = π * (1 - 2 * ty_min / n_zoom)\nimg_merc_south = π * (1 - 2 * (ty_max + 1) / n_zoom)\n\n# Flip y so index 1 maps to the south (Makie image! convention: y increases upward)\nimg_makie = reverse(stitched, dims = 2)\n\n# Latitude tick marks in Mercator Y with degree labels\nlat_ticks_deg = filter(l -> pad_lat_south < l < pad_lat_north, [-30.0, 0.0, 30.0, 60.0])\nlat_tick_merc = log.(tan.(lat_ticks_deg .* π ./ 180) .+ 1 ./ cos.(lat_ticks_deg .* π ./ 180))\nlat_tick_strs = [l >= 0 ? \"$(round(Int, l))°N\" : \"$(round(Int, -l))°S\" for l in lat_ticks_deg]\n\nlon_ticks_deg = filter(l -> ax_lon_min < l < ax_lon_max, [-60.0, 0.0, 60.0, 120.0])\nlon_tick_strs = [l >= 0 ? \"$(round(Int, l))°E\" : \"$(round(Int, -l))°W\" for l in lon_ticks_deg]\n\n# Figure\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = \"map-tile-background · julia · makie · anyplot.ai\",\n    titlesize          = 20,\n    titlecolor         = INK,\n    xlabel             = \"Longitude\",\n    ylabel             = \"Latitude\",\n    xlabelsize         = 14,\n    ylabelsize         = 14,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelsize     = 12,\n    yticklabelsize     = 12,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridvisible       = false,\n    ygridvisible       = false,\n    xticks             = (lon_ticks_deg, lon_tick_strs),\n    yticks             = (lat_tick_merc, lat_tick_strs),\n    limits             = (ax_lon_min, ax_lon_max, ax_merc_min, ax_merc_max),\n)\n\n# Tile background layer\nimage!(ax, (img_lon_west, img_lon_east), (img_merc_south, img_merc_north), img_makie)\n\n# Data layer: scatter by visitor count (size + color)\nscatter!(ax, lons, y_merc;\n    color       = v_norm,\n    colormap    = ANYPLOT_SEQ,\n    markersize  = marker_sizes,\n    strokewidth = 1.5,\n    strokecolor = PAGE_BG,\n)\n\n# Labels for top 8 most-visited cities\ntop8 = sortperm(visitors, rev = true)[1:8]\nfor i in top8\n    text!(ax, lons[i], y_merc[i];\n        text     = city_names[i],\n        fontsize = 11,\n        color    = INK,\n        # offset clears the marker edge; London goes west (clear of Amsterdam), Bangkok drops\n        # below-right so it does not overprint the tile's own \"Bangkok\" label\n        offset   = city_names[i] == \"London\" ? (-(marker_sizes[i] / 2 + 4), 5) :\n                   city_names[i] == \"Bangkok\" ? (marker_sizes[i] / 2 + 4, -4) : (marker_sizes[i] / 2 + 4, 5),\n        align    = (city_names[i] == \"London\" ? :right : :left, city_names[i] == \"Bangkok\" ? :top : :bottom),\n    )\nend\n\n# Tile provider attribution (bottom-right)\ntext!(ax, ax_lon_max, ax_merc_min;\n    text     = TILE_CREDIT,\n    fontsize = 10,\n    color    = INK_SOFT,\n    align    = (:right, :bottom),\n    offset   = (-4, 4),\n)\n\nColorbar(\n    fig[1, 2];\n    colormap       = ANYPLOT_SEQ,\n    limits         = (v_min, v_max),\n    label          = \"Annual Visitors (millions)\",\n    labelcolor     = INK,\n    tickcolor      = INK_SOFT,\n    ticklabelcolor = INK_SOFT,\n    width          = 20,\n)\n\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}