forked from MapComplete/MapComplete
		
	Add direction:centerpoint as calculated tag; improve typing of geoOperations.ts
This commit is contained in:
		
							parent
							
								
									4b6b709b4b
								
							
						
					
					
						commit
						4521889909
					
				
					 2 changed files with 37 additions and 3 deletions
				
			
		|  | @ -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<Point> { | ||||
|         const newFeature : Feature<Point> = turf.center(feature) | ||||
|         newFeature.properties = feature.properties | ||||
|         newFeature.id = feature.id | ||||
|         return newFeature | ||||
|  |  | |||
|  | @ -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<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( | ||||
|         { | ||||
|             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, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue