1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-21 12:53:15 +00:00

Extend geonearestpoint operator to work with feature collections

This commit is contained in:
jeremy@jermolene.com 2023-04-26 14:07:58 +01:00
parent 60fbfa3b19
commit 6a3977dddd
2 changed files with 58 additions and 6 deletions

View File

@ -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 [];
}

View File

@ -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={{{ [<places>geonearestpoint<new-york>jsonget[id]] }}}/>
</$let>
+
title: ExpectedResult
<p>Winchester</p>