@page-speed/maps 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -17
- package/README.md +530 -13
- package/dist/core/MapLibre.cjs +46 -20
- package/dist/core/MapLibre.cjs.map +1 -1
- package/dist/core/MapLibre.js +46 -20
- package/dist/core/MapLibre.js.map +1 -1
- package/dist/core/index.cjs +46 -20
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.js +46 -20
- package/dist/core/index.js.map +1 -1
- package/dist/hooks/index.cjs +109 -0
- package/dist/hooks/index.cjs.map +1 -0
- package/dist/hooks/index.d.cts +2 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +104 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useDefaultZoom.cjs +78 -0
- package/dist/hooks/useDefaultZoom.cjs.map +1 -0
- package/dist/hooks/useDefaultZoom.d.cts +30 -0
- package/dist/hooks/useDefaultZoom.d.ts +30 -0
- package/dist/hooks/useDefaultZoom.js +75 -0
- package/dist/hooks/useDefaultZoom.js.map +1 -0
- package/dist/hooks/useGeoCenter.cjs +39 -0
- package/dist/hooks/useGeoCenter.cjs.map +1 -0
- package/dist/hooks/useGeoCenter.d.cts +22 -0
- package/dist/hooks/useGeoCenter.d.ts +22 -0
- package/dist/hooks/useGeoCenter.js +36 -0
- package/dist/hooks/useGeoCenter.js.map +1 -0
- package/dist/index.cjs +147 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +145 -22
- package/dist/index.js.map +1 -1
- package/package.json +17 -2
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/hooks/useGeoCenter.ts
|
|
4
|
+
function computeGeoCenter(coordinates) {
|
|
5
|
+
if (coordinates.length === 0) return null;
|
|
6
|
+
if (coordinates.length === 1) {
|
|
7
|
+
return { lat: coordinates[0].lat, lng: coordinates[0].lng };
|
|
8
|
+
}
|
|
9
|
+
const toRad = (deg) => deg * Math.PI / 180;
|
|
10
|
+
const toDeg = (rad) => rad * 180 / Math.PI;
|
|
11
|
+
let x = 0;
|
|
12
|
+
let y = 0;
|
|
13
|
+
let z = 0;
|
|
14
|
+
for (const coord of coordinates) {
|
|
15
|
+
const latRad = toRad(coord.lat);
|
|
16
|
+
const lngRad = toRad(coord.lng);
|
|
17
|
+
x += Math.cos(latRad) * Math.cos(lngRad);
|
|
18
|
+
y += Math.cos(latRad) * Math.sin(lngRad);
|
|
19
|
+
z += Math.sin(latRad);
|
|
20
|
+
}
|
|
21
|
+
const total = coordinates.length;
|
|
22
|
+
x /= total;
|
|
23
|
+
y /= total;
|
|
24
|
+
z /= total;
|
|
25
|
+
const hyp = Math.sqrt(x * x + y * y);
|
|
26
|
+
const lat = toDeg(Math.atan2(z, hyp));
|
|
27
|
+
const lng = toDeg(Math.atan2(y, x));
|
|
28
|
+
return { lat, lng };
|
|
29
|
+
}
|
|
30
|
+
function useGeoCenter(coordinates) {
|
|
31
|
+
return useMemo(() => computeGeoCenter(coordinates), [coordinates]);
|
|
32
|
+
}
|
|
33
|
+
var TILE_SIZE = 512;
|
|
34
|
+
function latToMercatorY(lat) {
|
|
35
|
+
const latRad = lat * Math.PI / 180;
|
|
36
|
+
const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));
|
|
37
|
+
return TILE_SIZE / (2 * Math.PI) * (Math.PI - mercN);
|
|
38
|
+
}
|
|
39
|
+
function lngToMercatorX(lng) {
|
|
40
|
+
return TILE_SIZE / (2 * Math.PI) * ((lng + 180) / 360 * 2 * Math.PI);
|
|
41
|
+
}
|
|
42
|
+
function computeDefaultZoom(options) {
|
|
43
|
+
const {
|
|
44
|
+
coordinates,
|
|
45
|
+
mapWidth,
|
|
46
|
+
mapHeight,
|
|
47
|
+
padding = 50,
|
|
48
|
+
maxZoom = 18,
|
|
49
|
+
minZoom = 1
|
|
50
|
+
} = options;
|
|
51
|
+
if (coordinates.length === 0) return null;
|
|
52
|
+
if (coordinates.length === 1) return maxZoom;
|
|
53
|
+
if (mapWidth <= 0 || mapHeight <= 0) return null;
|
|
54
|
+
let minLat = Infinity;
|
|
55
|
+
let maxLat = -Infinity;
|
|
56
|
+
let minLng = Infinity;
|
|
57
|
+
let maxLng = -Infinity;
|
|
58
|
+
for (const coord of coordinates) {
|
|
59
|
+
if (coord.lat < minLat) minLat = coord.lat;
|
|
60
|
+
if (coord.lat > maxLat) maxLat = coord.lat;
|
|
61
|
+
if (coord.lng < minLng) minLng = coord.lng;
|
|
62
|
+
if (coord.lng > maxLng) maxLng = coord.lng;
|
|
63
|
+
}
|
|
64
|
+
const pixelXMin = lngToMercatorX(minLng);
|
|
65
|
+
const pixelXMax = lngToMercatorX(maxLng);
|
|
66
|
+
const pixelYMin = latToMercatorY(maxLat);
|
|
67
|
+
const pixelYMax = latToMercatorY(minLat);
|
|
68
|
+
const dx = Math.abs(pixelXMax - pixelXMin);
|
|
69
|
+
const dy = Math.abs(pixelYMax - pixelYMin);
|
|
70
|
+
const availableWidth = mapWidth - padding * 2;
|
|
71
|
+
const availableHeight = mapHeight - padding * 2;
|
|
72
|
+
if (availableWidth <= 0 || availableHeight <= 0) return minZoom;
|
|
73
|
+
let zoom;
|
|
74
|
+
if (dx === 0 && dy === 0) {
|
|
75
|
+
return maxZoom;
|
|
76
|
+
} else if (dx === 0) {
|
|
77
|
+
zoom = Math.log2(availableHeight / dy);
|
|
78
|
+
} else if (dy === 0) {
|
|
79
|
+
zoom = Math.log2(availableWidth / dx);
|
|
80
|
+
} else {
|
|
81
|
+
const zoomX = Math.log2(availableWidth / dx);
|
|
82
|
+
const zoomY = Math.log2(availableHeight / dy);
|
|
83
|
+
zoom = Math.min(zoomX, zoomY);
|
|
84
|
+
}
|
|
85
|
+
return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));
|
|
86
|
+
}
|
|
87
|
+
function useDefaultZoom(options) {
|
|
88
|
+
const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;
|
|
89
|
+
return useMemo(
|
|
90
|
+
() => computeDefaultZoom({
|
|
91
|
+
coordinates,
|
|
92
|
+
mapWidth,
|
|
93
|
+
mapHeight,
|
|
94
|
+
padding,
|
|
95
|
+
maxZoom,
|
|
96
|
+
minZoom
|
|
97
|
+
}),
|
|
98
|
+
[coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { computeDefaultZoom, computeGeoCenter, useDefaultZoom, useGeoCenter };
|
|
103
|
+
//# sourceMappingURL=index.js.map
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useGeoCenter.ts","../../src/hooks/useDefaultZoom.ts"],"names":["useMemo"],"mappings":";;;AAkBO,SAAS,iBACd,WAAA,EACwB;AACxB,EAAA,IAAI,WAAA,CAAY,MAAA,KAAW,CAAA,EAAG,OAAO,IAAA;AACrC,EAAA,IAAI,WAAA,CAAY,WAAW,CAAA,EAAG;AAC5B,IAAA,OAAO,EAAE,GAAA,EAAK,WAAA,CAAY,CAAC,CAAA,CAAE,KAAK,GAAA,EAAK,WAAA,CAAY,CAAC,CAAA,CAAE,GAAA,EAAI;AAAA,EAC5D;AAEA,EAAA,MAAM,KAAA,GAAQ,CAAC,GAAA,KAAiB,GAAA,GAAM,KAAK,EAAA,GAAM,GAAA;AACjD,EAAA,MAAM,KAAA,GAAQ,CAAC,GAAA,KAAiB,GAAA,GAAM,MAAO,IAAA,CAAK,EAAA;AAElD,EAAA,IAAI,CAAA,GAAI,CAAA;AACR,EAAA,IAAI,CAAA,GAAI,CAAA;AACR,EAAA,IAAI,CAAA,GAAI,CAAA;AAER,EAAA,KAAA,MAAW,SAAS,WAAA,EAAa;AAC/B,IAAA,MAAM,MAAA,GAA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AK,GAAA,CAAI,OAAA,EAAS,IAAA,CAAK,KAAA,CAAM,IAAA,GAAO,GAAG,CAAA,GAAI,GAAG,CAAC,CAAA;AAC1E;AAKO,SAAS,eAAe,OAAA,EAA4C;AACzE,EAAA,MAAM,EAAE,WAAA,EAAa,QAAA,EAAU,WAAW,OAAA,EAAS,OAAA,EAAS,SAAQ,GAAI,OAAA;AAExE,EAAA,OAAOA,OAAAA;AAAA,IACL,MACE,kBAAA,CAAmB;AAAA,MACjB,WAAA;AAAA,MACA,QAAA;AAAA,MACA,SAAA;AAAA,MACA,OAAA;AAAA,MACA,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,IACH,CAAC,WAAA,EAAa,QAAA,EAAU,SAAA,EAAW,OAAA,EAAS,SAAS,OAAO;AAAA,GAC9D;AACF","file":"index.js","sourcesContent":["import { useMemo } from \"react\";\n\nexport interface GeoCoordinate {\n lat: number;\n lng: number;\n}\n\nexport interface GeoCenterResult {\n lat: number;\n lng: number;\n}\n\n/**\n * Computes the geographic midpoint of an array of coordinates\n * using the Cartesian 3D averaging method (handles antimeridian, poles, etc.)\n *\n * Returns null if the input array is empty.\n */\nexport function computeGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) {\n return { lat: coordinates[0].lat, lng: coordinates[0].lng };\n }\n\n const toRad = (deg: number) => (deg * Math.PI) / 180;\n const toDeg = (rad: number) => (rad * 180) / Math.PI;\n\n let x = 0;\n let y = 0;\n let z = 0;\n\n for (const coord of coordinates) {\n const latRad = toRad(coord.lat);\n const lngRad = toRad(coord.lng);\n x += Math.cos(latRad) * Math.cos(lngRad);\n y += Math.cos(latRad) * Math.sin(lngRad);\n z += Math.sin(latRad);\n }\n\n const total = coordinates.length;\n x /= total;\n y /= total;\n z /= total;\n\n const hyp = Math.sqrt(x * x + y * y);\n const lat = toDeg(Math.atan2(z, hyp));\n const lng = toDeg(Math.atan2(y, x));\n\n return { lat, lng };\n}\n\n/**\n * React hook wrapper around computeGeoCenter.\n * Memoizes based on the coordinates array reference.\n */\nexport function useGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n return useMemo(() => computeGeoCenter(coordinates), [coordinates]);\n}\n","import { useMemo } from \"react\";\nimport type { GeoCoordinate } from \"./useGeoCenter\";\n\nexport interface DefaultZoomOptions {\n /** Array of coordinates to fit */\n coordinates: GeoCoordinate[];\n /** Map container width in pixels */\n mapWidth: number;\n /** Map container height in pixels */\n mapHeight: number;\n /** Padding in pixels around the bounds (default: 50) */\n padding?: number;\n /** Maximum zoom level to return (default: 18) */\n maxZoom?: number;\n /** Minimum zoom level to return (default: 1) */\n minZoom?: number;\n}\n\nconst TILE_SIZE = 512; // MapLibre GL JS uses 512px tiles\n\n/**\n * Converts latitude to Mercator Y pixel coordinate at zoom 0.\n */\nfunction latToMercatorY(lat: number): number {\n const latRad = (lat * Math.PI) / 180;\n const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));\n return (TILE_SIZE / (2 * Math.PI)) * (Math.PI - mercN);\n}\n\n/**\n * Converts longitude to Mercator X pixel coordinate at zoom 0.\n */\nfunction lngToMercatorX(lng: number): number {\n return (TILE_SIZE / (2 * Math.PI)) * (((lng + 180) / 360) * 2 * Math.PI);\n}\n\n/**\n * Pure function: computes the ideal zoom level to fit all coordinates\n * within the given map dimensions.\n *\n * Returns null if fewer than 1 coordinate is provided.\n * For a single coordinate, returns maxZoom (caller should use markerFocusZoom).\n */\nexport function computeDefaultZoom(options: DefaultZoomOptions): number | null {\n const {\n coordinates,\n mapWidth,\n mapHeight,\n padding = 50,\n maxZoom = 18,\n minZoom = 1,\n } = options;\n\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) return maxZoom;\n if (mapWidth <= 0 || mapHeight <= 0) return null;\n\n // Compute bounding box\n let minLat = Infinity;\n let maxLat = -Infinity;\n let minLng = Infinity;\n let maxLng = -Infinity;\n\n for (const coord of coordinates) {\n if (coord.lat < minLat) minLat = coord.lat;\n if (coord.lat > maxLat) maxLat = coord.lat;\n if (coord.lng < minLng) minLng = coord.lng;\n if (coord.lng > maxLng) maxLng = coord.lng;\n }\n\n // Compute pixel span at zoom 0\n const pixelXMin = lngToMercatorX(minLng);\n const pixelXMax = lngToMercatorX(maxLng);\n const pixelYMin = latToMercatorY(maxLat); // Note: Y is inverted in Mercator\n const pixelYMax = latToMercatorY(minLat);\n\n const dx = Math.abs(pixelXMax - pixelXMin);\n const dy = Math.abs(pixelYMax - pixelYMin);\n\n // Available viewport after padding\n const availableWidth = mapWidth - padding * 2;\n const availableHeight = mapHeight - padding * 2;\n\n if (availableWidth <= 0 || availableHeight <= 0) return minZoom;\n\n // Compute zoom for each axis: viewport = pixelSpan * 2^zoom\n // So zoom = log2(viewport / pixelSpan)\n let zoom: number;\n\n if (dx === 0 && dy === 0) {\n // All coordinates are identical\n return maxZoom;\n } else if (dx === 0) {\n zoom = Math.log2(availableHeight / dy);\n } else if (dy === 0) {\n zoom = Math.log2(availableWidth / dx);\n } else {\n const zoomX = Math.log2(availableWidth / dx);\n const zoomY = Math.log2(availableHeight / dy);\n zoom = Math.min(zoomX, zoomY); // Use the more restrictive axis\n }\n\n // Clamp to min/max and floor to avoid sub-pixel jitter\n return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));\n}\n\n/**\n * React hook wrapper around computeDefaultZoom.\n */\nexport function useDefaultZoom(options: DefaultZoomOptions): number | null {\n const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;\n\n return useMemo(\n () =>\n computeDefaultZoom({\n coordinates,\n mapWidth,\n mapHeight,\n padding,\n maxZoom,\n minZoom,\n }),\n [coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]\n );\n}\n"]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/hooks/useDefaultZoom.ts
|
|
6
|
+
var TILE_SIZE = 512;
|
|
7
|
+
function latToMercatorY(lat) {
|
|
8
|
+
const latRad = lat * Math.PI / 180;
|
|
9
|
+
const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));
|
|
10
|
+
return TILE_SIZE / (2 * Math.PI) * (Math.PI - mercN);
|
|
11
|
+
}
|
|
12
|
+
function lngToMercatorX(lng) {
|
|
13
|
+
return TILE_SIZE / (2 * Math.PI) * ((lng + 180) / 360 * 2 * Math.PI);
|
|
14
|
+
}
|
|
15
|
+
function computeDefaultZoom(options) {
|
|
16
|
+
const {
|
|
17
|
+
coordinates,
|
|
18
|
+
mapWidth,
|
|
19
|
+
mapHeight,
|
|
20
|
+
padding = 50,
|
|
21
|
+
maxZoom = 18,
|
|
22
|
+
minZoom = 1
|
|
23
|
+
} = options;
|
|
24
|
+
if (coordinates.length === 0) return null;
|
|
25
|
+
if (coordinates.length === 1) return maxZoom;
|
|
26
|
+
if (mapWidth <= 0 || mapHeight <= 0) return null;
|
|
27
|
+
let minLat = Infinity;
|
|
28
|
+
let maxLat = -Infinity;
|
|
29
|
+
let minLng = Infinity;
|
|
30
|
+
let maxLng = -Infinity;
|
|
31
|
+
for (const coord of coordinates) {
|
|
32
|
+
if (coord.lat < minLat) minLat = coord.lat;
|
|
33
|
+
if (coord.lat > maxLat) maxLat = coord.lat;
|
|
34
|
+
if (coord.lng < minLng) minLng = coord.lng;
|
|
35
|
+
if (coord.lng > maxLng) maxLng = coord.lng;
|
|
36
|
+
}
|
|
37
|
+
const pixelXMin = lngToMercatorX(minLng);
|
|
38
|
+
const pixelXMax = lngToMercatorX(maxLng);
|
|
39
|
+
const pixelYMin = latToMercatorY(maxLat);
|
|
40
|
+
const pixelYMax = latToMercatorY(minLat);
|
|
41
|
+
const dx = Math.abs(pixelXMax - pixelXMin);
|
|
42
|
+
const dy = Math.abs(pixelYMax - pixelYMin);
|
|
43
|
+
const availableWidth = mapWidth - padding * 2;
|
|
44
|
+
const availableHeight = mapHeight - padding * 2;
|
|
45
|
+
if (availableWidth <= 0 || availableHeight <= 0) return minZoom;
|
|
46
|
+
let zoom;
|
|
47
|
+
if (dx === 0 && dy === 0) {
|
|
48
|
+
return maxZoom;
|
|
49
|
+
} else if (dx === 0) {
|
|
50
|
+
zoom = Math.log2(availableHeight / dy);
|
|
51
|
+
} else if (dy === 0) {
|
|
52
|
+
zoom = Math.log2(availableWidth / dx);
|
|
53
|
+
} else {
|
|
54
|
+
const zoomX = Math.log2(availableWidth / dx);
|
|
55
|
+
const zoomY = Math.log2(availableHeight / dy);
|
|
56
|
+
zoom = Math.min(zoomX, zoomY);
|
|
57
|
+
}
|
|
58
|
+
return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));
|
|
59
|
+
}
|
|
60
|
+
function useDefaultZoom(options) {
|
|
61
|
+
const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;
|
|
62
|
+
return react.useMemo(
|
|
63
|
+
() => computeDefaultZoom({
|
|
64
|
+
coordinates,
|
|
65
|
+
mapWidth,
|
|
66
|
+
mapHeight,
|
|
67
|
+
padding,
|
|
68
|
+
maxZoom,
|
|
69
|
+
minZoom
|
|
70
|
+
}),
|
|
71
|
+
[coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.computeDefaultZoom = computeDefaultZoom;
|
|
76
|
+
exports.useDefaultZoom = useDefaultZoom;
|
|
77
|
+
//# sourceMappingURL=useDefaultZoom.cjs.map
|
|
78
|
+
//# sourceMappingURL=useDefaultZoom.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useDefaultZoom.ts"],"names":["useMemo"],"mappings":";;;;;AAkBA,IAAM,SAAA,GAAY,GAAA;AAKlB,SAAS,eAAe,GAAA,EAAqB;AAC3C,EAAA,MAAM,MAAA,GAAU,GAAA,GAAM,IAAA,CAAK,EAAA,GAAM,GAAA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GAAkB,EAAE,CAAA;AAC5C,IAAA,IAAA,GAAO,IAAA,CAAK,GAAA,CAAI,KAAA,EAAO,KAAK,CAAA;AAAA,EAC9B;AAGA,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,OAAA,EAAS,IAAA,CAAK,GAAA,CAAI,OAAA,EAAS,IAAA,CAAK,KAAA,CAAM,IAAA,GAAO,GAAG,CAAA,GAAI,GAAG,CAAC,CAAA;AAC1E;AAKO,SAAS,eAAe,OAAA,EAA4C;AACzE,EAAA,MAAM,EAAE,WAAA,EAAa,QAAA,EAAU,WAAW,OAAA,EAAS,OAAA,EAAS,SAAQ,GAAI,OAAA;AAExE,EAAA,OAAOA,aAAA;AAAA,IACL,MACE,kBAAA,CAAmB;AAAA,MACjB,WAAA;AAAA,MACA,QAAA;AAAA,MACA,SAAA;AAAA,MACA,OAAA;AAAA,MACA,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,IACH,CAAC,WAAA,EAAa,QAAA,EAAU,SAAA,EAAW,OAAA,EAAS,SAAS,OAAO;AAAA,GAC9D;AACF","file":"useDefaultZoom.cjs","sourcesContent":["import { useMemo } from \"react\";\nimport type { GeoCoordinate } from \"./useGeoCenter\";\n\nexport interface DefaultZoomOptions {\n /** Array of coordinates to fit */\n coordinates: GeoCoordinate[];\n /** Map container width in pixels */\n mapWidth: number;\n /** Map container height in pixels */\n mapHeight: number;\n /** Padding in pixels around the bounds (default: 50) */\n padding?: number;\n /** Maximum zoom level to return (default: 18) */\n maxZoom?: number;\n /** Minimum zoom level to return (default: 1) */\n minZoom?: number;\n}\n\nconst TILE_SIZE = 512; // MapLibre GL JS uses 512px tiles\n\n/**\n * Converts latitude to Mercator Y pixel coordinate at zoom 0.\n */\nfunction latToMercatorY(lat: number): number {\n const latRad = (lat * Math.PI) / 180;\n const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));\n return (TILE_SIZE / (2 * Math.PI)) * (Math.PI - mercN);\n}\n\n/**\n * Converts longitude to Mercator X pixel coordinate at zoom 0.\n */\nfunction lngToMercatorX(lng: number): number {\n return (TILE_SIZE / (2 * Math.PI)) * (((lng + 180) / 360) * 2 * Math.PI);\n}\n\n/**\n * Pure function: computes the ideal zoom level to fit all coordinates\n * within the given map dimensions.\n *\n * Returns null if fewer than 1 coordinate is provided.\n * For a single coordinate, returns maxZoom (caller should use markerFocusZoom).\n */\nexport function computeDefaultZoom(options: DefaultZoomOptions): number | null {\n const {\n coordinates,\n mapWidth,\n mapHeight,\n padding = 50,\n maxZoom = 18,\n minZoom = 1,\n } = options;\n\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) return maxZoom;\n if (mapWidth <= 0 || mapHeight <= 0) return null;\n\n // Compute bounding box\n let minLat = Infinity;\n let maxLat = -Infinity;\n let minLng = Infinity;\n let maxLng = -Infinity;\n\n for (const coord of coordinates) {\n if (coord.lat < minLat) minLat = coord.lat;\n if (coord.lat > maxLat) maxLat = coord.lat;\n if (coord.lng < minLng) minLng = coord.lng;\n if (coord.lng > maxLng) maxLng = coord.lng;\n }\n\n // Compute pixel span at zoom 0\n const pixelXMin = lngToMercatorX(minLng);\n const pixelXMax = lngToMercatorX(maxLng);\n const pixelYMin = latToMercatorY(maxLat); // Note: Y is inverted in Mercator\n const pixelYMax = latToMercatorY(minLat);\n\n const dx = Math.abs(pixelXMax - pixelXMin);\n const dy = Math.abs(pixelYMax - pixelYMin);\n\n // Available viewport after padding\n const availableWidth = mapWidth - padding * 2;\n const availableHeight = mapHeight - padding * 2;\n\n if (availableWidth <= 0 || availableHeight <= 0) return minZoom;\n\n // Compute zoom for each axis: viewport = pixelSpan * 2^zoom\n // So zoom = log2(viewport / pixelSpan)\n let zoom: number;\n\n if (dx === 0 && dy === 0) {\n // All coordinates are identical\n return maxZoom;\n } else if (dx === 0) {\n zoom = Math.log2(availableHeight / dy);\n } else if (dy === 0) {\n zoom = Math.log2(availableWidth / dx);\n } else {\n const zoomX = Math.log2(availableWidth / dx);\n const zoomY = Math.log2(availableHeight / dy);\n zoom = Math.min(zoomX, zoomY); // Use the more restrictive axis\n }\n\n // Clamp to min/max and floor to avoid sub-pixel jitter\n return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));\n}\n\n/**\n * React hook wrapper around computeDefaultZoom.\n */\nexport function useDefaultZoom(options: DefaultZoomOptions): number | null {\n const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;\n\n return useMemo(\n () =>\n computeDefaultZoom({\n coordinates,\n mapWidth,\n mapHeight,\n padding,\n maxZoom,\n minZoom,\n }),\n [coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]\n );\n}\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { GeoCoordinate } from './useGeoCenter.cjs';
|
|
2
|
+
|
|
3
|
+
interface DefaultZoomOptions {
|
|
4
|
+
/** Array of coordinates to fit */
|
|
5
|
+
coordinates: GeoCoordinate[];
|
|
6
|
+
/** Map container width in pixels */
|
|
7
|
+
mapWidth: number;
|
|
8
|
+
/** Map container height in pixels */
|
|
9
|
+
mapHeight: number;
|
|
10
|
+
/** Padding in pixels around the bounds (default: 50) */
|
|
11
|
+
padding?: number;
|
|
12
|
+
/** Maximum zoom level to return (default: 18) */
|
|
13
|
+
maxZoom?: number;
|
|
14
|
+
/** Minimum zoom level to return (default: 1) */
|
|
15
|
+
minZoom?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Pure function: computes the ideal zoom level to fit all coordinates
|
|
19
|
+
* within the given map dimensions.
|
|
20
|
+
*
|
|
21
|
+
* Returns null if fewer than 1 coordinate is provided.
|
|
22
|
+
* For a single coordinate, returns maxZoom (caller should use markerFocusZoom).
|
|
23
|
+
*/
|
|
24
|
+
declare function computeDefaultZoom(options: DefaultZoomOptions): number | null;
|
|
25
|
+
/**
|
|
26
|
+
* React hook wrapper around computeDefaultZoom.
|
|
27
|
+
*/
|
|
28
|
+
declare function useDefaultZoom(options: DefaultZoomOptions): number | null;
|
|
29
|
+
|
|
30
|
+
export { type DefaultZoomOptions, computeDefaultZoom, useDefaultZoom };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { GeoCoordinate } from './useGeoCenter.js';
|
|
2
|
+
|
|
3
|
+
interface DefaultZoomOptions {
|
|
4
|
+
/** Array of coordinates to fit */
|
|
5
|
+
coordinates: GeoCoordinate[];
|
|
6
|
+
/** Map container width in pixels */
|
|
7
|
+
mapWidth: number;
|
|
8
|
+
/** Map container height in pixels */
|
|
9
|
+
mapHeight: number;
|
|
10
|
+
/** Padding in pixels around the bounds (default: 50) */
|
|
11
|
+
padding?: number;
|
|
12
|
+
/** Maximum zoom level to return (default: 18) */
|
|
13
|
+
maxZoom?: number;
|
|
14
|
+
/** Minimum zoom level to return (default: 1) */
|
|
15
|
+
minZoom?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Pure function: computes the ideal zoom level to fit all coordinates
|
|
19
|
+
* within the given map dimensions.
|
|
20
|
+
*
|
|
21
|
+
* Returns null if fewer than 1 coordinate is provided.
|
|
22
|
+
* For a single coordinate, returns maxZoom (caller should use markerFocusZoom).
|
|
23
|
+
*/
|
|
24
|
+
declare function computeDefaultZoom(options: DefaultZoomOptions): number | null;
|
|
25
|
+
/**
|
|
26
|
+
* React hook wrapper around computeDefaultZoom.
|
|
27
|
+
*/
|
|
28
|
+
declare function useDefaultZoom(options: DefaultZoomOptions): number | null;
|
|
29
|
+
|
|
30
|
+
export { type DefaultZoomOptions, computeDefaultZoom, useDefaultZoom };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/hooks/useDefaultZoom.ts
|
|
4
|
+
var TILE_SIZE = 512;
|
|
5
|
+
function latToMercatorY(lat) {
|
|
6
|
+
const latRad = lat * Math.PI / 180;
|
|
7
|
+
const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));
|
|
8
|
+
return TILE_SIZE / (2 * Math.PI) * (Math.PI - mercN);
|
|
9
|
+
}
|
|
10
|
+
function lngToMercatorX(lng) {
|
|
11
|
+
return TILE_SIZE / (2 * Math.PI) * ((lng + 180) / 360 * 2 * Math.PI);
|
|
12
|
+
}
|
|
13
|
+
function computeDefaultZoom(options) {
|
|
14
|
+
const {
|
|
15
|
+
coordinates,
|
|
16
|
+
mapWidth,
|
|
17
|
+
mapHeight,
|
|
18
|
+
padding = 50,
|
|
19
|
+
maxZoom = 18,
|
|
20
|
+
minZoom = 1
|
|
21
|
+
} = options;
|
|
22
|
+
if (coordinates.length === 0) return null;
|
|
23
|
+
if (coordinates.length === 1) return maxZoom;
|
|
24
|
+
if (mapWidth <= 0 || mapHeight <= 0) return null;
|
|
25
|
+
let minLat = Infinity;
|
|
26
|
+
let maxLat = -Infinity;
|
|
27
|
+
let minLng = Infinity;
|
|
28
|
+
let maxLng = -Infinity;
|
|
29
|
+
for (const coord of coordinates) {
|
|
30
|
+
if (coord.lat < minLat) minLat = coord.lat;
|
|
31
|
+
if (coord.lat > maxLat) maxLat = coord.lat;
|
|
32
|
+
if (coord.lng < minLng) minLng = coord.lng;
|
|
33
|
+
if (coord.lng > maxLng) maxLng = coord.lng;
|
|
34
|
+
}
|
|
35
|
+
const pixelXMin = lngToMercatorX(minLng);
|
|
36
|
+
const pixelXMax = lngToMercatorX(maxLng);
|
|
37
|
+
const pixelYMin = latToMercatorY(maxLat);
|
|
38
|
+
const pixelYMax = latToMercatorY(minLat);
|
|
39
|
+
const dx = Math.abs(pixelXMax - pixelXMin);
|
|
40
|
+
const dy = Math.abs(pixelYMax - pixelYMin);
|
|
41
|
+
const availableWidth = mapWidth - padding * 2;
|
|
42
|
+
const availableHeight = mapHeight - padding * 2;
|
|
43
|
+
if (availableWidth <= 0 || availableHeight <= 0) return minZoom;
|
|
44
|
+
let zoom;
|
|
45
|
+
if (dx === 0 && dy === 0) {
|
|
46
|
+
return maxZoom;
|
|
47
|
+
} else if (dx === 0) {
|
|
48
|
+
zoom = Math.log2(availableHeight / dy);
|
|
49
|
+
} else if (dy === 0) {
|
|
50
|
+
zoom = Math.log2(availableWidth / dx);
|
|
51
|
+
} else {
|
|
52
|
+
const zoomX = Math.log2(availableWidth / dx);
|
|
53
|
+
const zoomY = Math.log2(availableHeight / dy);
|
|
54
|
+
zoom = Math.min(zoomX, zoomY);
|
|
55
|
+
}
|
|
56
|
+
return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));
|
|
57
|
+
}
|
|
58
|
+
function useDefaultZoom(options) {
|
|
59
|
+
const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;
|
|
60
|
+
return useMemo(
|
|
61
|
+
() => computeDefaultZoom({
|
|
62
|
+
coordinates,
|
|
63
|
+
mapWidth,
|
|
64
|
+
mapHeight,
|
|
65
|
+
padding,
|
|
66
|
+
maxZoom,
|
|
67
|
+
minZoom
|
|
68
|
+
}),
|
|
69
|
+
[coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { computeDefaultZoom, useDefaultZoom };
|
|
74
|
+
//# sourceMappingURL=useDefaultZoom.js.map
|
|
75
|
+
//# sourceMappingURL=useDefaultZoom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useDefaultZoom.ts"],"names":[],"mappings":";;;AAkBA,IAAM,SAAA,GAAY,GAAA;AAKlB,SAAS,eAAe,GAAA,EAAqB;AAC3C,EAAA,MAAM,MAAA,GAAU,GAAA,GAAM,IAAA,CAAK,EAAA,GAAM,GAAA;AACjC,EAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,GAAA,CAAI,IAAA,CAAK,GAAA,CAAI,KAAK,EAAA,GAAK,CAAA,GAAI,MAAA,GAAS,CAAC,CAAC,CAAA;AACzD,EAAA,OAAQ,SAAA,IAAa,CAAA,GAAI,IAAA,CAAK,EAAA,CAAA,IAAQ,KAAK,EAAA,G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AO,GAAG,CAAA,GAAI,GAAG,CAAC,CAAA;AAC1E;AAKO,SAAS,eAAe,OAAA,EAA4C;AACzE,EAAA,MAAM,EAAE,WAAA,EAAa,QAAA,EAAU,WAAW,OAAA,EAAS,OAAA,EAAS,SAAQ,GAAI,OAAA;AAExE,EAAA,OAAO,OAAA;AAAA,IACL,MACE,kBAAA,CAAmB;AAAA,MACjB,WAAA;AAAA,MACA,QAAA;AAAA,MACA,SAAA;AAAA,MACA,OAAA;AAAA,MACA,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,IACH,CAAC,WAAA,EAAa,QAAA,EAAU,SAAA,EAAW,OAAA,EAAS,SAAS,OAAO;AAAA,GAC9D;AACF","file":"useDefaultZoom.js","sourcesContent":["import { useMemo } from \"react\";\nimport type { GeoCoordinate } from \"./useGeoCenter\";\n\nexport interface DefaultZoomOptions {\n /** Array of coordinates to fit */\n coordinates: GeoCoordinate[];\n /** Map container width in pixels */\n mapWidth: number;\n /** Map container height in pixels */\n mapHeight: number;\n /** Padding in pixels around the bounds (default: 50) */\n padding?: number;\n /** Maximum zoom level to return (default: 18) */\n maxZoom?: number;\n /** Minimum zoom level to return (default: 1) */\n minZoom?: number;\n}\n\nconst TILE_SIZE = 512; // MapLibre GL JS uses 512px tiles\n\n/**\n * Converts latitude to Mercator Y pixel coordinate at zoom 0.\n */\nfunction latToMercatorY(lat: number): number {\n const latRad = (lat * Math.PI) / 180;\n const mercN = Math.log(Math.tan(Math.PI / 4 + latRad / 2));\n return (TILE_SIZE / (2 * Math.PI)) * (Math.PI - mercN);\n}\n\n/**\n * Converts longitude to Mercator X pixel coordinate at zoom 0.\n */\nfunction lngToMercatorX(lng: number): number {\n return (TILE_SIZE / (2 * Math.PI)) * (((lng + 180) / 360) * 2 * Math.PI);\n}\n\n/**\n * Pure function: computes the ideal zoom level to fit all coordinates\n * within the given map dimensions.\n *\n * Returns null if fewer than 1 coordinate is provided.\n * For a single coordinate, returns maxZoom (caller should use markerFocusZoom).\n */\nexport function computeDefaultZoom(options: DefaultZoomOptions): number | null {\n const {\n coordinates,\n mapWidth,\n mapHeight,\n padding = 50,\n maxZoom = 18,\n minZoom = 1,\n } = options;\n\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) return maxZoom;\n if (mapWidth <= 0 || mapHeight <= 0) return null;\n\n // Compute bounding box\n let minLat = Infinity;\n let maxLat = -Infinity;\n let minLng = Infinity;\n let maxLng = -Infinity;\n\n for (const coord of coordinates) {\n if (coord.lat < minLat) minLat = coord.lat;\n if (coord.lat > maxLat) maxLat = coord.lat;\n if (coord.lng < minLng) minLng = coord.lng;\n if (coord.lng > maxLng) maxLng = coord.lng;\n }\n\n // Compute pixel span at zoom 0\n const pixelXMin = lngToMercatorX(minLng);\n const pixelXMax = lngToMercatorX(maxLng);\n const pixelYMin = latToMercatorY(maxLat); // Note: Y is inverted in Mercator\n const pixelYMax = latToMercatorY(minLat);\n\n const dx = Math.abs(pixelXMax - pixelXMin);\n const dy = Math.abs(pixelYMax - pixelYMin);\n\n // Available viewport after padding\n const availableWidth = mapWidth - padding * 2;\n const availableHeight = mapHeight - padding * 2;\n\n if (availableWidth <= 0 || availableHeight <= 0) return minZoom;\n\n // Compute zoom for each axis: viewport = pixelSpan * 2^zoom\n // So zoom = log2(viewport / pixelSpan)\n let zoom: number;\n\n if (dx === 0 && dy === 0) {\n // All coordinates are identical\n return maxZoom;\n } else if (dx === 0) {\n zoom = Math.log2(availableHeight / dy);\n } else if (dy === 0) {\n zoom = Math.log2(availableWidth / dx);\n } else {\n const zoomX = Math.log2(availableWidth / dx);\n const zoomY = Math.log2(availableHeight / dy);\n zoom = Math.min(zoomX, zoomY); // Use the more restrictive axis\n }\n\n // Clamp to min/max and floor to avoid sub-pixel jitter\n return Math.max(minZoom, Math.min(maxZoom, Math.floor(zoom * 100) / 100));\n}\n\n/**\n * React hook wrapper around computeDefaultZoom.\n */\nexport function useDefaultZoom(options: DefaultZoomOptions): number | null {\n const { coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom } = options;\n\n return useMemo(\n () =>\n computeDefaultZoom({\n coordinates,\n mapWidth,\n mapHeight,\n padding,\n maxZoom,\n minZoom,\n }),\n [coordinates, mapWidth, mapHeight, padding, maxZoom, minZoom]\n );\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// src/hooks/useGeoCenter.ts
|
|
6
|
+
function computeGeoCenter(coordinates) {
|
|
7
|
+
if (coordinates.length === 0) return null;
|
|
8
|
+
if (coordinates.length === 1) {
|
|
9
|
+
return { lat: coordinates[0].lat, lng: coordinates[0].lng };
|
|
10
|
+
}
|
|
11
|
+
const toRad = (deg) => deg * Math.PI / 180;
|
|
12
|
+
const toDeg = (rad) => rad * 180 / Math.PI;
|
|
13
|
+
let x = 0;
|
|
14
|
+
let y = 0;
|
|
15
|
+
let z = 0;
|
|
16
|
+
for (const coord of coordinates) {
|
|
17
|
+
const latRad = toRad(coord.lat);
|
|
18
|
+
const lngRad = toRad(coord.lng);
|
|
19
|
+
x += Math.cos(latRad) * Math.cos(lngRad);
|
|
20
|
+
y += Math.cos(latRad) * Math.sin(lngRad);
|
|
21
|
+
z += Math.sin(latRad);
|
|
22
|
+
}
|
|
23
|
+
const total = coordinates.length;
|
|
24
|
+
x /= total;
|
|
25
|
+
y /= total;
|
|
26
|
+
z /= total;
|
|
27
|
+
const hyp = Math.sqrt(x * x + y * y);
|
|
28
|
+
const lat = toDeg(Math.atan2(z, hyp));
|
|
29
|
+
const lng = toDeg(Math.atan2(y, x));
|
|
30
|
+
return { lat, lng };
|
|
31
|
+
}
|
|
32
|
+
function useGeoCenter(coordinates) {
|
|
33
|
+
return react.useMemo(() => computeGeoCenter(coordinates), [coordinates]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.computeGeoCenter = computeGeoCenter;
|
|
37
|
+
exports.useGeoCenter = useGeoCenter;
|
|
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K,KAAA;AACL,EAAA,CAAA,IAAK,KAAA;AACL,EAAA,CAAA,IAAK,KAAA;AAEL,EAAA,MAAM,MAAM,IAAA,CAAK,IAAA,CAAK,CAAA,GAAI,CAAA,GAAI,IAAI,CAAC,CAAA;AACnC,EAAA,MAAM,MAAM,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,GAAG,CAAC,CAAA;AACpC,EAAA,MAAM,MAAM,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,CAAC,CAAC,CAAA;AAElC,EAAA,OAAO,EAAE,KAAK,GAAA,EAAI;AACpB;AAMO,SAAS,aACd,WAAA,EACwB;AACxB,EAAA,OAAOA,cAAQ,MAAM,gBAAA,CAAiB,WAAW,CAAA,EAAG,CAAC,WAAW,CAAC,CAAA;AACnE","file":"useGeoCenter.cjs","sourcesContent":["import { useMemo } from \"react\";\n\nexport interface GeoCoordinate {\n lat: number;\n lng: number;\n}\n\nexport interface GeoCenterResult {\n lat: number;\n lng: number;\n}\n\n/**\n * Computes the geographic midpoint of an array of coordinates\n * using the Cartesian 3D averaging method (handles antimeridian, poles, etc.)\n *\n * Returns null if the input array is empty.\n */\nexport function computeGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) {\n return { lat: coordinates[0].lat, lng: coordinates[0].lng };\n }\n\n const toRad = (deg: number) => (deg * Math.PI) / 180;\n const toDeg = (rad: number) => (rad * 180) / Math.PI;\n\n let x = 0;\n let y = 0;\n let z = 0;\n\n for (const coord of coordinates) {\n const latRad = toRad(coord.lat);\n const lngRad = toRad(coord.lng);\n x += Math.cos(latRad) * Math.cos(lngRad);\n y += Math.cos(latRad) * Math.sin(lngRad);\n z += Math.sin(latRad);\n }\n\n const total = coordinates.length;\n x /= total;\n y /= total;\n z /= total;\n\n const hyp = Math.sqrt(x * x + y * y);\n const lat = toDeg(Math.atan2(z, hyp));\n const lng = toDeg(Math.atan2(y, x));\n\n return { lat, lng };\n}\n\n/**\n * React hook wrapper around computeGeoCenter.\n * Memoizes based on the coordinates array reference.\n */\nexport function useGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n return useMemo(() => computeGeoCenter(coordinates), [coordinates]);\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface GeoCoordinate {
|
|
2
|
+
lat: number;
|
|
3
|
+
lng: number;
|
|
4
|
+
}
|
|
5
|
+
interface GeoCenterResult {
|
|
6
|
+
lat: number;
|
|
7
|
+
lng: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Computes the geographic midpoint of an array of coordinates
|
|
11
|
+
* using the Cartesian 3D averaging method (handles antimeridian, poles, etc.)
|
|
12
|
+
*
|
|
13
|
+
* Returns null if the input array is empty.
|
|
14
|
+
*/
|
|
15
|
+
declare function computeGeoCenter(coordinates: GeoCoordinate[]): GeoCenterResult | null;
|
|
16
|
+
/**
|
|
17
|
+
* React hook wrapper around computeGeoCenter.
|
|
18
|
+
* Memoizes based on the coordinates array reference.
|
|
19
|
+
*/
|
|
20
|
+
declare function useGeoCenter(coordinates: GeoCoordinate[]): GeoCenterResult | null;
|
|
21
|
+
|
|
22
|
+
export { type GeoCenterResult, type GeoCoordinate, computeGeoCenter, useGeoCenter };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface GeoCoordinate {
|
|
2
|
+
lat: number;
|
|
3
|
+
lng: number;
|
|
4
|
+
}
|
|
5
|
+
interface GeoCenterResult {
|
|
6
|
+
lat: number;
|
|
7
|
+
lng: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Computes the geographic midpoint of an array of coordinates
|
|
11
|
+
* using the Cartesian 3D averaging method (handles antimeridian, poles, etc.)
|
|
12
|
+
*
|
|
13
|
+
* Returns null if the input array is empty.
|
|
14
|
+
*/
|
|
15
|
+
declare function computeGeoCenter(coordinates: GeoCoordinate[]): GeoCenterResult | null;
|
|
16
|
+
/**
|
|
17
|
+
* React hook wrapper around computeGeoCenter.
|
|
18
|
+
* Memoizes based on the coordinates array reference.
|
|
19
|
+
*/
|
|
20
|
+
declare function useGeoCenter(coordinates: GeoCoordinate[]): GeoCenterResult | null;
|
|
21
|
+
|
|
22
|
+
export { type GeoCenterResult, type GeoCoordinate, computeGeoCenter, useGeoCenter };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/hooks/useGeoCenter.ts
|
|
4
|
+
function computeGeoCenter(coordinates) {
|
|
5
|
+
if (coordinates.length === 0) return null;
|
|
6
|
+
if (coordinates.length === 1) {
|
|
7
|
+
return { lat: coordinates[0].lat, lng: coordinates[0].lng };
|
|
8
|
+
}
|
|
9
|
+
const toRad = (deg) => deg * Math.PI / 180;
|
|
10
|
+
const toDeg = (rad) => rad * 180 / Math.PI;
|
|
11
|
+
let x = 0;
|
|
12
|
+
let y = 0;
|
|
13
|
+
let z = 0;
|
|
14
|
+
for (const coord of coordinates) {
|
|
15
|
+
const latRad = toRad(coord.lat);
|
|
16
|
+
const lngRad = toRad(coord.lng);
|
|
17
|
+
x += Math.cos(latRad) * Math.cos(lngRad);
|
|
18
|
+
y += Math.cos(latRad) * Math.sin(lngRad);
|
|
19
|
+
z += Math.sin(latRad);
|
|
20
|
+
}
|
|
21
|
+
const total = coordinates.length;
|
|
22
|
+
x /= total;
|
|
23
|
+
y /= total;
|
|
24
|
+
z /= total;
|
|
25
|
+
const hyp = Math.sqrt(x * x + y * y);
|
|
26
|
+
const lat = toDeg(Math.atan2(z, hyp));
|
|
27
|
+
const lng = toDeg(Math.atan2(y, x));
|
|
28
|
+
return { lat, lng };
|
|
29
|
+
}
|
|
30
|
+
function useGeoCenter(coordinates) {
|
|
31
|
+
return useMemo(() => computeGeoCenter(coordinates), [coordinates]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { computeGeoCenter, useGeoCenter };
|
|
35
|
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,CAAA,IAAK,KAAA;AACL,EAAA,CAAA,IAAK,KAAA;AAEL,EAAA,MAAM,MAAM,IAAA,CAAK,IAAA,CAAK,CAAA,GAAI,CAAA,GAAI,IAAI,CAAC,CAAA;AACnC,EAAA,MAAM,MAAM,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,GAAG,CAAC,CAAA;AACpC,EAAA,MAAM,MAAM,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,CAAA,EAAG,CAAC,CAAC,CAAA;AAElC,EAAA,OAAO,EAAE,KAAK,GAAA,EAAI;AACpB;AAMO,SAAS,aACd,WAAA,EACwB;AACxB,EAAA,OAAO,QAAQ,MAAM,gBAAA,CAAiB,WAAW,CAAA,EAAG,CAAC,WAAW,CAAC,CAAA;AACnE","file":"useGeoCenter.js","sourcesContent":["import { useMemo } from \"react\";\n\nexport interface GeoCoordinate {\n lat: number;\n lng: number;\n}\n\nexport interface GeoCenterResult {\n lat: number;\n lng: number;\n}\n\n/**\n * Computes the geographic midpoint of an array of coordinates\n * using the Cartesian 3D averaging method (handles antimeridian, poles, etc.)\n *\n * Returns null if the input array is empty.\n */\nexport function computeGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n if (coordinates.length === 0) return null;\n if (coordinates.length === 1) {\n return { lat: coordinates[0].lat, lng: coordinates[0].lng };\n }\n\n const toRad = (deg: number) => (deg * Math.PI) / 180;\n const toDeg = (rad: number) => (rad * 180) / Math.PI;\n\n let x = 0;\n let y = 0;\n let z = 0;\n\n for (const coord of coordinates) {\n const latRad = toRad(coord.lat);\n const lngRad = toRad(coord.lng);\n x += Math.cos(latRad) * Math.cos(lngRad);\n y += Math.cos(latRad) * Math.sin(lngRad);\n z += Math.sin(latRad);\n }\n\n const total = coordinates.length;\n x /= total;\n y /= total;\n z /= total;\n\n const hyp = Math.sqrt(x * x + y * y);\n const lat = toDeg(Math.atan2(z, hyp));\n const lng = toDeg(Math.atan2(y, x));\n\n return { lat, lng };\n}\n\n/**\n * React hook wrapper around computeGeoCenter.\n * Memoizes based on the coordinates array reference.\n */\nexport function useGeoCenter(\n coordinates: GeoCoordinate[]\n): GeoCenterResult | null {\n return useMemo(() => computeGeoCenter(coordinates), [coordinates]);\n}\n"]}
|