Add direction:centerpoint as calculated tag; improve typing of geoOperations.ts

This commit is contained in:
Pieter Vander Vennet 2022-09-27 18:52:13 +02:00
parent 4b6b709b4b
commit 4521889909
2 changed files with 37 additions and 3 deletions

View file

@ -13,7 +13,7 @@ import {
Polygon, Polygon,
Properties, Properties,
} from "@turf/turf" } from "@turf/turf"
import {GeoJSON, LineString} from "geojson"; import {GeoJSON, LineString, Point} from "geojson";
export class GeoOperations { export class GeoOperations {
private static readonly _earthRadius = 6378137 private static readonly _earthRadius = 6378137
@ -27,8 +27,8 @@ export class GeoOperations {
* Converts a GeoJson feature to a point GeoJson feature * Converts a GeoJson feature to a point GeoJson feature
* @param feature * @param feature
*/ */
static centerpoint(feature: any) { static centerpoint(feature: any): Feature<Point> {
const newFeature = turf.center(feature) const newFeature : Feature<Point> = turf.center(feature)
newFeature.properties = feature.properties newFeature.properties = feature.properties
newFeature.id = feature.id newFeature.id = feature.id
return newFeature return newFeature

View file

@ -9,6 +9,7 @@ import LayerConfig from "../Models/ThemeConfig/LayerConfig"
import { CountryCoder } from "latlon2country" import { CountryCoder } from "latlon2country"
import Constants from "../Models/Constants" import Constants from "../Models/Constants"
import { TagUtils } from "./Tags/TagUtils" import { TagUtils } from "./Tags/TagUtils"
import {Feature, LineString} from "geojson";
export class SimpleMetaTagger { export class SimpleMetaTagger {
public readonly keys: string[] public readonly keys: string[]
@ -420,6 +421,38 @@ export default class SimpleMetaTaggers {
return true 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<LineString>> 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( private static currentTime = new SimpleMetaTagger(
{ {
keys: ["_now:date", "_now:datetime", "_loaded:date", "_loaded:_datetime"], keys: ["_now:date", "_now:datetime", "_loaded:date", "_loaded:_datetime"],
@ -457,6 +490,7 @@ export default class SimpleMetaTaggers {
SimpleMetaTaggers.country, SimpleMetaTaggers.country,
SimpleMetaTaggers.isOpen, SimpleMetaTaggers.isOpen,
SimpleMetaTaggers.directionSimplified, SimpleMetaTaggers.directionSimplified,
SimpleMetaTaggers.directionCenterpoint,
SimpleMetaTaggers.currentTime, SimpleMetaTaggers.currentTime,
SimpleMetaTaggers.objectMetaInfo, SimpleMetaTaggers.objectMetaInfo,
SimpleMetaTaggers.noBothButLeftRight, SimpleMetaTaggers.noBothButLeftRight,