diff --git a/Logic/GeoOperations.ts b/Logic/GeoOperations.ts index b45f536261..80371f9af2 100644 --- a/Logic/GeoOperations.ts +++ b/Logic/GeoOperations.ts @@ -13,7 +13,7 @@ import { Polygon, Properties, } from "@turf/turf" -import {GeoJSON, LineString} from "geojson"; +import {GeoJSON, LineString, Point} from "geojson"; export class GeoOperations { private static readonly _earthRadius = 6378137 @@ -27,8 +27,8 @@ export class GeoOperations { * Converts a GeoJson feature to a point GeoJson feature * @param feature */ - static centerpoint(feature: any) { - const newFeature = turf.center(feature) + static centerpoint(feature: any): Feature { + const newFeature : Feature = turf.center(feature) newFeature.properties = feature.properties newFeature.id = feature.id return newFeature diff --git a/Logic/SimpleMetaTagger.ts b/Logic/SimpleMetaTagger.ts index 2e2d257f03..33210ceaa1 100644 --- a/Logic/SimpleMetaTagger.ts +++ b/Logic/SimpleMetaTagger.ts @@ -9,6 +9,7 @@ import LayerConfig from "../Models/ThemeConfig/LayerConfig" import { CountryCoder } from "latlon2country" import Constants from "../Models/Constants" import { TagUtils } from "./Tags/TagUtils" +import {Feature, LineString} from "geojson"; export class SimpleMetaTagger { public readonly keys: string[] @@ -420,6 +421,38 @@ export default class SimpleMetaTaggers { return true } ) + + private static directionCenterpoint = new SimpleMetaTagger( + { + keys:["_direction:centerpoint"], + isLazy: true, + doc: "_direction:centerpoint is the direction of the linestring (in degrees) if one were standing at the projected centerpoint." + }, + (feature: Feature) => { + if(feature.geometry.type !== "LineString"){ + return false + } + + const ls = > feature; + + Object.defineProperty(feature.properties, "_direction:centerpoint", { + enumerable: false, + configurable: true, + get: () => { + const centroid = GeoOperations.centerpoint(feature) + const projected = GeoOperations.nearestPoint(ls, <[number,number]> centroid.geometry.coordinates) + const nextPoint = ls.geometry.coordinates[projected.properties.index + 1] + const bearing = GeoOperations.bearing(projected.geometry.coordinates, nextPoint) + delete feature.properties["_direction:centerpoint"] + feature.properties["_direction:centerpoint"] = bearing + return bearing + }, + }) + + return true + } + ) + private static currentTime = new SimpleMetaTagger( { keys: ["_now:date", "_now:datetime", "_loaded:date", "_loaded:_datetime"], @@ -457,6 +490,7 @@ export default class SimpleMetaTaggers { SimpleMetaTaggers.country, SimpleMetaTaggers.isOpen, SimpleMetaTaggers.directionSimplified, + SimpleMetaTaggers.directionCenterpoint, SimpleMetaTaggers.currentTime, SimpleMetaTaggers.objectMetaInfo, SimpleMetaTaggers.noBothButLeftRight,