From b6a4cc11bdf25c4cf09e55ad802eb0d460311c9d Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Mon, 22 Jan 2024 03:45:06 +0100 Subject: [PATCH] Fix: don't crash if no features nearby --- src/Logic/ExtraFunctions.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Logic/ExtraFunctions.ts b/src/Logic/ExtraFunctions.ts index 6575a543f..261c6832c 100644 --- a/src/Logic/ExtraFunctions.ts +++ b/src/Logic/ExtraFunctions.ts @@ -15,7 +15,7 @@ export interface ExtraFuncParams { */ getFeaturesWithin: ( layerId: string, - bbox: BBox, + bbox: BBox ) => Feature>[][] getFeatureById: (id: string) => Feature> } @@ -71,7 +71,7 @@ class EnclosingFunc implements ExtraFunction { if ( GeoOperations.completelyWithin( feat, - >otherFeature, + >otherFeature ) ) { result.push({ feat: otherFeature }) @@ -162,7 +162,7 @@ class IntersectionFunc implements ExtraFunction { for (const otherFeature of otherFeatures) { const intersections = GeoOperations.LineIntersections( feat, - >>otherFeature, + >>otherFeature ) if (intersections.length === 0) { continue @@ -192,7 +192,7 @@ class DistanceToFunc implements ExtraFunction { // Feature._lon and ._lat is conveniently place by one of the other metatags return GeoOperations.distanceBetween( [arg0, lat], - GeoOperations.centerpointCoordinates(feature), + GeoOperations.centerpointCoordinates(feature) ) } if (typeof arg0 === "string") { @@ -207,7 +207,7 @@ class DistanceToFunc implements ExtraFunction { // arg0 is probably a geojsonfeature return GeoOperations.distanceBetween( GeoOperations.centerpointCoordinates(arg0), - GeoOperations.centerpointCoordinates(feature), + GeoOperations.centerpointCoordinates(feature) ) } } @@ -253,7 +253,7 @@ class ClosestNObjectFunc implements ExtraFunction { params: ExtraFuncParams, feature: any, features: string | string[] | Feature[], - options?: { maxFeatures?: number; uniqueTag?: string | undefined; maxDistance?: number }, + options?: { maxFeatures?: number; uniqueTag?: string | undefined; maxDistance?: number } ): { feat: any; distance: number }[] { const maxFeatures = options?.maxFeatures ?? 1 const maxDistance = options?.maxDistance ?? 500 @@ -267,7 +267,7 @@ class ClosestNObjectFunc implements ExtraFunction { if (typeof spec === "string") { const name = spec const bbox = GeoOperations.bbox( - GeoOperations.buffer(GeoOperations.bbox(feature), maxDistance), + GeoOperations.buffer(GeoOperations.bbox(feature), maxDistance) ) const coors = <[number, number][]>bbox.geometry.coordinates allFeatures.push(...params.getFeaturesWithin(name, new BBox(coors))) @@ -283,7 +283,7 @@ class ClosestNObjectFunc implements ExtraFunction { const selfCenter = GeoOperations.centerpointCoordinates(feature) let closestFeatures: { feat: any; distance: number }[] = [] - for (const feats of allFeatures) { + for (const feats of allFeatures ?? []) { for (const otherFeature of feats) { if (otherFeature.properties === undefined) { console.warn("OtherFeature does not have properties:", otherFeature) @@ -296,14 +296,14 @@ class ClosestNObjectFunc implements ExtraFunction { } const distance = GeoOperations.distanceBetween( GeoOperations.centerpointCoordinates(otherFeature), - selfCenter, + selfCenter ) if (distance === undefined || distance === null || isNaN(distance)) { console.error( "Could not calculate the distance between", feature, "and", - otherFeature, + otherFeature ) throw "Undefined distance!" } @@ -313,7 +313,7 @@ class ClosestNObjectFunc implements ExtraFunction { "Got a suspiciously zero distance between", otherFeature, "and self-feature", - feature, + feature ) } @@ -347,7 +347,7 @@ class ClosestNObjectFunc implements ExtraFunction { const uniqueTagsMatch = otherFeature.properties[uniqueTag] !== undefined && closestFeature.feat.properties[uniqueTag] === - otherFeature.properties[uniqueTag] + otherFeature.properties[uniqueTag] if (uniqueTagsMatch) { targetIndex = -1 if (closestFeature.distance > distance) { @@ -440,7 +440,7 @@ class GetParsed implements ExtraFunction { return parsed } catch (e) { console.warn( - "Could not parse property " + key + " due to: " + e + ", the value is " + value, + "Could not parse property " + key + " due to: " + e + ", the value is " + value ) return undefined } @@ -464,10 +464,10 @@ export class ExtraFunctions { ]), "To enable this feature, add a field `calculatedTags` in the layer object, e.g.:", "````", - "\"calculatedTags\": [", - " \"_someKey=javascript-expression (lazy execution)\",", - " \"_some_other_key:=javascript expression (strict execution)", - " \"name=feat.properties.name ?? feat.properties.ref ?? feat.properties.operator\",", + '"calculatedTags": [', + ' "_someKey=javascript-expression (lazy execution)",', + ' "_some_other_key:=javascript expression (strict execution)', + ' "name=feat.properties.name ?? feat.properties.ref ?? feat.properties.operator",', " \"_distanceCloserThen3Km=distanceTo(feat)( some_lon, some_lat) < 3 ? 'yes' : 'no'\" ", " ]", "````", @@ -506,7 +506,7 @@ export class ExtraFunctions { ] public static constructHelpers( - params: ExtraFuncParams, + params: ExtraFuncParams ): Record Function> { const record: Record Function> = {} for (const f of ExtraFunctions.allFuncs) {