2

I have a geojson file defined, and I am checking to see whether a coordinate is in the given feature inside that geojson file. No matter what coordinate I plug in to the d3.geocontains() function, inRegion is always set to true, when obviously coordinates outside of the polygon should cause the function to return false.

geo.geojson file:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
    "name": "test_region_name"
    },
      "geometry": {
        "coordinates": [
          [
            [
              -73.96200080026726,
              40.82690844987039
            ],
            [
              -74.02402960789588,
              40.823413089420626
            ],
            [
              -74.07813984433737,
              40.760465102484545
            ],
            [
              -74.08143924899831,
              40.6679333560935
            ],
            [
              -73.95606187187775,
              40.66843387335888
            ],
            [
              -73.87357675535053,
              40.70896329528995
            ],
            [
              -73.89733246891002,
              40.79394487450159
            ],
            [
              -73.96200080026726,
              40.82690844987039
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}

app.js file:

const d3 = require('d3-geo'); // this is version 2.0.2
const fs = require('fs');

async function test() {
    const coordinate = [40.73496919065889,-73.97519841891166] // regardless of the coordinate, the d3.geoContains() call always returns true...
    fs.readFile("./geo.geojson", function(err, data) {
        if (err) throw err;
        const geojson = JSON.parse(data);
        for (const feature of geojson.features) {
            const inRegion = d3.geoContains(feature, coordinate)
            console.log(`coordinate is in region ${feature.properties.name}? ${inRegion}`)
        }
    });
}

test()
nickcoding2
  • 142
  • 1
  • 8
  • 34

0 Answers0