node-red-contrib-tak-registration
Version:
A Node-RED node to register to TAK and to help wrap files as datapackages to send to TAK
54 lines (47 loc) • 1.61 kB
JavaScript
var helpers = require('@turf/helpers');
// http://stackoverflow.com/questions/11935175/sampling-a-random-subset-from-an-array
/**
* Takes a {@link FeatureCollection} and returns a FeatureCollection with given number of {@link Feature|features} at random.
*
* @name sample
* @param {FeatureCollection} featurecollection set of input features
* @param {number} num number of features to select
* @returns {FeatureCollection} a FeatureCollection with `n` features
* @example
* var points = turf.randomPoint(100, {bbox: [-80, 30, -60, 60]});
*
* var sample = turf.sample(points, 5);
*
* //addToMap
* var addToMap = [points, sample]
* turf.featureEach(sample, function (currentFeature) {
* currentFeature.properties['marker-size'] = 'large';
* currentFeature.properties['marker-color'] = '#000';
* });
*/
function sample(featurecollection, num) {
if (!featurecollection) throw new Error("featurecollection is required");
if (num === null || num === undefined) throw new Error("num is required");
if (typeof num !== "number") throw new Error("num must be a number");
var outFC = helpers.featureCollection(
getRandomSubarray(featurecollection.features, num)
);
return outFC;
}
function getRandomSubarray(arr, size) {
var shuffled = arr.slice(0),
i = arr.length,
min = i - size,
temp,
index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
module.exports = sample;
module.exports.default = sample;
;