newPoints = {
let newFeaturesBBox = [];
let newFeaturesPoints = [];
const spacing = .001;
function generateBBox(coords, n, spacing) {
const offset = (Math.ceil(Math.sqrt(n)) / 2) * spacing;
const bbox = [
coords[0] - offset,
coords[1] - offset,
coords[0] + offset,
coords[1] + offset
];
return bbox;
}
for (const el of points.features) {
const count = el.properties.n;
const coords = [...el.geometry.coordinates];
const props = Object.assign({}, el.properties);
let newFeature;
if (count > 1) {
const bbox = generateBBox([...el.geometry.coordinates], count, spacing);
const points = turf.randomPoint(count, { bbox: bbox });
newFeaturesBBox.push(turf.bboxPolygon(bbox));
points.features = points.features.map(d => ((d.properties = props), d));
newFeaturesPoints = [...newFeaturesPoints, ...points.features];
} else {
newFeaturesPoints.push(turf.point(coords, props));
}
}
return [
turf.featureCollection(newFeaturesBBox),
turf.featureCollection(newFeaturesPoints)
];
}