Published
Edited
Oct 23, 2019
Insert cell
Insert cell
Insert cell
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)
];
}
Insert cell
newPoints[1].features.map(d => ({
X: d.geometry.coordinates[0],
Y: d.geometry.coordinates[1],
ZIP_CODE: d.properties.ZIP_CODE
}))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more