1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2026-01-30 14:50:22 +00:00
Files
TiddlyWiki5/plugins/tiddlywiki/geospatial/operators/transformation.js
Mario Pietsch 8aa558eb2c Remove module function wrapper and add matching configurations for dprint and eslint (#7596)
* remove blks first try

* dprint.json seems to be OK, some forgotten functions

* add some more space-after-keyword settings

* server remove blks

* add **/files to dprint exclude

* dprint.js fixes a typo

* add boot.js and bootprefix.js to dprint exclude

* dprint change dprint.json

* add dprint fmt as script

* remove jslint comments

* fix whitespace

* fix whitespace

* remove function-wrapper from geospatial plugin

* fix whitespace

* add function wrapper to dyannotate-startup

* remove dpring.json
2025-03-21 17:22:57 +00:00

83 lines
2.4 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/geospatial/operators/transformation.js
type: application/javascript
module-type: filteroperator
Filter operators for geospatial transformation
\*/
"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);
}