I have a dataset of earthquakes of my country but it doesn’t have references about where was the epicenter.
So I decided to compute the centroid of each county (called district in Perú) that is located near the shore and then compute which location has the closest distance to the epicenter.
But the centroid function and other close variations are not working with the features i have. Could be something related to the projection?. What am I doing wrong?
Here is the notebook https://observablehq.com/@ccalobeto/peru-earthquakes-1960-2024
districts = {
const topo = topojson.feature(pe, pe.objects.level4)
topo.features.forEach(feature => {
feature.centroid = centroid(feature)
feature.centroid1 = d3.geoCentroid(feature)
feature.centroid2 = projection(d3.geoCentroid(feature))
return feature
})
return topo
}
centroid = (feature) => {
const geometry = feature.geometry;
if (geometry.type === "Polygon"){
return d3.geoCentroid(feature);
}
else {
let largestPolygon = {}, largestArea = 0;
geometry.coordinates.forEach(coordinates => {
const polygon = { type: "Polygon", coordinates },
area = d3.geoArea(polygon);
if (area > largestArea) {
largestPolygon = polygon;
largestArea = area;
}
});
return d3.geoCentroid(largestPolygon);
}
}