mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-11 18:00:26 +00:00
Add geolookup operator
This commit is contained in:
parent
17ebeaf725
commit
bb5489b31e
8
plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid
Normal file
8
plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid
Normal file
@ -0,0 +1,8 @@
|
||||
title: $:/plugins/tiddlywiki/geospatial/demo/cities/NewYork
|
||||
tags: $:/tags/GeoMarker
|
||||
caption: New York
|
||||
lat: 40.712778
|
||||
long: -74.006111
|
||||
alt: 0
|
||||
|
||||
This is New York!
|
@ -87,6 +87,21 @@ title: $:/plugins/tiddlywiki/geospatial/demo/ui/geomarker
|
||||
</ul>
|
||||
</$let>
|
||||
|
||||
!! GeoLayer Lookups
|
||||
|
||||
<$let
|
||||
thisLocation={{{ [geopoint{!!long},{!!lat}] }}}
|
||||
>
|
||||
<ul>
|
||||
<$list filter="[all[shadows+tiddlers]tag[$:/tags/GeoLayer]sort[caption]]">
|
||||
<li>
|
||||
<$text text={{{ [<currentTiddler>get[caption]] :else[<currentTiddler>] }}}/> --
|
||||
<$text text={{{ [<thisLocation>geolookup{!!text}] }}}/>
|
||||
</li>
|
||||
</$list>
|
||||
</ul>
|
||||
</$let>
|
||||
|
||||
!! Travel Time
|
||||
|
||||
<$button actions=<<get-traveltime-actions>>>
|
||||
|
45
plugins/tiddlywiki/geospatial/operators/lookup.js
Normal file
45
plugins/tiddlywiki/geospatial/operators/lookup.js
Normal file
@ -0,0 +1,45 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/geospatial/operators/lookup.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Filter operators for geospatial lookup
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var turf = require("$:/plugins/tiddlywiki/geospatial/turf.js"),
|
||||
geotools = require("$:/plugins/tiddlywiki/geospatial/geotools.js");
|
||||
|
||||
exports.geolookup = function(source,operator,options) {
|
||||
// Get the GeoJSON object
|
||||
var output = [],
|
||||
jsonObject = $tw.utils.parseJSONSafe(operator.operands[0],null);
|
||||
if(jsonObject) {
|
||||
// Process the input points
|
||||
source(function(tiddler,title) {
|
||||
var point = geotools.parsePoint(title),
|
||||
result = getPolygonsContainingPoint(jsonObject,point);
|
||||
output.push(JSON.stringify(result))
|
||||
});
|
||||
}
|
||||
// Perform the transformation
|
||||
return output;
|
||||
};
|
||||
|
||||
function getPolygonsContainingPoint(featureCollection,point) {
|
||||
// Filter the GeoJSON feature collection to only include polygon features containing the point
|
||||
const properties = [];
|
||||
turf.featureEach(featureCollection,function(feature) {
|
||||
if(feature.geometry.type === "Polygon" && turf.booleanPointInPolygon(point,feature)) {
|
||||
properties.push(feature.properties);
|
||||
}
|
||||
});
|
||||
return properties;
|
||||
}
|
||||
|
||||
})();
|
60
plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid
Normal file
60
plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid
Normal file
@ -0,0 +1,60 @@
|
||||
title: $:/plugins/tiddlywiki/geospatial/tests/geolookup
|
||||
description: geolookup operator
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\whitespace trim
|
||||
<$text text={{{ [geopoint[100.5],[0.5]geolookup{TestData}jsonget[0],[animal]] }}}/>
|
||||
+
|
||||
title: TestData
|
||||
type: application/json
|
||||
|
||||
{
|
||||
"type": "FeatureCollection",
|
||||
"features": [
|
||||
{
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "Point",
|
||||
"coordinates": [102.0,0.5]
|
||||
},
|
||||
"properties": {
|
||||
"animal": "amoeba"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "LineString",
|
||||
"coordinates": [
|
||||
[102.0,0.0],[103.0,1.0],[104.0,0.0],[105.0,1.0]
|
||||
]
|
||||
},
|
||||
"properties": {
|
||||
"animal": "snake",
|
||||
"length": 100.0
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "Polygon",
|
||||
"coordinates": [
|
||||
[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]
|
||||
]
|
||||
},
|
||||
"properties": {
|
||||
"animal": "boxfish",
|
||||
"prop1": {
|
||||
"this": "that"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
<p>boxfish</p>
|
Loading…
Reference in New Issue
Block a user