forked from MapComplete/MapComplete
		
	Wire in aspected-routing as calculated tag
This commit is contained in:
		
							parent
							
								
									829efc5d55
								
							
						
					
					
						commit
						e0b71ca53e
					
				
					 10 changed files with 14806 additions and 84 deletions
				
			
		|  | @ -15,7 +15,7 @@ import {LocalStorageSource} from "./Logic/Web/LocalStorageSource"; | |||
| import {Utils} from "./Utils"; | ||||
| import Svg from "./Svg"; | ||||
| import Link from "./UI/Base/Link"; | ||||
| import * as personal from "./assets/themes/personalLayout/personalLayout.json" | ||||
| import * as personal from "./assets/themes/personal/personal.json" | ||||
| import LayoutConfig from "./Customizations/JSON/LayoutConfig"; | ||||
| import * as L from "leaflet"; | ||||
| import Img from "./UI/Base/Img"; | ||||
|  |  | |||
|  | @ -6,7 +6,9 @@ import {Utils} from "../Utils"; | |||
| import BaseUIElement from "../UI/BaseUIElement"; | ||||
| import List from "../UI/Base/List"; | ||||
| import Title from "../UI/Base/Title"; | ||||
| import * as AR from "aspected-routing" | ||||
| import {RuleSet} from "aspected-routing" | ||||
| import {UIEventSourceTools} from "./UIEventSource"; | ||||
| 
 | ||||
| export class ExtraFunction { | ||||
| 
 | ||||
| 
 | ||||
|  | @ -41,9 +43,11 @@ export class ExtraFunction { | |||
| 
 | ||||
| 
 | ||||
|     private static readonly OverlapFunc = new ExtraFunction( | ||||
|         "overlapWith", | ||||
|         "Gives a list of features from the specified layer which this feature (partly) overlaps with. If the current feature is a point, all features that embed the point are given. The returned value is `{ feat: GeoJSONFeature, overlap: number}[]` where `overlap` is the overlapping surface are (in m²) for areas, the overlapping length (in meter) if the current feature is a line or `undefined` if the current feature is a point", | ||||
|         ["...layerIds - one or more layer ids  of the layer from which every feature is checked for overlap)"], | ||||
|         { | ||||
|             name: "overlapWith", | ||||
|             doc: "Gives a list of features from the specified layer which this feature (partly) overlaps with. If the current feature is a point, all features that embed the point are given. The returned value is `{ feat: GeoJSONFeature, overlap: number}[]` where `overlap` is the overlapping surface are (in m²) for areas, the overlapping length (in meter) if the current feature is a line or `undefined` if the current feature is a point", | ||||
|             args: ["...layerIds - one or more layer ids  of the layer from which every feature is checked for overlap)"] | ||||
|         }, | ||||
|         (params, feat) => { | ||||
|             return (...layerIds: string[]) => { | ||||
|                 const result = [] | ||||
|  | @ -62,9 +66,11 @@ export class ExtraFunction { | |||
|         } | ||||
|     ) | ||||
|     private static readonly DistanceToFunc = new ExtraFunction( | ||||
|         "distanceTo", | ||||
|         "Calculates the distance between the feature and a specified point in kilometer. The input should either be a pair of coordinates, a geojson feature or the ID of an object", | ||||
|         ["longitude", "latitude"], | ||||
|         { | ||||
|             name: "distanceTo", | ||||
|             doc: "Calculates the distance between the feature and a specified point in kilometer. The input should either be a pair of coordinates, a geojson feature or the ID of an object", | ||||
|             args: ["longitude", "latitude"] | ||||
|         }, | ||||
|         (featuresPerLayer, feature) => { | ||||
|             return (arg0, lat) => { | ||||
|                 if (typeof arg0 === "number") { | ||||
|  | @ -88,9 +94,11 @@ export class ExtraFunction { | |||
|     ) | ||||
| 
 | ||||
|     private static readonly ClosestObjectFunc = new ExtraFunction( | ||||
|         "closest", | ||||
|         "Given either a list of geojson features or a single layer name, gives the single object which is nearest to the feature. In the case of ways/polygons, only the centerpoint is considered.", | ||||
|         ["list of features"], | ||||
|         { | ||||
|             name: "closest", | ||||
|             doc: "Given either a list of geojson features or a single layer name, gives the single object which is nearest to the feature. In the case of ways/polygons, only the centerpoint is considered.", | ||||
|             args: ["list of features"] | ||||
|         }, | ||||
|         (params, feature) => { | ||||
|             return (features) => { | ||||
|                 if (typeof features === "string") { | ||||
|  | @ -139,29 +147,40 @@ export class ExtraFunction { | |||
| 
 | ||||
| 
 | ||||
|     private static readonly Memberships = new ExtraFunction( | ||||
|         "memberships", | ||||
|         "Gives a list of `{role: string, relation: Relation}`-objects, containing all the relations that this feature is part of. " + | ||||
|         "\n\n" + | ||||
|         "For example: `_part_of_walking_routes=feat.memberships().map(r => r.relation.tags.name).join(';')`", | ||||
|         [], | ||||
|         { | ||||
|             name: "memberships", | ||||
|             doc: "Gives a list of `{role: string, relation: Relation}`-objects, containing all the relations that this feature is part of. " + | ||||
|                 "\n\n" + | ||||
|                 "For example: `_part_of_walking_routes=feat.memberships().map(r => r.relation.tags.name).join(';')`", | ||||
|             args: [] | ||||
|         }, | ||||
|         (params, _) => { | ||||
|             return () => params.relations ?? []; | ||||
|         } | ||||
|     ) | ||||
| 
 | ||||
|     private static readonly AspectedRouting = new ExtraFunction( | ||||
|         "score", | ||||
|         "Given the path of an aspected routing json file, will calculate the score" + | ||||
|         "\n\n" + | ||||
|         "For example: `_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')`", | ||||
|         [], | ||||
|         (params, _) => { | ||||
|             return () => params.relations ?? []; | ||||
|         { | ||||
|             name: "score", | ||||
|             doc: "Given the path of an aspected routing json file, will calculate the score. This score is wrapped in a UIEventSource, so for further calculations, use `.map(score => ...)`" + | ||||
|                 "\n\n" + | ||||
|                 "For example: `_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')`", | ||||
|             args: ["path"] | ||||
|         }, | ||||
|         (_, feature) => { | ||||
|             return (path) => { | ||||
|                 return UIEventSourceTools.downloadJsonCached(path).map(config => { | ||||
|                     if (config === undefined) { | ||||
|                         return | ||||
|                     } | ||||
|                     return new RuleSet(config).runProgram(feature.properties) | ||||
|                 }) | ||||
|             } | ||||
|         } | ||||
|     ) | ||||
| 
 | ||||
|     private static readonly allFuncs: ExtraFunction[] = [ | ||||
|         ExtraFunction.DistanceToFunc,  | ||||
|         ExtraFunction.DistanceToFunc, | ||||
|         ExtraFunction.OverlapFunc, | ||||
|         ExtraFunction.ClosestObjectFunc, | ||||
|         ExtraFunction.Memberships, | ||||
|  | @ -170,14 +189,15 @@ export class ExtraFunction { | |||
|     private readonly _name: string; | ||||
|     private readonly _args: string[]; | ||||
|     private readonly _doc: string; | ||||
|     private readonly _async: boolean; | ||||
|     private readonly _f: (params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any; | ||||
| 
 | ||||
|     constructor(name: string, doc: string, args: string[], f: ((params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any)) { | ||||
|         this._name = name; | ||||
|         this._doc = doc; | ||||
|         this._args = args; | ||||
|     constructor(options: { name: string, doc: string, args: string[] }, | ||||
|                 f: ((params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any)) { | ||||
|         this._name = options.name; | ||||
|         this._doc = options.doc; | ||||
|         this._args = options.args; | ||||
|         this._f = f; | ||||
| console.dir(AR) | ||||
|     } | ||||
| 
 | ||||
|     public static FullPatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature) { | ||||
|  | @ -203,7 +223,6 @@ console.dir(AR) | |||
|     } | ||||
| 
 | ||||
|     public PatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature: any) { | ||||
| 
 | ||||
|         feature[this._name] = this._f({featuresPerLayer: featuresPerLayer, relations: relations}, feature); | ||||
|         feature[this._name] = this._f({featuresPerLayer: featuresPerLayer, relations: relations}, feature) | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -27,8 +27,8 @@ export default class MetaTagging { | |||
|                        relations: Map<string, { role: string, relation: Relation }[]>, | ||||
|                        layers: LayerConfig[], | ||||
|                        includeDates = true) { | ||||
|          | ||||
|         if(features === undefined || features.length === 0){ | ||||
| 
 | ||||
|         if (features === undefined || features.length === 0) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|  | @ -79,14 +79,10 @@ export default class MetaTagging { | |||
|                 } | ||||
| 
 | ||||
|             } | ||||
|              | ||||
|              | ||||
|              | ||||
|              | ||||
|              | ||||
|              | ||||
| 
 | ||||
| 
 | ||||
|         }) | ||||
|          | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|  | @ -115,6 +111,17 @@ export default class MetaTagging { | |||
|                 const f = (featuresPerLayer, feature: any) => { | ||||
|                     try { | ||||
|                         let result = func(feature); | ||||
|                         if(result instanceof UIEventSource){ | ||||
|                             result.addCallbackAndRunD(d => { | ||||
|                                 if (typeof d !== "string") { | ||||
|                                     // Make sure it is a string!
 | ||||
|                                     d = JSON.stringify(d); | ||||
|                                 } | ||||
|                                 feature.properties[key] = d; | ||||
|                             }) | ||||
|                             result = result.data | ||||
|                         } | ||||
|                          | ||||
|                         if (result === undefined || result === "") { | ||||
|                             return; | ||||
|                         } | ||||
|  | @ -124,11 +131,11 @@ export default class MetaTagging { | |||
|                         } | ||||
|                         feature.properties[key] = result; | ||||
|                     } catch (e) { | ||||
|                         if(MetaTagging. errorPrintCount < MetaTagging.stopErrorOutputAt){ | ||||
|                         if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) { | ||||
|                             console.warn("Could not calculate a calculated tag defined by " + code + " due to " + e + ". This is code defined in the theme. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e) | ||||
|                             MetaTagging.   errorPrintCount ++; | ||||
|                             if(MetaTagging. errorPrintCount == MetaTagging.stopErrorOutputAt){ | ||||
|                                 console.error("Got ",MetaTagging.stopErrorOutputAt," errors calculating this metatagging - stopping output now") | ||||
|                             MetaTagging.errorPrintCount++; | ||||
|                             if (MetaTagging.errorPrintCount == MetaTagging.stopErrorOutputAt) { | ||||
|                                 console.error("Got ", MetaTagging.stopErrorOutputAt, " errors calculating this metatagging - stopping output now") | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|  |  | |||
|  | @ -166,4 +166,21 @@ export class UIEventSource<T> { | |||
|             } | ||||
|         }) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| export class UIEventSourceTools { | ||||
| 
 | ||||
|     private static readonly _download_cache = new Map<string, UIEventSource<any>>() | ||||
| 
 | ||||
|     public static downloadJsonCached(url: string): UIEventSource<any>{ | ||||
|         const cached = UIEventSourceTools._download_cache.get(url) | ||||
|         if(cached !== undefined){ | ||||
|             return cached; | ||||
|         } | ||||
|         const src = new UIEventSource<any>(undefined) | ||||
|         UIEventSourceTools._download_cache.set(url, src) | ||||
|         Utils.downloadJson(url).then(r => src.setData(r)) | ||||
|         return src; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										2
									
								
								State.ts
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								State.ts
									
										
									
									
									
								
							|  | @ -100,7 +100,7 @@ export default class State { | |||
|     public readonly featureSwitchFakeUser: UIEventSource<boolean>; | ||||
| 
 | ||||
| 
 | ||||
|     public readonly featurePipeline: FeaturePipeline; | ||||
|     public featurePipeline: FeaturePipeline; | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| import State from "../../State"; | ||||
| import ThemeIntroductionPanel from "./ThemeIntroductionPanel"; | ||||
| import * as personal from "../../assets/themes/personalLayout/personalLayout.json"; | ||||
| import * as personal from "../../assets/themes/personal/personal.json"; | ||||
| import PersonalLayersPanel from "./PersonalLayersPanel"; | ||||
| import Svg from "../../Svg"; | ||||
| import Translations from "../i18n/Translations"; | ||||
|  |  | |||
|  | @ -51,6 +51,9 @@ | |||
|           ] | ||||
|         } | ||||
|       }, | ||||
|       "calculatedTags": [ | ||||
|         "_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')" | ||||
|       ], | ||||
|       "title": { | ||||
|         "render": { | ||||
|           "en": "Cycleways", | ||||
|  | @ -1072,6 +1075,9 @@ | |||
|           ] | ||||
|         } | ||||
|       }, | ||||
|       "calculatedTags": [ | ||||
|         "_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')" | ||||
|       ], | ||||
|       "minzoom": 14, | ||||
|       "wayHandling": 0, | ||||
|       "title": { | ||||
|  |  | |||
							
								
								
									
										14746
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										14746
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -58,7 +58,7 @@ | |||
|     "@types/leaflet.markercluster": "^1.4.3", | ||||
|     "@types/lz-string": "^1.3.34", | ||||
|     "@types/prompt-sync": "^4.1.0", | ||||
|     "aspected-routing": "^0.1.0", | ||||
|     "aspected-routing": "^0.2.0", | ||||
|     "autoprefixer": "^9.8.6", | ||||
|     "country-language": "^0.1.7", | ||||
|     "email-validator": "^2.0.4", | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| import {lstatSync, readdirSync, readFileSync} from "fs"; | ||||
| import {Utils} from "../Utils"; | ||||
| 
 | ||||
| Utils.runningFromConsole = true | ||||
| import * as https from "https"; | ||||
| import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue