1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 01:20:30 +00:00

Add new geounion/geointersect/geodifference operators

This commit is contained in:
jeremy@jermolene.com 2023-01-06 10:38:12 +00:00
parent 94cf1bbe0d
commit 0aa3be0488

View File

@ -0,0 +1,88 @@
/*\
title: $:/plugins/tiddlywiki/geospatial/operators/transformation.js
type: application/javascript
module-type: filteroperator
Filter operators for geospatial transformation
\*/
(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.geounion = makeTransformation("union");
exports.geointersect = makeTransformation("intersect");
exports.geodifference = makeTransformation("difference");
function makeTransformation(methodName) {
return function(source,operator,options) {
// Collect the input
var jsonObjects = [];
source(function(tiddler,title) {
var jsonObject = $tw.utils.parseJSONSafe(title,null);
if(jsonObject) {
jsonObjects.push(jsonObject)
}
});
// Perform the transformation
var result = geojsonOp(jsonObjects,methodName);
return [JSON.stringify(result)];
};
}
function geojsonOp(geojsonObjects, op) {
var resultFeatures = [];
$tw.utils.each(geojsonObjects,function (geojson1) {
if(geojson1.type === "FeatureCollection") {
resultFeatures = resultFeatures.length ? resultFeatures : geojson1.features;
} else if(geojson1.type === "Feature") {
resultFeatures = resultFeatures.length ? resultFeatures : [geojson1];
}
$tw.utils.each(geojsonObjects,function (geojson2) {
if(geojson1 !== geojson2) {
var newResultFeatures = [];
$tw.utils.each(resultFeatures,function (feature1) {
if(geojson2.type === "FeatureCollection") {
$tw.utils.each(geojson2.features,function (feature2) {
var result;
if(op === "union") {
result = turf.union(feature1, feature2);
} else if(op === "intersect") {
result = turf.intersect(feature1, feature2);
} else if(op === "difference") {
result = turf.difference(feature1, feature2);
}
if(result) {
newResultFeatures.push(result);
}
});
} else if(geojson2.type === "Feature") {
var result;
if(op === "union") {
result = turf.union(feature1, geojson2);
} else if(op === "intersect") {
result = turf.intersect(feature1, geojson2);
} else if(op === "difference") {
result = turf.difference(feature1, geojson2);
}
if(result) {
newResultFeatures.push(result);
}
}
});
resultFeatures = newResultFeatures;
}
});
});
return turf.featureCollection(resultFeatures);
}
})();