Skip to content

Commit 2ec1fd6

Browse files
committed
fix: Move getIndexAndFractions outside of worker
1 parent 88471ff commit 2ec1fd6

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

src/utils/projections.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { degreesToRadians, lat2tile, lon2tile, radiansToDegrees, tile2lat, tile2lon } from './math';
2-
3-
import type { Bounds, Domain } from '../types';
1+
import {
2+
degreesToRadians,
3+
getIndexFromLatLong,
4+
lat2tile,
5+
lon2tile,
6+
radiansToDegrees,
7+
tile2lat,
8+
tile2lon
9+
} from './math';
10+
11+
import type { Bounds, Domain, IndexAndFractions } from '../types';
412
import type { DimensionRange } from '../types';
513

614
export interface Projection {
@@ -551,3 +559,37 @@ export const getBoundsFromGrid = (
551559
const maxLat = minLat + dy * ny;
552560
return [minLon, minLat, maxLon, maxLat];
553561
};
562+
563+
export const getIndexAndFractions = (
564+
lat: number,
565+
lon: number,
566+
domain: Domain,
567+
projectionGrid: ProjectionGrid | null,
568+
ranges = [
569+
{ start: 0, end: domain.grid.ny },
570+
{ start: 0, end: domain.grid.nx }
571+
],
572+
latLonMinMax: [minLat: number, minLon: number, maxLat: number, maxLon: number]
573+
) => {
574+
let indexObject: IndexAndFractions;
575+
if (domain.grid.projection && projectionGrid) {
576+
indexObject = projectionGrid.findPointInterpolated(lat, lon, ranges);
577+
} else {
578+
indexObject = getIndexFromLatLong(
579+
lat,
580+
lon,
581+
domain.grid.dx,
582+
domain.grid.dy,
583+
ranges[1]['end'] - ranges[1]['start'],
584+
latLonMinMax
585+
);
586+
}
587+
588+
return (
589+
indexObject ?? {
590+
index: NaN,
591+
xFraction: 0,
592+
yFraction: 0
593+
}
594+
);
595+
};

src/worker.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { hideZero, drawOnTiles } from './utils/variables';
22

3-
import { DynamicProjection, ProjectionGrid, type Projection } from './utils/projections';
3+
import {
4+
DynamicProjection,
5+
getIndexAndFractions,
6+
ProjectionGrid,
7+
type Projection
8+
} from './utils/projections';
49

510
import {
611
tile2lat,
@@ -132,40 +137,6 @@ const drawArrow = (
132137
}
133138
};
134139

135-
const getIndexAndFractions = (
136-
lat: number,
137-
lon: number,
138-
domain: Domain,
139-
projectionGrid: ProjectionGrid | null,
140-
ranges = [
141-
{ start: 0, end: domain.grid.ny },
142-
{ start: 0, end: domain.grid.nx }
143-
],
144-
latLonMinMax: [minLat: number, minLon: number, maxLat: number, maxLon: number]
145-
) => {
146-
let indexObject: IndexAndFractions;
147-
if (domain.grid.projection && projectionGrid) {
148-
indexObject = projectionGrid.findPointInterpolated(lat, lon, ranges);
149-
} else {
150-
indexObject = getIndexFromLatLong(
151-
lat,
152-
lon,
153-
domain.grid.dx,
154-
domain.grid.dy,
155-
ranges[1]['end'] - ranges[1]['start'],
156-
latLonMinMax
157-
);
158-
}
159-
160-
return (
161-
indexObject ?? {
162-
index: NaN,
163-
xFraction: 0,
164-
yFraction: 0
165-
}
166-
);
167-
};
168-
169140
self.onmessage = async (message) => {
170141
if (message.data.type == 'GT') {
171142
const key = message.data.key;

0 commit comments

Comments
 (0)