# retrieve countyid from coordinates

**URL:** <https://talk.observablehq.com/t/retrieve-countyid-from-coordinates/8427>\
**Category:** Help\
**Created:** [October 27, 2023, 11:50pm UTC](https://talk.observablehq.com/t/retrieve-countyid-from-coordinates/8427 "2023-10-27T23:50:29Z")\
**Posts on this page:** 3\
**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:** [October 27, 2023, 11:50pm UTC](https://talk.observablehq.com/t/retrieve-countyid-from-coordinates/8427/1 "2023-10-27T23:50:29Z")

</div>

Hi,  
I am calibrating a function **getCountyId** that receives coordinates and features as parameters, it returns levels id from Ecuador.  
Here is the code

> function getCountyId(features, point) {  
> const n = features.length  
> if (!!point) {  
> for (let i = 0; i \< n; i++) {  
> const coordinates = features[i].geometry.coordinates[0]  
> if (d3.polygonContains(coordinates, point)) {  
> return features[i].id  
> }  
> }  
> return NaN  
> }  
> }

The function is working around 80% and still has problems with some provinces from Ecuador.  
Here is the status of the number of coordinates that the function doesn’t identify.  
Map(6) {“ESMERALDAS” =\> 59, “GUAYAS” =\> 501, “EL ORO” =\> 10, “MANABI” =\> 15, “AZUAY” =\> 2, “GALAPAGOS” =\> 1}

Any suggestion would be eternally appreciated.

Notebook is here  
[Missed in Ecuador](https://observablehq.com/d/81bb51a9defcbc7e)

---

<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:** [October 30, 2023, 8:52am UTC](https://talk.observablehq.com/t/retrieve-countyid-from-coordinates/8427/2 "2023-10-30T08:52:06Z")

</div>

Here’s an approach that seems to solve 99% of the points, by taking into account the multipolygons:

```auto
function getCountyId(features, point) {
  // ref https://stackoverflow.com/questions/71272770/using-d3-to-retrieve-a-topojson-u-s-county-from-coordinates
  const n = features.length;
  if (!!point) {
    for (let i = 0; i < n; i++) {
      const geometry = features[i].geometry;
      // treat Polygon as MultiPolygon
      for (const coordinates of geometry.type === "Polygon"
        ? [geometry.coordinates]
        : geometry.coordinates) {
        if (d3.polygonContains(coordinates[0], point)) {
          return features[i].id;
        }
      }
    }
    return NaN;
  }
}

```

It’s a bit slow, but this is a different question 🙂

---

<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:** [October 30, 2023, 3:48pm UTC](https://talk.observablehq.com/t/retrieve-countyid-from-coordinates/8427/3 "2023-10-30T15:48:09Z")

</div>

Thanks, Philipe
