mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
refactor(geography): separate cable sources and bundled lookup modules
This commit is contained in:
1 parent
2213c42200
commit
3ce5d2e454
21 files changed
+2423
-1437
No files matched your search
@@ -0,0 +1,79 @@
|
||||
export function featureReference(feature) {
|
||||
const geometry = feature?.geometry;
|
||||
if (!geometry) return null;
|
||||
|
||||
const props = feature?.properties || {};
|
||||
const propertyCoords = coordsFromProperty(props.coordinates);
|
||||
if (propertyCoords) {
|
||||
return {
|
||||
lon: propertyCoords[0],
|
||||
lat: propertyCoords[1],
|
||||
};
|
||||
}
|
||||
|
||||
if (geometry.type === 'Point') {
|
||||
const coords = coordsFromPoint(geometry.coordinates);
|
||||
if (!coords) return null;
|
||||
return { lon: coords[0], lat: coords[1] };
|
||||
}
|
||||
|
||||
const coords = [];
|
||||
collectLonLat(geometry.coordinates, coords);
|
||||
if (!coords.length) return null;
|
||||
|
||||
let lonSum = 0;
|
||||
let latSum = 0;
|
||||
for (const [lon, lat] of coords) {
|
||||
lonSum += lon;
|
||||
latSum += lat;
|
||||
}
|
||||
return {
|
||||
lon: lonSum / coords.length,
|
||||
lat: latSum / coords.length,
|
||||
};
|
||||
}
|
||||
|
||||
export function coordsFromProperty(value) {
|
||||
if (!Array.isArray(value) || value.length < 2) return null;
|
||||
const lon = Number(value[0]);
|
||||
const lat = Number(value[1]);
|
||||
if (!Number.isFinite(lon) || !Number.isFinite(lat)) return null;
|
||||
return [lon, lat];
|
||||
}
|
||||
|
||||
export function coordsFromPoint(value) {
|
||||
if (!Array.isArray(value) || value.length < 2) return null;
|
||||
const lon = Number(value[0]);
|
||||
const lat = Number(value[1]);
|
||||
if (!Number.isFinite(lon) || !Number.isFinite(lat)) return null;
|
||||
return [lon, lat];
|
||||
}
|
||||
|
||||
export function collectLonLat(value, out) {
|
||||
if (!Array.isArray(value)) return;
|
||||
if (typeof value[0] === 'number' && typeof value[1] === 'number') {
|
||||
const lon = Number(value[0]);
|
||||
const lat = Number(value[1]);
|
||||
if (Number.isFinite(lon) && Number.isFinite(lat)) {
|
||||
out.push([lon, lat]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (const child of value) collectLonLat(child, out);
|
||||
}
|
||||
|
||||
export function featureLabel(feature) {
|
||||
const props = feature?.properties || {};
|
||||
return String(props.name || props.id || feature?.id || '').trim();
|
||||
}
|
||||
|
||||
export function normalizeFeatures(json, kind) {
|
||||
const features = Array.isArray(json?.features) ? json.features : [];
|
||||
return features.map((feature, index) => {
|
||||
const id = feature?.properties?.id || feature?.id || `${kind}-${index}`;
|
||||
return {
|
||||
...feature,
|
||||
id: String(id),
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user