Find if a coordinate point is within a topojson feature, geograpic area, using d3js

785 views Asked by At

I need to find if a geographical point is within a given feature. I need to find this before projection.

In my application I have a set of countries and I want to take the coordinates of the user and see which country those coordinates are within.

I am working through the lets make a map tutorial http://bost.ocks.org/mike/map/ and have code similar to the styling polygons section

1

There are 1 answers

2
bowcot84 On

This might be a good starting point. From my bookmarks of using something similar a number of years ago to calculate point in polygon for Google Maps.

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
function isPointInPoly(poly, pt){
    for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
        && (c = !c);
    return c;
}