From 6a3977dddd7adc2b833ba3ee6706023dc00cd2a5 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Wed, 26 Apr 2023 14:07:58 +0100 Subject: [PATCH] Extend geonearestpoint operator to work with feature collections --- .../geospatial/operators/measurement.js | 19 +++++--- .../tests/operators/geonearestpoint2.tid | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 plugins/tiddlywiki/geospatial/tests/operators/geonearestpoint2.tid diff --git a/plugins/tiddlywiki/geospatial/operators/measurement.js b/plugins/tiddlywiki/geospatial/operators/measurement.js index c215fabcb..51c46b654 100644 --- a/plugins/tiddlywiki/geospatial/operators/measurement.js +++ b/plugins/tiddlywiki/geospatial/operators/measurement.js @@ -30,15 +30,22 @@ exports.geodistance = function(source,operator,options) { exports.geonearestpoint = function(source,operator,options) { var target = geotools.parsePoint(operator.operands[0]), - points = []; + featureCollection = { + "type": "FeatureCollection", + "features": [] + }; source(function(tiddler,title) { - var point = geotools.parsePoint(title); - if(point) { - points.push(point) + var fc = $tw.utils.parseJSONSafe(title); + if(fc) { + if(fc.type === "FeatureCollection" && $tw.utils.isArray(fc.features)) { + Array.prototype.push.apply(featureCollection.features,fc.features); + } else if(fc.type === "Feature") { + featureCollection.features.push(fc); + } } }); - if(points.length > 0) { - return [JSON.stringify(turf.nearestPoint(target,turf.featureCollection(points)))]; + if(featureCollection.features.length > 0) { + return [JSON.stringify(turf.nearestPoint(target,featureCollection))]; } else { return []; } diff --git a/plugins/tiddlywiki/geospatial/tests/operators/geonearestpoint2.tid b/plugins/tiddlywiki/geospatial/tests/operators/geonearestpoint2.tid new file mode 100644 index 000000000..ef20e830b --- /dev/null +++ b/plugins/tiddlywiki/geospatial/tests/operators/geonearestpoint2.tid @@ -0,0 +1,45 @@ +title: $:/plugins/tiddlywiki/geospatial/tests/operators/geonearestpoint2 +description: geonearestpoint operator +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Description + +geonearestpoint operator ++ +title: Output + +\whitespace trim +<$let + oxford={{{ [geopoint[51.751944],[-1.257778]jsonset[id],[Oxford]] }}} + winchester={{{ [geopoint[51.0632],[-1.308]jsonset[id],[Winchester]] }}} + new-york={{{ [geopoint[40.730610],[-73.935242]jsonset[id],[New York]] }}} + places="""{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "id": "Oxford", + "geometry": { + "type": "Point", + "coordinates": [51.751944, -1.257778] + } + },{ + "type": "Feature", + "id": "Winchester", + "geometry": { + "type": "Point", + "coordinates": [51.0632, -1.308] + } + } + ] +} +""" +> +<$text text={{{ [geonearestpointjsonget[id]] }}}/> + + ++ +title: ExpectedResult + +

Winchester

\ No newline at end of file