facilmap/server/src/utils/geo.ts

40 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2020-04-14 23:40:00 +00:00
import { Bbox, Point } from "facilmap-types";
import { Latitude } from "../../../types/src";
const R = 6371; // km
2020-12-24 17:51:21 +00:00
export function isInBbox(position: Point, bbox: Bbox): boolean {
2020-04-14 23:40:00 +00:00
if(position.lat > bbox.top || position.lat < bbox.bottom)
return false;
if(bbox.right < bbox.left) // bbox spans over lon = 180
return (position.lon > bbox.left || position.lon < bbox.right);
else
return (position.lon > bbox.left && position.lon < bbox.right);
}
2020-12-24 17:51:21 +00:00
export function distanceToDegreesLat(km: number): number {
2020-04-14 23:40:00 +00:00
return km / (R * Math.PI / 180);
}
2020-12-24 17:51:21 +00:00
export function distanceToDegreesLon(km: number, lat: Latitude): number {
const fac = Math.cos(lat * Math.PI / 180);
2020-04-14 23:40:00 +00:00
return km / (fac * R * Math.PI / 180)
}
2020-12-24 17:51:21 +00:00
export function calculateBbox(trackPoints: Point[]): Bbox {
const bbox: Partial<Bbox> = { };
2020-04-14 23:40:00 +00:00
2020-12-24 17:51:21 +00:00
for(const trackPoint of trackPoints) {
2020-04-14 23:40:00 +00:00
if(bbox.top == null || trackPoint.lat > bbox.top)
bbox.top = trackPoint.lat;
if(bbox.bottom == null || trackPoint.lat < bbox.bottom)
bbox.bottom = trackPoint.lat;
if(bbox.left == null || trackPoint.lon < bbox.left)
bbox.left = trackPoint.lon;
if(bbox.right == null || trackPoint.lon > bbox.right)
bbox.right = trackPoint.lon;
}
2020-12-24 04:57:46 +00:00
return bbox as Bbox;
2020-04-14 23:40:00 +00:00
}