Skip to content

Commit ee3819e

Browse files
committed
move types to types.ts and use vectorOptions instead of url state
1 parent b699bc3 commit ee3819e

File tree

5 files changed

+100
-71
lines changed

5 files changed

+100
-71
lines changed

examples/vector/wind-arrows.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
});
5454

5555
// s3 storage structure: /data_spatial/{domain}/{modelRun /year/month/day/time}Z/{selectedTime year-month-day-time.om}?variable={variable}
56-
const omUrl = `https://map-tiles.open-meteo.com/data_spatial/dwd_icon/2025/10/27/1200Z/2025-10-27T1200.om?variable=wind_u_component_10m`;
56+
const omUrl = `https://map-tiles.open-meteo.com/data_spatial/dwd_icon/2025/11/20/1200Z/2025-11-20T1200.om?variable=wind_u_component_10m`;
5757

5858
map.on('load', () => {
5959
map.addSource('omFileRasterSource', {
@@ -70,7 +70,7 @@
7070
});
7171

7272
map.addSource('omFileVectorSource', {
73-
url: 'om://' + omUrl + '&arrows=true',
73+
url: 'om://' + omUrl,
7474
type: 'vector'
7575
});
7676

src/om-protocol.ts

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,18 @@ import { OMapsFileReader } from './om-file-reader';
1212
import { capitalize } from './utils';
1313
import { TilePromise, WorkerPool } from './worker-pool';
1414

15-
import type { ColorScales, DimensionRange, Domain, TileIndex, TileJSON, Variable } from './types';
16-
17-
interface OmProtocolInstance {
18-
colorScales: ColorScales;
19-
domainOptions: Domain[];
20-
variableOptions: Variable[];
21-
resolutionFactor: 0.5 | 1 | 2;
22-
omFileReader: OMapsFileReader;
23-
24-
// per-URL state:
25-
stateByKey: Map<string, OmUrlState>;
26-
}
27-
28-
interface OmUrlState {
29-
omUrl: string;
30-
dark: boolean;
31-
partial: boolean;
32-
tileSize: number;
33-
interval: number;
34-
domain: Domain;
35-
variable: Variable;
36-
mapBounds: number[];
37-
ranges: DimensionRange[] | null;
38-
39-
data: Data | null;
40-
dataPromise: Promise<Data> | null;
41-
lastAccess: number;
42-
}
43-
44-
export interface OmParseUrlCallbackResult {
45-
variable: Variable;
46-
ranges: DimensionRange[] | null;
47-
omUrl: string;
48-
dark: boolean;
49-
partial: boolean;
50-
interval: number;
51-
domain: Domain;
52-
mapBounds: number[];
53-
}
54-
55-
export interface OmProtocolSettings {
56-
tileSize: number;
57-
useSAB: boolean;
58-
colorScales: ColorScales;
59-
domainOptions: Domain[];
60-
variableOptions: Variable[];
61-
resolutionFactor: 0.5 | 1 | 2;
62-
parseUrlCallback: (
63-
url: string,
64-
domainOptions: Domain[],
65-
variableOptions: Variable[]
66-
) => OmParseUrlCallbackResult;
67-
postReadCallback:
68-
| ((omFileReader: OMapsFileReader, omUrl: string, data: Data) => void)
69-
| undefined;
70-
}
71-
72-
export interface Data {
73-
values: Float32Array | undefined;
74-
directions: Float32Array | undefined;
75-
}
15+
import type {
16+
Data,
17+
DimensionRange,
18+
Domain,
19+
OmParseUrlCallbackResult,
20+
OmProtocolInstance,
21+
OmProtocolSettings,
22+
OmUrlState,
23+
TileIndex,
24+
TileJSON,
25+
Variable
26+
} from './types';
7627

7728
setupGlobalCache();
7829
const workerPool = new WorkerPool();
@@ -215,7 +166,8 @@ const getTile = async (
215166
state: OmUrlState,
216167
data: Data,
217168
omUrl: string,
218-
type: 'image' | 'arrayBuffer'
169+
type: 'image' | 'arrayBuffer',
170+
settings: OmProtocolSettings
219171
): TilePromise => {
220172
const key = `${omUrl}/${state.tileSize}/${z}/${x}/${y}`;
221173

@@ -236,7 +188,8 @@ const getTile = async (
236188
protocol.colorScales?.custom ??
237189
protocol.colorScales[state.variable.value] ??
238190
getColorScale(state.variable.value),
239-
mapBounds: state.mapBounds
191+
mapBounds: state.mapBounds,
192+
vectorOptions: settings.vectorOptions
240193
});
241194
};
242195

@@ -258,7 +211,7 @@ const renderTile = async (
258211
const y = parseInt(result[4]);
259212

260213
const data = await ensureData(omUrl, protocol, state, settings);
261-
return getTile({ z, x, y }, protocol, state, data, omUrl, type);
214+
return getTile({ z, x, y }, protocol, state, data, omUrl, type, settings);
262215
};
263216

264217
const getTilejson = async (fullUrl: string, state: OmUrlState): Promise<TileJSON> => {
@@ -324,7 +277,12 @@ export const defaultOmProtocolSettings: OmProtocolSettings = {
324277
variableOptions: defaultVariableOptions,
325278
resolutionFactor: 1,
326279
parseUrlCallback: parseOmUrl,
327-
postReadCallback: undefined
280+
postReadCallback: undefined,
281+
vectorOptions: {
282+
grid: false,
283+
arrows: true,
284+
contours: false
285+
}
328286
};
329287

330288
export const omProtocol = async (

src/types.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
1+
import { OMapsFileReader } from './om-file-reader';
2+
3+
export interface OmProtocolInstance {
4+
colorScales: ColorScales;
5+
domainOptions: Domain[];
6+
variableOptions: Variable[];
7+
resolutionFactor: 0.5 | 1 | 2;
8+
omFileReader: OMapsFileReader;
9+
10+
// per-URL state:
11+
stateByKey: Map<string, OmUrlState>;
12+
}
13+
14+
export interface OmUrlState {
15+
omUrl: string;
16+
dark: boolean;
17+
partial: boolean;
18+
tileSize: number;
19+
interval: number;
20+
domain: Domain;
21+
variable: Variable;
22+
mapBounds: number[];
23+
ranges: DimensionRange[] | null;
24+
25+
data: Data | null;
26+
dataPromise: Promise<Data> | null;
27+
lastAccess: number;
28+
}
29+
30+
export interface OmParseUrlCallbackResult {
31+
variable: Variable;
32+
ranges: DimensionRange[] | null;
33+
omUrl: string;
34+
dark: boolean;
35+
partial: boolean;
36+
interval: number;
37+
domain: Domain;
38+
mapBounds: number[];
39+
}
40+
41+
export interface VectorOptions {
42+
grid: boolean;
43+
arrows: boolean;
44+
contours: boolean;
45+
}
46+
47+
export interface OmProtocolSettings {
48+
tileSize: number;
49+
useSAB: boolean;
50+
colorScales: ColorScales;
51+
domainOptions: Domain[];
52+
variableOptions: Variable[];
53+
resolutionFactor: 0.5 | 1 | 2;
54+
parseUrlCallback: (
55+
url: string,
56+
domainOptions: Domain[],
57+
variableOptions: Variable[]
58+
) => OmParseUrlCallbackResult;
59+
postReadCallback:
60+
| ((omFileReader: OMapsFileReader, omUrl: string, data: Data) => void)
61+
| undefined;
62+
vectorOptions: VectorOptions;
63+
}
64+
65+
export interface Data {
66+
values: Float32Array | undefined;
67+
directions: Float32Array | undefined;
68+
}
69+
170
export type TileJSON = {
271
tilejson: '2.2.0';
372
tiles: Array<string>;

src/worker-pool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Data } from './om-protocol';
22
// @ts-expect-error worker import
33
import TileWorker from './worker?worker&inline';
44

5-
import type { ColorScale, DimensionRange, Domain, Variable } from './types';
5+
import type { ColorScale, DimensionRange, Domain, Variable, VectorOptions } from './types';
66

77
export interface TileRequest {
88
type: 'getArrayBuffer' | 'getImage';
@@ -20,6 +20,7 @@ export interface TileRequest {
2020
variable: Variable;
2121
colorScale: ColorScale;
2222
mapBounds: number[];
23+
vectorOptions: VectorOptions;
2324
}
2425

2526
export type TileResponse = ImageBitmap | ArrayBuffer;

src/worker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,24 @@ self.onmessage = async (message: MessageEvent<TileRequest>): Promise<void> => {
9393
const interval = message.data.interval;
9494
const directions = message.data.data.directions;
9595
const colorScale = message.data.colorScale;
96+
const vectorOptions = message.data.vectorOptions;
9697

9798
if (!values) {
9899
throw new Error('No values provided');
99100
}
100101

101102
const pbf = new Pbf();
102103

103-
if (key.includes('grid=true')) {
104+
if (vectorOptions.grid) {
104105
if (domain.grid.type !== 'regular') {
105106
throw new Error('Only regular grid types supported');
106107
}
107108
generateGridPoints(pbf, values, directions, domain.grid, x, y, z);
108109
}
109-
if (key.includes('arrows=true') && directions) {
110+
if (vectorOptions.arrows && directions) {
110111
generateArrows(pbf, values, directions, domain, ranges, x, y, z, colorScale);
111112
}
112-
if (key.includes('contours=true')) {
113+
if (vectorOptions.contours) {
113114
const grid = GridFactory.create(domain.grid, ranges);
114115
generateContours(pbf, values, grid, x, y, z, interval ? interval : 2);
115116
}

0 commit comments

Comments
 (0)