What is @turf/sample?
@turf/sample is a module within the Turf.js library, which is a collection of geospatial analysis tools written in JavaScript. The @turf/sample package specifically allows users to sample a specified number of points from a given set of features, such as polygons or lines, within a GeoJSON object.
Random Point Sampling
This feature allows you to randomly sample a specified number of points from a set of points. In this example, 100 random points are generated within the given bounding box, and then 10 points are sampled from these.
const turf = require('@turf/turf');
const sample = require('@turf/sample');
const points = turf.randomPoint(100, {bbox: [-180, -90, 180, 90]});
const sampledPoints = sample(points, 10);
console.log(sampledPoints);
Sampling from Polygons
This feature allows you to sample points from within a polygon. In this example, a polygon is defined, 100 random points are generated within the bounding box of the polygon, and then 10 points are sampled from these.
const turf = require('@turf/turf');
const sample = require('@turf/sample');
const polygon = turf.polygon([[
[-5, 52], [-4, 52], [-4, 53], [-5, 53], [-5, 52]
]]);
const points = turf.randomPoint(100, {bbox: [-5, 52, -4, 53]});
const sampledPoints = sample(points, 10);
console.log(sampledPoints);