forked from MapComplete/MapComplete
More refactoring, move minimap behind facade
This commit is contained in:
parent
c11ff652b8
commit
d5c1ba4cd1
79 changed files with 1848 additions and 1118 deletions
|
@ -1,14 +1,24 @@
|
|||
import {GeoOperations} from "./GeoOperations";
|
||||
import {BBox, GeoOperations} from "./GeoOperations";
|
||||
import Combine from "../UI/Base/Combine";
|
||||
import {Relation} from "./Osm/ExtractRelations";
|
||||
import RelationsTracker from "./Osm/RelationsTracker";
|
||||
import State from "../State";
|
||||
import {Utils} from "../Utils";
|
||||
import BaseUIElement from "../UI/BaseUIElement";
|
||||
import List from "../UI/Base/List";
|
||||
import Title from "../UI/Base/Title";
|
||||
import {UIEventSourceTools} from "./UIEventSource";
|
||||
import AspectedRouting from "./Osm/aspectedRouting";
|
||||
|
||||
export interface ExtraFuncParams {
|
||||
/**
|
||||
* Gets all the features from the given layer within the given BBOX.
|
||||
* Note that more features then requested can be given back.
|
||||
* Format: [ [ geojson, geojson, geojson, ... ], [geojson, ...], ...]
|
||||
*/
|
||||
getFeaturesWithin: (layerId: string, bbox: BBox) => any[][],
|
||||
memberships: RelationsTracker
|
||||
}
|
||||
|
||||
|
||||
export class ExtraFunction {
|
||||
|
||||
|
||||
|
@ -55,15 +65,20 @@ export class ExtraFunction {
|
|||
(params, feat) => {
|
||||
return (...layerIds: string[]) => {
|
||||
const result = []
|
||||
|
||||
const bbox = BBox.get(feat)
|
||||
|
||||
for (const layerId of layerIds) {
|
||||
const otherLayer = params.featuresPerLayer.get(layerId);
|
||||
if (otherLayer === undefined) {
|
||||
const otherLayers = params.getFeaturesWithin(layerId, bbox)
|
||||
if (otherLayers === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (otherLayer.length === 0) {
|
||||
if (otherLayers.length === 0) {
|
||||
continue;
|
||||
}
|
||||
result.push(...GeoOperations.calculateOverlap(feat, otherLayer));
|
||||
for (const otherLayer of otherLayers) {
|
||||
result.push(...GeoOperations.calculateOverlap(feat, otherLayer));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -77,6 +92,9 @@ export class ExtraFunction {
|
|||
},
|
||||
(featuresPerLayer, feature) => {
|
||||
return (arg0, lat) => {
|
||||
if (arg0 === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (typeof arg0 === "number") {
|
||||
// Feature._lon and ._lat is conveniently place by one of the other metatags
|
||||
return GeoOperations.distanceBetween([arg0, lat], [feature._lon, feature._lat]);
|
||||
|
@ -103,7 +121,7 @@ export class ExtraFunction {
|
|||
args: ["list of features"]
|
||||
},
|
||||
(params, feature) => {
|
||||
return (features) => ExtraFunction.GetClosestNFeatures(params, feature, features)[0].feat
|
||||
return (features) => ExtraFunction.GetClosestNFeatures(params, feature, features)?.[0]?.feat
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -113,12 +131,13 @@ export class ExtraFunction {
|
|||
doc: "Given either a list of geojson features or a single layer name, gives the n closest objects which are nearest to the feature. In the case of ways/polygons, only the centerpoint is considered. " +
|
||||
"Returns a list of `{feat: geojson, distance:number}` the empty list if nothing is found (or not yet laoded)\n\n" +
|
||||
"If a 'unique tag key' is given, the tag with this key will only appear once (e.g. if 'name' is given, all features will have a different name)",
|
||||
args: ["list of features", "amount of features", "unique tag key (optional)"]
|
||||
args: ["list of features", "amount of features", "unique tag key (optional)", "maxDistanceInMeters (optional)"]
|
||||
},
|
||||
(params, feature) => {
|
||||
return (features, amount, uniqueTag) => ExtraFunction.GetClosestNFeatures(params, feature, features, {
|
||||
return (features, amount, uniqueTag, maxDistanceInMeters) => ExtraFunction.GetClosestNFeatures(params, feature, features, {
|
||||
maxFeatures: Number(amount),
|
||||
uniqueTag: uniqueTag
|
||||
uniqueTag: uniqueTag,
|
||||
maxDistance: Number(maxDistanceInMeters)
|
||||
})
|
||||
}
|
||||
)
|
||||
|
@ -131,8 +150,10 @@ export class ExtraFunction {
|
|||
"For example: `_part_of_walking_routes=feat.memberships().map(r => r.relation.tags.name).join(';')`",
|
||||
args: []
|
||||
},
|
||||
(params, _) => {
|
||||
return () => params.relations ?? [];
|
||||
(params, feat) => {
|
||||
return () =>
|
||||
params.memberships.knownRelations.data.get(feat.properties.id) ?? []
|
||||
|
||||
}
|
||||
)
|
||||
private static readonly AspectedRouting = new ExtraFunction(
|
||||
|
@ -165,19 +186,19 @@ export class ExtraFunction {
|
|||
private readonly _name: string;
|
||||
private readonly _args: string[];
|
||||
private readonly _doc: string;
|
||||
private readonly _f: (params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any;
|
||||
private readonly _f: (params: ExtraFuncParams, feat: any) => any;
|
||||
|
||||
constructor(options: { name: string, doc: string, args: string[] },
|
||||
f: ((params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any)) {
|
||||
f: ((params: ExtraFuncParams, feat: any) => any)) {
|
||||
this._name = options.name;
|
||||
this._doc = options.doc;
|
||||
this._args = options.args;
|
||||
this._f = f;
|
||||
}
|
||||
|
||||
public static FullPatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature) {
|
||||
public static FullPatchFeature(params: ExtraFuncParams, feature) {
|
||||
for (const func of ExtraFunction.allFuncs) {
|
||||
func.PatchFeature(featuresPerLayer, relations, feature);
|
||||
func.PatchFeature(params, feature);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -198,121 +219,132 @@ export class ExtraFunction {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the closes N features, sorted by ascending distance
|
||||
* Gets the closes N features, sorted by ascending distance.
|
||||
*
|
||||
* @param params: The link to mapcomplete state
|
||||
* @param feature: The central feature under consideration
|
||||
* @param features: The other features
|
||||
* @param options: maxFeatures: The maximum amount of features to be returned. Default: 1; uniqueTag: returned features are not allowed to have the same value for this key; maxDistance: stop searching if it is too far away (in meter). Default: 500m
|
||||
* @constructor
|
||||
* @private
|
||||
*/
|
||||
private static GetClosestNFeatures(params, feature, features, options?: { maxFeatures?: number, uniqueTag?: string | undefined }): { feat: any, distance: number }[] {
|
||||
private static GetClosestNFeatures(params: ExtraFuncParams,
|
||||
feature: any,
|
||||
features: string | any[],
|
||||
options?: { maxFeatures?: number, uniqueTag?: string | undefined, maxDistance?: number }): { feat: any, distance: number }[] {
|
||||
const maxFeatures = options?.maxFeatures ?? 1
|
||||
const uniqueTag : string | undefined = options?.uniqueTag
|
||||
const maxDistance = options?.maxDistance ?? 500
|
||||
const uniqueTag: string | undefined = options?.uniqueTag
|
||||
if (typeof features === "string") {
|
||||
const name = features
|
||||
features = params.featuresPerLayer.get(features)
|
||||
if (features === undefined) {
|
||||
var keys = Utils.NoNull(Array.from(params.featuresPerLayer.keys()));
|
||||
if (keys.length > 0) {
|
||||
throw `No features defined for ${name}. Defined layers are ${keys.join(", ")}`;
|
||||
} else {
|
||||
// This is the first pass over an external dataset
|
||||
// Other data probably still has to load!
|
||||
return undefined;
|
||||
}
|
||||
|
||||
}
|
||||
const bbox = GeoOperations.bbox(GeoOperations.buffer(GeoOperations.bbox(feature), maxDistance))
|
||||
features = params.getFeaturesWithin(name, new BBox(bbox.geometry.coordinates))
|
||||
}else{
|
||||
features = [features]
|
||||
}
|
||||
if (features === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
let closestFeatures: { feat: any, distance: number }[] = [];
|
||||
for (const otherFeature of features) {
|
||||
if (otherFeature == feature || otherFeature.id == feature.id) {
|
||||
continue; // We ignore self
|
||||
}
|
||||
let distance = undefined;
|
||||
if (otherFeature._lon !== undefined && otherFeature._lat !== undefined) {
|
||||
distance = GeoOperations.distanceBetween([otherFeature._lon, otherFeature._lat], [feature._lon, feature._lat]);
|
||||
} else {
|
||||
distance = GeoOperations.distanceBetween(
|
||||
GeoOperations.centerpointCoordinates(otherFeature),
|
||||
[feature._lon, feature._lat]
|
||||
)
|
||||
}
|
||||
if (distance === undefined) {
|
||||
throw "Undefined distance!"
|
||||
}
|
||||
for(const featureList of features) {
|
||||
for (const otherFeature of featureList) {
|
||||
if (otherFeature == feature || otherFeature.id == feature.id) {
|
||||
continue; // We ignore self
|
||||
}
|
||||
let distance = undefined;
|
||||
if (otherFeature._lon !== undefined && otherFeature._lat !== undefined) {
|
||||
distance = GeoOperations.distanceBetween([otherFeature._lon, otherFeature._lat], [feature._lon, feature._lat]);
|
||||
} else {
|
||||
distance = GeoOperations.distanceBetween(
|
||||
GeoOperations.centerpointCoordinates(otherFeature),
|
||||
[feature._lon, feature._lat]
|
||||
)
|
||||
}
|
||||
if (distance === undefined) {
|
||||
throw "Undefined distance!"
|
||||
}
|
||||
if (distance > maxDistance) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (closestFeatures.length === 0) {
|
||||
closestFeatures.push({
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
})
|
||||
continue;
|
||||
}
|
||||
if (closestFeatures.length === 0) {
|
||||
closestFeatures.push({
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
})
|
||||
continue;
|
||||
}
|
||||
|
||||
if (closestFeatures.length >= maxFeatures && closestFeatures[maxFeatures - 1].distance < distance) {
|
||||
// The last feature of the list (and thus the furthest away is still closer
|
||||
// No use for checking, as we already have plenty of features!
|
||||
continue
|
||||
}
|
||||
if (closestFeatures.length >= maxFeatures && closestFeatures[maxFeatures - 1].distance < distance) {
|
||||
// The last feature of the list (and thus the furthest away is still closer
|
||||
// No use for checking, as we already have plenty of features!
|
||||
continue
|
||||
}
|
||||
|
||||
let targetIndex = closestFeatures.length
|
||||
for (let i = 0; i < closestFeatures.length; i++) {
|
||||
const closestFeature = closestFeatures[i];
|
||||
let targetIndex = closestFeatures.length
|
||||
for (let i = 0; i < closestFeatures.length; i++) {
|
||||
const closestFeature = closestFeatures[i];
|
||||
|
||||
if (uniqueTag !== undefined) {
|
||||
const uniqueTagsMatch = otherFeature.properties[uniqueTag] !== undefined &&
|
||||
closestFeature.feat.properties[uniqueTag] === otherFeature.properties[uniqueTag]
|
||||
if (uniqueTagsMatch) {
|
||||
targetIndex = -1
|
||||
if (closestFeature.distance > distance) {
|
||||
// This is a very special situation:
|
||||
// We want to see the tag `uniquetag=some_value` only once in the entire list (e.g. to prevent road segements of identical names to fill up the list of 'names of nearby roads')
|
||||
// AT this point, we have found a closer segment with the same, identical tag
|
||||
// so we replace directly
|
||||
closestFeatures[i] = {feat: otherFeature, distance: distance}
|
||||
if (uniqueTag !== undefined) {
|
||||
const uniqueTagsMatch = otherFeature.properties[uniqueTag] !== undefined &&
|
||||
closestFeature.feat.properties[uniqueTag] === otherFeature.properties[uniqueTag]
|
||||
if (uniqueTagsMatch) {
|
||||
targetIndex = -1
|
||||
if (closestFeature.distance > distance) {
|
||||
// This is a very special situation:
|
||||
// We want to see the tag `uniquetag=some_value` only once in the entire list (e.g. to prevent road segements of identical names to fill up the list of 'names of nearby roads')
|
||||
// AT this point, we have found a closer segment with the same, identical tag
|
||||
// so we replace directly
|
||||
closestFeatures[i] = {feat: otherFeature, distance: distance}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestFeature.distance > distance) {
|
||||
targetIndex = i
|
||||
|
||||
if (uniqueTag !== undefined) {
|
||||
const uniqueValue = otherFeature.properties[uniqueTag]
|
||||
// We might still have some other values later one with the same uniquetag that have to be cleaned
|
||||
for (let j = i; j < closestFeatures.length; j++) {
|
||||
if (closestFeatures[j].feat.properties[uniqueTag] === uniqueValue) {
|
||||
closestFeatures.splice(j, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestFeature.distance > distance) {
|
||||
targetIndex = i
|
||||
if (targetIndex == -1) {
|
||||
continue; // value is already swapped by the unique tag
|
||||
}
|
||||
|
||||
if (uniqueTag !== undefined) {
|
||||
const uniqueValue = otherFeature.properties[uniqueTag]
|
||||
// We might still have some other values later one with the same uniquetag that have to be cleaned
|
||||
for (let j = i; j < closestFeatures.length; j++) {
|
||||
if(closestFeatures[j].feat.properties[uniqueTag] === uniqueValue){
|
||||
closestFeatures.splice(j, 1)
|
||||
}
|
||||
}
|
||||
if (targetIndex < maxFeatures) {
|
||||
// insert and drop one
|
||||
closestFeatures.splice(targetIndex, 0, {
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
})
|
||||
if (closestFeatures.length >= maxFeatures) {
|
||||
closestFeatures.splice(maxFeatures, 1)
|
||||
}
|
||||
} else {
|
||||
// Overwrite the last element
|
||||
closestFeatures[targetIndex] = {
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (targetIndex == -1) {
|
||||
continue; // value is already swapped by the unique tag
|
||||
}
|
||||
|
||||
if (targetIndex < maxFeatures) {
|
||||
// insert and drop one
|
||||
closestFeatures.splice(targetIndex, 0, {
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
})
|
||||
if (closestFeatures.length >= maxFeatures) {
|
||||
closestFeatures.splice(maxFeatures, 1)
|
||||
}
|
||||
} else {
|
||||
// Overwrite the last element
|
||||
closestFeatures[targetIndex] = {
|
||||
feat: otherFeature,
|
||||
distance: distance
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return closestFeatures;
|
||||
}
|
||||
|
||||
public PatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature: any) {
|
||||
feature[this._name] = this._f({featuresPerLayer: featuresPerLayer, relations: relations}, feature)
|
||||
public PatchFeature(params: ExtraFuncParams, feature: any) {
|
||||
feature[this._name] = this._f(params, feature)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue