From bb5489b31e4bb73f23c4ef5bb32a7d1750ed1689 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Mon, 6 Mar 2023 14:06:16 +0000 Subject: [PATCH] Add geolookup operator --- .../geospatial/demo/cities/NewYork.tid | 8 +++ .../geospatial/demo/ui/geomarker.tid | 15 +++++ .../tiddlywiki/geospatial/operators/lookup.js | 45 ++++++++++++++ .../geospatial/tests/operators/geolookup.tid | 60 +++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid create mode 100644 plugins/tiddlywiki/geospatial/operators/lookup.js create mode 100644 plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid diff --git a/plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid b/plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid new file mode 100644 index 000000000..6c532d2a4 --- /dev/null +++ b/plugins/tiddlywiki/geospatial/demo/cities/NewYork.tid @@ -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! diff --git a/plugins/tiddlywiki/geospatial/demo/ui/geomarker.tid b/plugins/tiddlywiki/geospatial/demo/ui/geomarker.tid index 7ad8fe339..847721d1e 100644 --- a/plugins/tiddlywiki/geospatial/demo/ui/geomarker.tid +++ b/plugins/tiddlywiki/geospatial/demo/ui/geomarker.tid @@ -87,6 +87,21 @@ title: $:/plugins/tiddlywiki/geospatial/demo/ui/geomarker +!! GeoLayer Lookups + +<$let + thisLocation={{{ [geopoint{!!long},{!!lat}] }}} +> + + + !! Travel Time <$button actions=<>> diff --git a/plugins/tiddlywiki/geospatial/operators/lookup.js b/plugins/tiddlywiki/geospatial/operators/lookup.js new file mode 100644 index 000000000..59ca131e7 --- /dev/null +++ b/plugins/tiddlywiki/geospatial/operators/lookup.js @@ -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; +} + +})(); diff --git a/plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid b/plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid new file mode 100644 index 000000000..0bbd9eb09 --- /dev/null +++ b/plugins/tiddlywiki/geospatial/tests/operators/geolookup.tid @@ -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 + +

boxfish

\ No newline at end of file