-
Notifications
You must be signed in to change notification settings - Fork 10
feat: wind barbs #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: wind barbs #366
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * Colours and stroke weights for drawing the arrow and barb shapes in a | ||
| * legend, taken from the styles the map draws them with so the legend cannot | ||
| * drift away from the map. | ||
| */ | ||
| import { | ||
| ARROW_LATTICE, | ||
| type ArrowStyle, | ||
| BARB_LATTICE, | ||
| TILE_PX | ||
| } from '@openmeteo/weather-map-layer'; | ||
|
|
||
| import { SHAPE_UNITS } from '$lib/arrow-shapes'; | ||
| import { BARB_OPACITY_RANGE, arrowLevelFor, defaultArrowStyle } from '$lib/chart-styles'; | ||
| import { alphaOfCssColor, rescaleInto } from '$lib/color'; | ||
| import { BARB_LINE_WIDTH } from '$lib/om-layer-defs'; | ||
|
|
||
| /** | ||
| * On-screen size of one lattice cell at an integer zoom, which is the size a | ||
| * shape is drawn to fill in the tile. | ||
| */ | ||
| const cellPx = (style: ArrowStyle): number => | ||
| TILE_PX / (style === 'barb' ? BARB_LATTICE : ARROW_LATTICE); | ||
|
|
||
| /** | ||
| * Stroke width in shape units, i.e. what a legend drawing the shape at | ||
| * `SHAPE_UNITS` needs to match the line the map draws at `sizePx`. | ||
| */ | ||
| const strokeUnits = (lineWidth: number, sizePx: number): number => | ||
| (lineWidth * SHAPE_UNITS) / sizePx; | ||
|
|
||
| const levels = () => [...defaultArrowStyle.levels].sort((a, b) => a.minSpeed - b.minSpeed); | ||
|
|
||
| const levelFor = (speed: number) => arrowLevelFor(defaultArrowStyle, speed); | ||
|
|
||
| const arrowColor = (speed: number, dark: boolean): string => { | ||
| const level = levelFor(speed); | ||
| return dark ? level.darkColor : level.lightColor; | ||
| }; | ||
|
|
||
| const barbColor = (speed: number, dark: boolean): string => { | ||
| const alphas = levels().map((level) => | ||
| alphaOfCssColor(dark ? level.darkColor : level.lightColor) | ||
| ); | ||
| const level = levelFor(speed); | ||
| const alpha = alphaOfCssColor(dark ? level.darkColor : level.lightColor); | ||
| const rgb = dark ? '255,255,255' : '0,0,0'; | ||
| return `rgba(${rgb},${rescaleInto(alpha, alphas, BARB_OPACITY_RANGE).toFixed(3)})`; | ||
| }; | ||
|
|
||
| /** Colour the map draws a shape in, for legends: the opacity ramps included. */ | ||
| export const shapeColor = (style: ArrowStyle, speed: number, dark: boolean): string => | ||
| style === 'barb' ? barbColor(speed, dark) : arrowColor(speed, dark); | ||
|
|
||
| /** Stroke a legend should use for a shape, matching the map's own weight. */ | ||
| export const shapeStrokeUnits = (style: ArrowStyle, speed: number): number => | ||
| style === 'barb' | ||
| ? strokeUnits(BARB_LINE_WIDTH, cellPx('barb')) | ||
| : strokeUnits(levelFor(speed).width, cellPx('arrow')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /** | ||
| * The arrow and wind-barb shapes, as polylines in a 100-unit box, mirroring | ||
| * the geometry `generateArrows` and `generateWindBarbs` write into vector | ||
| * tiles. Kept here so the sprites drawn for the icon renderer, and the | ||
| * previews in the settings pane, come from one description of the shape. | ||
| * | ||
| * Both shapes point up (north) at rest and are rotated by the renderer. | ||
| */ | ||
|
|
||
| /** Viewbox units per shape, i.e. `size` in the tile generators. */ | ||
| export const SHAPE_UNITS = 100; | ||
|
|
||
| export const MS_TO_KNOTS = 1.9438445; | ||
|
|
||
| // ── Arrow, from `generateArrows` ──────────────────────────────────────── | ||
|
|
||
| /** Head half-width and depth, as fractions of `size` in the generator. */ | ||
| const HEAD_HALF = 13; | ||
| const HEAD_DEPTH = 22; | ||
|
|
||
| /** Total arrow length per speed (m/s), the generator's own steps. */ | ||
| export const arrowLength = (speed: number): number => { | ||
| if (speed < 1) return 0.5; | ||
| if (speed < 2) return 0.55; | ||
| if (speed < 3) return 0.6; | ||
| if (speed < 5) return 0.7; | ||
| if (speed < 10) return 0.75; | ||
| if (speed < 20) return 0.8; | ||
| return 0.85; | ||
| }; | ||
|
|
||
| /** Arrow pointing where the flow goes, as polylines in the box. */ | ||
| export const arrowShape = (speed: number): number[][][] => { | ||
| const centre = SHAPE_UNITS / 2; | ||
| const half = (arrowLength(speed) * SHAPE_UNITS) / 2; | ||
| const barb = HEAD_DEPTH - half; | ||
| return [ | ||
| [ | ||
| [centre, centre + half], | ||
| [centre, centre - half] | ||
| ], | ||
| [ | ||
| [centre - HEAD_HALF, centre + barb], | ||
| [centre, centre - half], | ||
| [centre + HEAD_HALF, centre + barb] | ||
| ] | ||
| ]; | ||
| }; | ||
|
|
||
| // ── Wind barb, from `generateWindBarbs` ───────────────────────────────── | ||
|
|
||
| const CIRCLE_KNOTS = 0.5; | ||
| const STAFF_HALF = 0.42; | ||
| const SLOT_STEP = 0.16; | ||
| const BARB_SPAN = 0.64; | ||
| const BARB_LENGTH = 0.26; | ||
| const BARB_LEAN = 0.13; | ||
| const CALM_RADIUS = 0.21; | ||
| const CALM_INNER_RADIUS = 0.14; | ||
| const CELL_BUDGET = 0.47; | ||
| const FIT = Math.min(1, CELL_BUDGET / Math.hypot(STAFF_HALF + BARB_LEAN, BARB_LENGTH)); | ||
|
|
||
| /** Pennants, full barbs and half barbs for a speed, rounded to 5 kt. */ | ||
| export const barbCounts = (knots: number): { pennants: number; full: number; half: number } => { | ||
| let remaining = Math.round(knots / 5) * 5; | ||
| const pennants = Math.floor(remaining / 50); | ||
| remaining -= pennants * 50; | ||
| const full = Math.floor(remaining / 10); | ||
| remaining -= full * 10; | ||
| return { pennants, full, half: remaining >= 5 ? 1 : 0 }; | ||
| }; | ||
|
|
||
| export interface BarbShape { | ||
| /** Staff, barbs and calm rings. */ | ||
| lines: number[][][]; | ||
| /** Pennant triangles, drawn solid. */ | ||
| pennants: number[][][]; | ||
| } | ||
|
|
||
| /** Wind barb with its staff pointing into the wind, as polylines in the box. */ | ||
| export const barbShape = (knots: number): BarbShape => { | ||
| const centre = SHAPE_UNITS / 2; | ||
| const at = (across: number, along: number): number[] => [ | ||
| centre + across * FIT * SHAPE_UNITS, | ||
| centre + along * FIT * SHAPE_UNITS | ||
| ]; | ||
|
|
||
| const lines: number[][][] = []; | ||
| const pennants: number[][][] = []; | ||
|
|
||
| if (knots < CIRCLE_KNOTS) { | ||
| const corners = 12; | ||
| for (const radius of [CALM_RADIUS, CALM_INNER_RADIUS]) { | ||
| const ring: number[][] = []; | ||
| for (let i = 0; i <= corners; i++) { | ||
| const angle = (i / corners) * 2 * Math.PI; | ||
| ring.push(at(radius * Math.sin(angle), radius * Math.cos(angle))); | ||
| } | ||
| lines.push(ring); | ||
| } | ||
| return { lines, pennants }; | ||
| } | ||
|
|
||
| lines.push([at(0, STAFF_HALF), at(0, -STAFF_HALF)]); | ||
|
|
||
| const counts = barbCounts(knots); | ||
| const lonely = counts.half === 1 && counts.pennants === 0 && counts.full === 0; | ||
| const slots = counts.pennants * 2 + counts.full + counts.half + (lonely ? 1 : 0); | ||
| const step = Math.min(SLOT_STEP, BARB_SPAN / Math.max(1, slots)); | ||
|
|
||
| let along = -STAFF_HALF + (lonely ? step : 0); | ||
| for (let i = 0; i < counts.pennants; i++) { | ||
| pennants.push([at(0, along), at(BARB_LENGTH, along - BARB_LEAN), at(0, along + 2 * step)]); | ||
| along += 2 * step; | ||
| } | ||
| for (let i = 0; i < counts.full; i++) { | ||
| lines.push([at(0, along), at(BARB_LENGTH, along - BARB_LEAN)]); | ||
| along += step; | ||
| } | ||
| if (counts.half) { | ||
| lines.push([at(0, along), at(BARB_LENGTH / 2, along - BARB_LEAN / 2)]); | ||
| } | ||
|
|
||
| return { lines, pennants }; | ||
| }; | ||
|
|
||
| /** An SVG path for a shape's polylines, for previews outside the map. */ | ||
| export const shapePath = (polylines: number[][][]): string => | ||
| polylines | ||
| .map( | ||
| (line) => | ||
| 'M' + line.map(([x, y]) => `${Number(x.toFixed(1))} ${Number(y.toFixed(1))}`).join('L') | ||
| ) | ||
| .join(''); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.