forked from MapComplete/MapComplete
Refactoring: fix metatagging
This commit is contained in:
parent
177697fe0a
commit
8fd3fbc0b7
34 changed files with 378 additions and 265 deletions
|
@ -1,10 +1,11 @@
|
|||
import { GeoOperations } from "./GeoOperations"
|
||||
import {GeoOperations} from "./GeoOperations"
|
||||
import Combine from "../UI/Base/Combine"
|
||||
import BaseUIElement from "../UI/BaseUIElement"
|
||||
import List from "../UI/Base/List"
|
||||
import Title from "../UI/Base/Title"
|
||||
import { BBox } from "./BBox"
|
||||
import { Feature, Geometry, MultiPolygon, Polygon } from "geojson"
|
||||
import {BBox} from "./BBox"
|
||||
import {Feature, Geometry, MultiPolygon, Polygon} from "geojson"
|
||||
import {GeoJSONFeature} from "maplibre-gl";
|
||||
|
||||
export interface ExtraFuncParams {
|
||||
/**
|
||||
|
@ -12,7 +13,7 @@ export interface ExtraFuncParams {
|
|||
* Note that more features then requested can be given back.
|
||||
* Format: [ [ geojson, geojson, geojson, ... ], [geojson, ...], ...]
|
||||
*/
|
||||
getFeaturesWithin: (layerId: string, bbox: BBox) => Feature<Geometry, Record<string, string>>[]
|
||||
getFeaturesWithin: (layerId: string, bbox: BBox) => Feature<Geometry, Record<string, string>>[][]
|
||||
getFeatureById: (id: string) => Feature<Geometry, Record<string, string>>
|
||||
}
|
||||
|
||||
|
@ -52,24 +53,27 @@ class EnclosingFunc implements ExtraFunction {
|
|||
if (otherFeaturess.length === 0) {
|
||||
continue
|
||||
}
|
||||
for (const otherFeature of otherFeaturess) {
|
||||
if (seenIds.has(otherFeature.properties.id)) {
|
||||
continue
|
||||
}
|
||||
seenIds.add(otherFeature.properties.id)
|
||||
if (
|
||||
otherFeature.geometry.type !== "Polygon" &&
|
||||
otherFeature.geometry.type !== "MultiPolygon"
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (
|
||||
GeoOperations.completelyWithin(
|
||||
<Feature>feat,
|
||||
<Feature<Polygon | MultiPolygon, any>>otherFeature
|
||||
)
|
||||
) {
|
||||
result.push({ feat: otherFeature })
|
||||
for (const otherFeatures of otherFeaturess) {
|
||||
for (const otherFeature of otherFeatures) {
|
||||
|
||||
if (seenIds.has(otherFeature.properties.id)) {
|
||||
continue
|
||||
}
|
||||
seenIds.add(otherFeature.properties.id)
|
||||
if (
|
||||
otherFeature.geometry.type !== "Polygon" &&
|
||||
otherFeature.geometry.type !== "MultiPolygon"
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (
|
||||
GeoOperations.completelyWithin(
|
||||
<Feature>feat,
|
||||
<Feature<Polygon | MultiPolygon, any>>otherFeature
|
||||
)
|
||||
) {
|
||||
result.push({feat: otherFeature})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +161,7 @@ class IntersectionFunc implements ExtraFunction {
|
|||
if (intersections.length === 0) {
|
||||
continue
|
||||
}
|
||||
result.push({ feat: otherFeature, intersections })
|
||||
result.push({feat: otherFeature, intersections})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -241,20 +245,22 @@ class ClosestNObjectFunc implements ExtraFunction {
|
|||
static GetClosestNFeatures(
|
||||
params: ExtraFuncParams,
|
||||
feature: any,
|
||||
features: string | any[],
|
||||
features: string | Feature[],
|
||||
options?: { maxFeatures?: number; uniqueTag?: string | undefined; maxDistance?: number }
|
||||
): { feat: any; distance: number }[] {
|
||||
const maxFeatures = options?.maxFeatures ?? 1
|
||||
const maxDistance = options?.maxDistance ?? 500
|
||||
const uniqueTag: string | undefined = options?.uniqueTag
|
||||
console.log("Calculating 'closestn' features")
|
||||
let allFeatures: Feature[][]
|
||||
if (typeof features === "string") {
|
||||
const name = features
|
||||
const bbox = GeoOperations.bbox(
|
||||
GeoOperations.buffer(GeoOperations.bbox(feature), maxDistance)
|
||||
)
|
||||
features = params.getFeaturesWithin(name, new BBox(bbox.geometry.coordinates))
|
||||
allFeatures = params.getFeaturesWithin(name, new BBox(bbox.geometry.coordinates))
|
||||
} else {
|
||||
features = [features]
|
||||
allFeatures = [features]
|
||||
}
|
||||
if (features === undefined) {
|
||||
return
|
||||
|
@ -263,9 +269,9 @@ class ClosestNObjectFunc implements ExtraFunction {
|
|||
const selfCenter = GeoOperations.centerpointCoordinates(feature)
|
||||
let closestFeatures: { feat: any; distance: number }[] = []
|
||||
|
||||
for (const featureList of features) {
|
||||
// Features is provided by 'getFeaturesWithin' which returns a list of lists of features, hence the double loop here
|
||||
for (const otherFeature of featureList) {
|
||||
for (const feats of allFeatures) {
|
||||
|
||||
for (const otherFeature of feats) {
|
||||
if (
|
||||
otherFeature === feature ||
|
||||
otherFeature.properties.id === feature.properties.id
|
||||
|
@ -333,7 +339,7 @@ class ClosestNObjectFunc implements ExtraFunction {
|
|||
// 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 }
|
||||
closestFeatures[i] = {feat: otherFeature, distance: distance}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
@ -423,6 +429,8 @@ class GetParsed implements ExtraFunction {
|
|||
}
|
||||
}
|
||||
|
||||
export type ExtraFuncType = typeof ExtraFunctions.types[number]
|
||||
|
||||
export class ExtraFunctions {
|
||||
static readonly intro = new Combine([
|
||||
new Title("Calculating tags with Javascript", 2),
|
||||
|
@ -440,7 +448,7 @@ export class ExtraFunctions {
|
|||
'"calculatedTags": [',
|
||||
' "_someKey=javascript-expression",',
|
||||
' "name=feat.properties.name ?? feat.properties.ref ?? feat.properties.operator",',
|
||||
" \"_distanceCloserThen3Km=feat.distanceTo( some_lon, some_lat) < 3 ? 'yes' : 'no'\" ",
|
||||
" \"_distanceCloserThen3Km=distanceTo(feat)( some_lon, some_lat) < 3 ? 'yes' : 'no'\" ",
|
||||
" ]",
|
||||
"````",
|
||||
"",
|
||||
|
@ -455,7 +463,8 @@ export class ExtraFunctions {
|
|||
.SetClass("flex-col")
|
||||
.AsMarkdown()
|
||||
|
||||
private static readonly allFuncs: ExtraFunction[] = [
|
||||
static readonly types = ["distanceTo", "overlapWith", "enclosingFeatures", "intersectionsWith", "closest", "closestn", "get"] as const
|
||||
private static readonly allFuncs = [
|
||||
new DistanceToFunc(),
|
||||
new OverlapFunc(),
|
||||
new EnclosingFunc(),
|
||||
|
@ -465,14 +474,16 @@ export class ExtraFunctions {
|
|||
new GetParsed(),
|
||||
]
|
||||
|
||||
public static FullPatchFeature(params: ExtraFuncParams, feature) {
|
||||
if (feature._is_patched) {
|
||||
return
|
||||
}
|
||||
feature._is_patched = true
|
||||
for (const func of ExtraFunctions.allFuncs) {
|
||||
feature[func._name] = func._f(params, feature)
|
||||
|
||||
public static constructHelpers(params: ExtraFuncParams): Record<ExtraFuncType, (feature: Feature) => Function> {
|
||||
const record: Record<string, (feature: GeoJSONFeature) => Function> = {}
|
||||
for (const f of ExtraFunctions.allFuncs) {
|
||||
if (this.types.indexOf(<any>f._name) < 0) {
|
||||
throw "Invalid extraFunc-type: " + f._name
|
||||
}
|
||||
record[f._name] = (feat) => f._f(params, feat)
|
||||
}
|
||||
return record
|
||||
}
|
||||
|
||||
public static HelpText(): BaseUIElement {
|
||||
|
|
|
@ -23,8 +23,7 @@ export default class GeoIndexedStore implements FeatureSource {
|
|||
* @param bbox
|
||||
* @constructor
|
||||
*/
|
||||
public GetFeaturesWithin(bbox: BBox, strict: boolean = false): Feature[] {
|
||||
// TODO optimize
|
||||
public GetFeaturesWithin(bbox: BBox): Feature[] {
|
||||
const bboxFeature = bbox.asGeojsonCached()
|
||||
return this.features.data.filter((f) => {
|
||||
if (f.geometry.type === "Point") {
|
||||
|
@ -40,7 +39,6 @@ export default class GeoIndexedStore implements FeatureSource {
|
|||
if (f.geometry.type === "Polygon" || f.geometry.type === "MultiPolygon") {
|
||||
return GeoOperations.intersect(f, bboxFeature) !== undefined
|
||||
}
|
||||
console.log("Calculating intersection between", bboxFeature, "and", f)
|
||||
return GeoOperations.intersect(f, bboxFeature) !== undefined
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
import SimpleMetaTaggers, { MetataggingState, SimpleMetaTagger } from "./SimpleMetaTagger"
|
||||
import { ExtraFuncParams, ExtraFunctions } from "./ExtraFunctions"
|
||||
import SimpleMetaTaggers, {MetataggingState, SimpleMetaTagger} from "./SimpleMetaTagger"
|
||||
import {ExtraFuncParams, ExtraFunctions, ExtraFuncType} from "./ExtraFunctions"
|
||||
import LayerConfig from "../Models/ThemeConfig/LayerConfig"
|
||||
import { Feature } from "geojson"
|
||||
import {Feature} from "geojson"
|
||||
import FeaturePropertiesStore from "./FeatureSource/Actors/FeaturePropertiesStore"
|
||||
import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"
|
||||
import { GeoIndexedStoreForLayer } from "./FeatureSource/Actors/GeoIndexedStore"
|
||||
import { IndexedFeatureSource } from "./FeatureSource/FeatureSource"
|
||||
import {GeoIndexedStoreForLayer} from "./FeatureSource/Actors/GeoIndexedStore"
|
||||
import {IndexedFeatureSource} from "./FeatureSource/FeatureSource"
|
||||
import OsmObjectDownloader from "./Osm/OsmObjectDownloader"
|
||||
import {Utils} from "../Utils";
|
||||
import {GeoJSONFeature} from "maplibre-gl";
|
||||
|
||||
/**
|
||||
* Metatagging adds various tags to the elements, e.g. lat, lon, surface area, ...
|
||||
|
@ -27,8 +29,16 @@ export default class MetaTagging {
|
|||
}) {
|
||||
const params: ExtraFuncParams = {
|
||||
getFeatureById: (id) => state.indexedFeatures.featuresById.data.get(id),
|
||||
getFeaturesWithin: (layerId, bbox) =>
|
||||
state.perLayer.get(layerId).GetFeaturesWithin(bbox),
|
||||
getFeaturesWithin: (layerId, bbox) => {
|
||||
if(layerId === '*' || layerId === null || layerId === undefined){
|
||||
const feats: Feature[][] = []
|
||||
state.perLayer.forEach((layer) => {
|
||||
feats.push(layer.GetFeaturesWithin(bbox))
|
||||
})
|
||||
return feats
|
||||
}
|
||||
return [state.perLayer.get(layerId).GetFeaturesWithin(bbox)];
|
||||
},
|
||||
}
|
||||
for (const layer of state.layout.layers) {
|
||||
if (layer.source === null) {
|
||||
|
@ -60,7 +70,7 @@ export default class MetaTagging {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method (re)calculates all metatags and calculated tags on every given object.
|
||||
* This method (re)calculates all metatags and calculated tags on every given feature.
|
||||
* The given features should be part of the given layer
|
||||
*
|
||||
* Returns true if at least one feature has changed properties
|
||||
|
@ -96,16 +106,26 @@ export default class MetaTagging {
|
|||
}
|
||||
|
||||
// The calculated functions - per layer - which add the new keys
|
||||
const layerFuncs = this.createRetaggingFunc(layer)
|
||||
// Calculated functions are defined by the layer
|
||||
const layerFuncs = this.createRetaggingFunc(layer, ExtraFunctions.constructHelpers(params))
|
||||
const state: MetataggingState = { layout, osmObjectDownloader }
|
||||
|
||||
let atLeastOneFeatureChanged = false
|
||||
|
||||
let strictlyEvaluated = 0
|
||||
for (let i = 0; i < features.length; i++) {
|
||||
const feature = features[i]
|
||||
const tags = featurePropertiesStores?.getStore(feature.properties.id)
|
||||
let somethingChanged = false
|
||||
let definedTags = new Set(Object.getOwnPropertyNames(feature.properties))
|
||||
if (layerFuncs !== undefined) {
|
||||
let retaggingChanged = false
|
||||
try {
|
||||
retaggingChanged = layerFuncs(feature)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
somethingChanged = somethingChanged || retaggingChanged
|
||||
}
|
||||
for (const metatag of metatagsToApply) {
|
||||
try {
|
||||
if (!metatag.keys.some((key) => !(key in feature.properties))) {
|
||||
|
@ -123,7 +143,10 @@ export default class MetaTagging {
|
|||
metatag.applyMetaTagsOnFeature(feature, layer, tags, state)
|
||||
if (options?.evaluateStrict) {
|
||||
for (const key of metatag.keys) {
|
||||
feature.properties[key]
|
||||
const evaluated = feature.properties[key]
|
||||
if(evaluated !== undefined){
|
||||
strictlyEvaluated++
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -153,15 +176,7 @@ export default class MetaTagging {
|
|||
}
|
||||
}
|
||||
|
||||
if (layerFuncs !== undefined) {
|
||||
let retaggingChanged = false
|
||||
try {
|
||||
retaggingChanged = layerFuncs(params, feature)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
somethingChanged = somethingChanged || retaggingChanged
|
||||
}
|
||||
|
||||
|
||||
if (somethingChanged) {
|
||||
try {
|
||||
|
@ -175,91 +190,83 @@ export default class MetaTagging {
|
|||
return atLeastOneFeatureChanged
|
||||
}
|
||||
|
||||
private static createFunctionsForFeature(
|
||||
layerId: string,
|
||||
calculatedTags: [string, string, boolean][]
|
||||
): ((feature: any) => void)[] {
|
||||
const functions: ((feature: any) => any)[] = []
|
||||
for (const entry of calculatedTags) {
|
||||
const key = entry[0]
|
||||
const code = entry[1]
|
||||
const isStrict = entry[2]
|
||||
if (code === undefined) {
|
||||
continue
|
||||
}
|
||||
/**
|
||||
* Creates a function that implements that calculates a property and adds this property onto the feature properties
|
||||
* @param specification
|
||||
* @param helperFunctions
|
||||
* @param layerId
|
||||
* @private
|
||||
*/
|
||||
private static createFunctionForFeature( [key, code, isStrict]: [string, string, boolean],
|
||||
helperFunctions: Record<ExtraFuncType, (feature: Feature) => Function>,
|
||||
layerId: string = "unkown layer"
|
||||
): ((feature: GeoJSONFeature) => void) | undefined {
|
||||
if (code === undefined) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const calculateAndAssign: (feat: any) => any = (feat) => {
|
||||
try {
|
||||
let result = new Function("feat", "return " + code + ";")(feat)
|
||||
if (result === "") {
|
||||
result === undefined
|
||||
}
|
||||
if (result !== undefined && typeof result !== "string") {
|
||||
// Make sure it is a string!
|
||||
result = JSON.stringify(result)
|
||||
}
|
||||
delete feat.properties[key]
|
||||
feat.properties[key] = result
|
||||
return result
|
||||
} catch (e) {
|
||||
if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) {
|
||||
console.warn(
|
||||
"Could not calculate a " +
|
||||
(isStrict ? "strict " : "") +
|
||||
" calculated tag for key " +
|
||||
key +
|
||||
" defined by " +
|
||||
code +
|
||||
" (in layer" +
|
||||
layerId +
|
||||
") due to \n" +
|
||||
e +
|
||||
"\n. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features",
|
||||
e,
|
||||
e.stack
|
||||
)
|
||||
MetaTagging.errorPrintCount++
|
||||
if (MetaTagging.errorPrintCount == MetaTagging.stopErrorOutputAt) {
|
||||
console.error(
|
||||
"Got ",
|
||||
MetaTagging.stopErrorOutputAt,
|
||||
" errors calculating this metatagging - stopping output now"
|
||||
)
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
|
||||
|
||||
const calculateAndAssign: ((feat: GeoJSONFeature) => (string | undefined)) = (feat) => {
|
||||
try {
|
||||
let result = new Function("feat", "{"+ExtraFunctions.types.join(", ")+"}", "return " + code + ";")(feat, helperFunctions)
|
||||
if (result === "") {
|
||||
result = undefined
|
||||
}
|
||||
if (result !== undefined && typeof result !== "string") {
|
||||
// Make sure it is a string!
|
||||
result = JSON.stringify(result)
|
||||
}
|
||||
delete feat.properties[key]
|
||||
feat.properties[key] = result
|
||||
return result
|
||||
} catch (e) {
|
||||
if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) {
|
||||
console.warn(
|
||||
"Could not calculate a " +
|
||||
(isStrict ? "strict " : "") +
|
||||
" calculated tag for key " +
|
||||
key +
|
||||
" defined by " +
|
||||
code +
|
||||
" (in layer" +
|
||||
layerId +
|
||||
") due to \n" +
|
||||
e +
|
||||
"\n. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features",
|
||||
e,
|
||||
e.stack
|
||||
)
|
||||
MetaTagging.errorPrintCount++
|
||||
if (MetaTagging.errorPrintCount == MetaTagging.stopErrorOutputAt) {
|
||||
console.error(
|
||||
"Got ",
|
||||
MetaTagging.stopErrorOutputAt,
|
||||
" errors calculating this metatagging - stopping output now"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isStrict) {
|
||||
functions.push(calculateAndAssign)
|
||||
continue
|
||||
}
|
||||
|
||||
// Lazy function
|
||||
const f = (feature: any) => {
|
||||
delete feature.properties[key]
|
||||
Object.defineProperty(feature.properties, key, {
|
||||
configurable: true,
|
||||
enumerable: false, // By setting this as not enumerable, the localTileSaver will _not_ calculate this
|
||||
get: function () {
|
||||
return calculateAndAssign(feature)
|
||||
},
|
||||
})
|
||||
return undefined
|
||||
}
|
||||
|
||||
functions.push(f)
|
||||
}
|
||||
return functions
|
||||
|
||||
if(isStrict){
|
||||
return calculateAndAssign
|
||||
}
|
||||
return (feature: any) => {
|
||||
delete feature.properties[key]
|
||||
Utils.AddLazyProperty(feature.properties, key, () => calculateAndAssign(feature))
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the function which adds all the calculated tags to a feature. Called once per layer
|
||||
*/
|
||||
private static createRetaggingFunc(
|
||||
layer: LayerConfig
|
||||
): (params: ExtraFuncParams, feature: any) => boolean {
|
||||
layer: LayerConfig,
|
||||
helpers: Record<ExtraFuncType, (feature: Feature) => Function>
|
||||
): (feature: any) => boolean {
|
||||
const calculatedTags: [string, string, boolean][] = layer.calculatedTags
|
||||
if (calculatedTags === undefined || calculatedTags.length === 0) {
|
||||
return undefined
|
||||
|
@ -267,18 +274,17 @@ export default class MetaTagging {
|
|||
|
||||
let functions: ((feature: Feature) => void)[] = MetaTagging.retaggingFuncCache.get(layer.id)
|
||||
if (functions === undefined) {
|
||||
functions = MetaTagging.createFunctionsForFeature(layer.id, calculatedTags)
|
||||
functions = calculatedTags.map(spec => this.createFunctionForFeature(spec, helpers, layer.id))
|
||||
MetaTagging.retaggingFuncCache.set(layer.id, functions)
|
||||
}
|
||||
|
||||
return (params: ExtraFuncParams, feature) => {
|
||||
return (feature: Feature) => {
|
||||
const tags = feature.properties
|
||||
if (tags === undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
ExtraFunctions.FullPatchFeature(params, feature)
|
||||
for (const f of functions) {
|
||||
f(feature)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue