All files / src/utils basemap.ts

100% Statements 20/20
90% Branches 9/10
100% Functions 6/6
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                    1x         1x     41x     1x               1x                                                                                                                                               1x               8x   7x 6x 6x 2x   4x     3x 3x   8x         5x         2x 2x                            
/**
 * The self-hosted basemap (docs/architecture/MAPS.md): OpenStreetMap data built
 * by Protomaps into one PMTiles file, on S3 behind CloudFront
 * (`VITE_MAPS_URL`). Only this file and MapView.vue know the map library and
 * the basemap, so another provider can replace them.
 */
import type { StyleSpecification } from 'maplibre-gl'
import { LIGHT, language_script_pairs, layers } from '@protomaps/basemaps'
import type { Flavor } from '@protomaps/basemaps'
 
export const MAPS_URL = (
  import.meta.env.VITE_MAPS_URL || 'https://maps.flows.browsway.com'
).replace(/\/$/, '')
 
/** The source name the Protomaps layers read */
const SOURCE = 'protomaps'
 
/** Map labels: the languages Protomaps names places in (others fall back to English) */
const LABEL_LANGUAGES = new Set(language_script_pairs.map((pair) => pair.lang))
 
export const ATTRIBUTION =
  '<a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">© OpenStreetMap</a>' +
  ' · <a href="https://protomaps.com" target="_blank" rel="noopener">Protomaps</a>'
 
/**
 * The light map's colors. Protomaps' own light flavor is a grey canvas made
 * for data overlays; readers expect the colors of everyday maps (cream land,
 * green parks and woods, blue water, yellow highways, white streets).
 */
const CLASSIC: Flavor = {
  ...LIGHT,
  background: '#f5f3ee',
  earth: '#f5f3ee',
  park_a: '#d4ecc8',
  park_b: '#bfe3ae',
  wood_a: '#cfe8c0',
  wood_b: '#b4dba0',
  scrub_a: '#dcedcb',
  scrub_b: '#c8e4b2',
  zoo: '#cfe8c8',
  hospital: '#f6e1e1',
  school: '#f3ecd9',
  industrial: '#ebe6ef',
  pedestrian: '#f0ede6',
  military: '#ece6e0',
  aerodrome: '#e6e8ec',
  runway: '#d4d8de',
  sand: '#f4ecd0',
  beach: '#f8efc8',
  glacier: '#ffffff',
  water: '#aad3f2',
  pier: '#ffffff',
  buildings: '#e5e1da',
  other: '#ffffff',
  minor_service: '#ffffff',
  minor_a: '#ffffff',
  minor_b: '#ffffff',
  link: '#ffffff',
  major: '#ffffff',
  highway: '#fbd77a',
  minor_service_casing: '#dfdad0',
  minor_casing: '#dfdad0',
  link_casing: '#d6d0c4',
  major_casing_early: '#d6d0c4',
  major_casing_late: '#d6d0c4',
  highway_casing_early: '#e3ae3c',
  highway_casing_late: '#e3ae3c',
  tunnel_other: '#eeeeee',
  tunnel_minor: '#eeeeee',
  tunnel_link: '#eeeeee',
  tunnel_major: '#eeeeee',
  tunnel_highway: '#fbe5ad',
  bridges_other: '#ffffff',
  bridges_minor: '#ffffff',
  bridges_link: '#ffffff',
  bridges_major: '#ffffff',
  bridges_highway: '#fbd77a',
  bridges_highway_casing: '#e3ae3c',
  railway: '#9aa3a8',
  boundaries: '#a99cb8',
  ocean_label: '#4a7fb5',
  city_label: '#3c3c3c',
  city_label_halo: '#ffffff',
  subplace_label: '#6f6f6f',
  subplace_label_halo: '#ffffff',
  state_label: '#8a8a8a',
  state_label_halo: '#ffffff',
  country_label: '#6b6b6b',
  roads_label_minor: '#6f6a66',
  roads_label_major: '#5f5a56',
  landcover: {
    grassland: '#dcefd2',
    barren: '#f6efdc',
    urban_area: '#ece9e3',
    farmland: '#eaf2d8',
    glacier: '#ffffff',
    scrub: '#e2efcf',
    forest: '#c4e3b4'
  }
}
 
let currentBasemap: Promise<string> | null = null
 
/**
 * The URL of the basemap file to read, named by `current.json` (an update
 * writes a new file, then points to it). Read once per page load; a failure
 * is not kept, so a later map tries again.
 */
export function basemapUrl(): Promise<string> {
  currentBasemap ??= fetch(`${MAPS_URL}/current.json`)
    .then(async (response) => {
      if (!response.ok) throw new Error(`current.json answered ${response.status}`)
      const { basemap } = (await response.json()) as { basemap?: unknown }
      if (typeof basemap !== 'string' || !/^[\w./-]+\.pmtiles$/.test(basemap)) {
        throw new Error('current.json names no basemap')
      }
      return `${MAPS_URL}/${basemap}`
    })
    .catch((error: unknown) => {
      currentBasemap = null
      throw error
    })
  return currentBasemap
}
 
/** Forgets the basemap read (tests) */
export function resetBasemap(): void {
  currentBasemap = null
}
 
/** The map's style: the Protomaps layers in the reader's language, always light (a dark map reads poorly) */
export function basemapStyle(fileUrl: string, lang: string): StyleSpecification {
  const labels = LABEL_LANGUAGES.has(lang) ? lang : 'en'
  return {
    version: 8,
    glyphs: `${MAPS_URL}/fonts/{fontstack}/{range}.pbf`,
    sprite: `${MAPS_URL}/sprites/v4/light`,
    sources: {
      [SOURCE]: {
        type: 'vector',
        url: `pmtiles://${fileUrl}`,
        attribution: ATTRIBUTION
      }
    },
    layers: layers(SOURCE, CLASSIC, { lang: labels }) as StyleSpecification['layers']
  }
}