mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-10 08:00:03 +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) {
|
exports.geonearestpoint = function(source,operator,options) {
|
||||||
var target = geotools.parsePoint(operator.operands[0]),
|
var target = geotools.parsePoint(operator.operands[0]),
|
||||||
points = [];
|
featureCollection = {
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"features": []
|
||||||
|
};
|
||||||
source(function(tiddler,title) {
|
source(function(tiddler,title) {
|
||||||
var point = geotools.parsePoint(title);
|
var fc = $tw.utils.parseJSONSafe(title);
|
||||||
if(point) {
|
if(fc) {
|
||||||
points.push(point)
|
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) {
|
if(featureCollection.features.length > 0) {
|
||||||
return [JSON.stringify(turf.nearestPoint(target,turf.featureCollection(points)))];
|
return [JSON.stringify(turf.nearestPoint(target,featureCollection))];
|
||||||
} else {
|
} else {
|
||||||
return [];
|
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…
x
Reference in New Issue
Block a user