Hii I have a geoJSON
file of an area
(COLORADA) USA
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-109.0283203125,
37.020098201368114
],
[
-102.06298828125,
37.020098201368114
],
[
-102.06298828125,
40.9964840143779
],
[
-109.0283203125,
40.9964840143779
],
[
-109.0283203125,
37.020098201368114
]
]
]
}
}
]
}
and a location
-105.8544607,39.1160152
and I have many geoJSON files of different regions so I want to know the location from the received Condinates
My question
How can I validate that a location (-105.8544607,39.1160152
) exist in the polygon or not ?
While Colorado is a trivial case (just compare the East/West corners to your points longitude and North/South with the latitude) I assume that you want to be able to handle non-square states too.
I would use the turf.js
library, in particular the BooleanContains
method:
var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
var point = turf.point([1, 2]);
turf.booleanContains(line, point);
Or possibly booleanPointInPolygon
:
var pt = turf.point([-77, 44]);
var poly = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
turf.booleanPointInPolygon(pt, poly);
I can't see much difference but it probably depends on where your data is coming from and if it is a geometry or a feature.