mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-27 09:24:45 +00:00
Extend geonearestpoint operator to work with feature collections
This commit is contained in:
parent
60fbfa3b19
commit
6a3977dddd
@ -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 [];
|
||||
}
|
||||
|
@ -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>
|
Loading…
Reference in New Issue
Block a user