All files / map overlay.ts

100% Statements 27/27
100% Branches 8/8
100% Functions 15/15
100% Lines 27/27

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                                                                          3x     3x 28x 28x                   3x     3x       28x 73x 4326x                   3x       27x 72x 356x         3x 28x 10x   23x   5x         5x     30x               3x 28x 28x   252x   73x     28x 28x                                                      
import { Array, Option } from 'effect'
 
import type { GridToggles, MapStyle, RangeRings } from '../core/domain'
import type { LatLon } from '../core/geo/ellipsoid'
import type { Box, GridGeometry } from '../core/geo/geometry'
import { type ProfileId, gridDrawOrderFor } from '../core/systems/profiles'
import { isGridEnabled } from '../core/systems/registry'
import type { Grid, MapTone, SystemOptions } from '../core/systems/types'
import { basemapTone } from './basemaps'
import { CUE_COLOR, cueCellFeatures, cueMarkerFeatures } from './cue'
import { LineRank, labelFeature, lineFeature } from './features'
import { buildRings, ringFeatures, ringStep } from './rings'
 
export type OverlayInput = Readonly<{
  gridToggles: GridToggles.GridToggles
  isGridLabelsVisible: boolean
  /** What the map shows now, as [west, south, east, north]. */
  bounds: Box
  zoom: number
  maybeCue: Option.Option<LatLon>
  options: SystemOptions
  isRangeRingsVisible: boolean
  ringSpacing: RangeRings.RingSpacing
  ringUnit: RangeRings.RingUnit
  basemap: MapStyle.Basemap
  /** Sets the drawing order: the profile's grids draw on top. */
  profile: ProfileId
}>
 
/** What the map's overlay layers show: lines, the cue marker, and labels. */
export type OverlayFrame = Readonly<{
  lines: GeoJSON.FeatureCollection<GeoJSON.LineString>
  cueMarker: GeoJSON.FeatureCollection<GeoJSON.Point>
  labels: GeoJSON.FeatureCollection<GeoJSON.Point>
}>
 
/** Below this zoom the whole globe can be in view, so grids cover every longitude. */
const WHOLE_WORLD_ZOOM = 2.5
 
/** Clip the view to the world, and use every longitude when the whole globe can show. */
export const gridBox = ([west, south, east, north]: Box, zoom: number): Box => {
  const isWholeWorld = zoom < WHOLE_WORLD_ZOOM || east - west >= 360
  return [
    isWholeWorld ? -180 : Math.max(-180, west),
    Math.max(-90, south),
    isWholeWorld ? 180 : Math.min(180, east),
    Math.min(90, north),
  ]
}
 
type BuiltGrid = Readonly<{ grid: Grid; geometry: GridGeometry }>
 
const MIN_LINE_WIDTH = 0.5
 
/** Grids later in the drawing order draw on top, so their lines get the higher rank. */
const gridLineFeatures = (
  built: ReadonlyArray<BuiltGrid>,
  tone: MapTone,
): Array<GeoJSON.Feature<GeoJSON.LineString>> =>
  Array.flatMap(built, ({ grid, geometry }, index) =>
    Array.map(geometry.paths, gridPath =>
      lineFeature(gridPath.path, {
        layer: grid.id,
        color: grid.colors[tone],
        width: Math.max(MIN_LINE_WIDTH, gridPath.width),
        rank: LineRank.Grid + index,
      }),
    ),
  )
 
/** Grids later in the drawing order draw on top, so their labels get the lower sort key and win collisions. */
const gridLabelFeatures = (
  built: ReadonlyArray<BuiltGrid>,
  tone: MapTone,
): Array<GeoJSON.Feature<GeoJSON.Point>> =>
  Array.flatMap(built, ({ grid, geometry }, index) =>
    Array.map(geometry.labels, label =>
      labelFeature(label, built.length - index, grid.colors[tone], tone),
    ),
  )
 
/** Range rings belong to the cue, so they show with grid labels off and their labels outrank grid labels. */
const ringsAroundCue = (input: OverlayInput, tone: MapTone) =>
  Option.match(
    Option.filter(input.maybeCue, () => input.isRangeRingsVisible),
    {
      onNone: () => ({ lines: [], labels: [] }),
      onSome: cue => {
        const rings = buildRings(
          cue,
          ringStep(input.ringSpacing, input.bounds, input.ringUnit),
          input.ringUnit,
        )
        return {
          lines: ringFeatures(rings),
          labels: Array.map(rings, ring =>
            labelFeature(ring.label, 0, CUE_COLOR, tone),
          ),
        }
      },
    },
  )
 
/** Turn the enabled grids, the cue and its rings into the GeoJSON the map's overlay layers draw. */
export const buildOverlayFrame = (input: OverlayInput): OverlayFrame => {
  const box = gridBox(input.bounds, input.zoom)
  const built = Array.map(
    Array.filter(gridDrawOrderFor(input.profile), grid =>
      isGridEnabled(input.gridToggles, grid),
    ),
    grid => ({ grid, geometry: grid.build(box, input.zoom) }),
  )
  const tone = basemapTone[input.basemap]
  const rings = ringsAroundCue(input, tone)
  return {
    lines: {
      type: 'FeatureCollection',
      features: [
        ...gridLineFeatures(built, tone),
        ...rings.lines,
        ...cueCellFeatures(
          input.maybeCue,
          input.options,
          input.gridToggles,
          tone,
        ),
      ],
    },
    cueMarker: {
      type: 'FeatureCollection',
      features: [...cueMarkerFeatures(input.maybeCue)],
    },
    labels: {
      type: 'FeatureCollection',
      features: [
        ...rings.labels,
        ...(input.isGridLabelsVisible ? gridLabelFeatures(built, tone) : []),
      ],
    },
  }
}