# Mistake computing district centroids

**URL:** <https://talk.observablehq.com/t/mistake-computing-district-centroids/8932>\
**Category:** Help\
**Created:** [March 16, 2024, 2:30am UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932 "2024-03-16T02:30:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccalobeto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ccalobeto/32/8138_2.png) [@ccalobeto](https://talk.observablehq.com/u/ccalobeto)\
**Post date:** [March 16, 2024, 2:30am UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/1 "2024-03-16T02:30:29Z")

</div>

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](https://observablehq.com/@ccalobeto/peru-earthquakes-1960-2024)

```auto
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
}

```

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 16, 2024, 11:36am UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/2 "2024-03-16T11:36:27Z")

</div>

It seems that your notebook is still private?

---

<div class="post-metadata">

**Author:** ![ccalobeto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ccalobeto/32/8138_2.png) [@ccalobeto](https://talk.observablehq.com/u/ccalobeto)\
**Post date:** [March 16, 2024, 2:43pm UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/3 "2024-03-16T14:43:31Z")

</div>

It is ok now. Thanks @mootari

---

<div class="post-metadata">

**Author:** ![ccalobeto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ccalobeto/32/8138_2.png) [@ccalobeto](https://talk.observablehq.com/u/ccalobeto)\
**Post date:** [March 19, 2024, 10:25pm UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/4 "2024-03-19T22:25:42Z")

</div>

any suggestion of what is happening, please

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [March 20, 2024, 1:48am UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/5 "2024-03-20T01:48:58Z")

</div>

Can you elaborate on what you mean by “not working”? Are you getting an error? Is the computed centroid not in the place you expect? What do you expect or want to happen, and how is what is happening different? If you reduce the problem down to its essence it’ll be easier for us to help you.

---

<div class="post-metadata">

**Author:** ![ccalobeto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ccalobeto/32/8138_2.png) [@ccalobeto](https://talk.observablehq.com/u/ccalobeto)\
**Post date:** [March 20, 2024, 5:17pm UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/6 "2024-03-20T17:17:22Z")

</div>

I am sorry Mike. The centroids are not in the place I expect.  
Here is more detail of what I found

- The projection I used is d3.geoIdentity().reflectY(true) and there is no problem with this projection since the data points of earthquakes are well displayed.
- The problem are the centroid computations of each district (level4 in the cartography) with d3.geoCentroid(feature). They throw centroid coordinates in Malasia, but should be in Perú.
- Just in case I tried projection.invert and throws centroid coordinates in Ecuador.

obs. The Peru’s bounding box is : Lon: [-87.382, -65.624]; lat: [-1.396, -25.701]

[Perú Cartography](https://cdn.jsdelivr.net/npm/latam-atlas@0.0.4/files/peru-100k.json)

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [March 20, 2024, 8:19pm UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/7 "2024-03-20T20:19:12Z")

</div>

Try this: [Geo: Rewind / Fil | Observable](https://observablehq.com/@fil/rewind)

It will cure polygons that might be defined with the opposite winding order to what geoCentroid expects.

---

<div class="post-metadata">

**Author:** ![ccalobeto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ccalobeto/32/8138_2.png) [@ccalobeto](https://talk.observablehq.com/u/ccalobeto)\
**Post date:** [March 21, 2024, 1:01pm UTC](https://talk.observablehq.com/t/mistake-computing-district-centroids/8932/8 "2024-03-21T13:01:41Z")

</div>

Cool! Thanks @Fil.
