turfjs / turf-inside Goto Github PK
View Code? Open in Web Editor NEWChecks to see if a point is inside of a polygon.
License: MIT License
Checks to see if a point is inside of a polygon.
License: MIT License
Do a bbox intersect check first before running the full intersection check.
The linked gist shows a polygon and a point outside of it.
But turf.inside returns true when called with the point and polygon
gist:
http://geojson.io/#id=gist:kujosHeist/1030e392bd751daf5d9af57aa412a49c&map=3/46.80/-22.76
sample code:
var turf = require('turf')
var pt1 = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-9.9964077,
53.8040989
]
}
};
var poly = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[5.080336744095521,67.89398938540765],
[0.35070899909145403,69.32470003971179],
[-24.453622256504122,41.146696777884564],
[-21.6445524714804,40.43225902006474],
[5.080336744095521,67.89398938540765]
]
]
}
};
var result = turf.inside(pt1, poly);
console.log(result);
I'm seeing inconsistent results at different points on the boundary of this rectangular polygon
var ul = [-79.4586181640625,39.53793974517629]
var ur = [ul[0] + 0.03, ul[1]]
var ll = [ul[0], ul[1] + 0.03]
var lr = [ll[0] + 0.03, ll[1]]
var ml = [ul[0], ul[1]*0.5 + ll[1]*0.5]
var mr = [ur[0], ul[1]*0.5 + ll[1]*0.5]
var um = [ul[0]*0.5 + ur[0]*.5, ul[1]]
var lm = [ll[0]*0.5 + lr[0]*.5, ul[1]]
var points = [ul, ur, ll, lr, ml, mr, um, lm].map(function (p) {
return {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": p
}
}
});
var poly = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[ul, ur, lr, ll, ul]]},
"properties":{}};
points.forEach(function (point) {
point.properties['marker-color'] = turf.inside(point, poly) ? "#0f0" : "#f00"
})
var features = {
"type": "FeatureCollection",
"features": [poly].concat(points)
};
Either that's the expected behavior, or it's a bug. If the first, then maybe mention that in the README?
Replacement link is broken.
https://github.com/Turfjs/turf/tree/master/packages/turf-inside
is a github 404.
Maybe the new replacement is
http://turfjs.org/docs#pointsWithinPolygon ?
Point:
{
"type": "Feature",
"properties": {
},
"geometry": {
"type": "Point",
"coordinates": [
-122.2171193047933,
37.047091805854045
]
}
}
Polygon:
{
"type": "Feature",
"properties": {
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.218341126668,
37.0465514120195
],
[
-122.21875363261,
37.0469941356116
],
[
-122.217833796929,
37.0475452075565
],
[
-122.21742563268,
37.0471058303351
],
[
-122.218341126668,
37.0465514120195
]
],
[
[
-122.222392869885,
37.0432084718519
],
[
-122.221690878603,
37.0438739119672
],
[
-122.220524064154,
37.0442681419687
],
[
-122.219978075999,
37.0445589586444
],
[
-122.219844303242,
37.0446221968967
],
[
-122.219321062266,
37.0448796850055
],
[
-122.218826884815,
37.045122604153
],
[
-122.218573784174,
37.0453666868006
],
[
-122.218268215349,
37.0456303902646
],
[
-122.217780502062,
37.0461559809545
],
[
-122.217557000305,
37.046409050564
],
[
-122.216913331727,
37.0468002588239
],
[
-122.216745578174,
37.0469205472114
],
[
-122.216640591301,
37.047214285367
],
[
-122.21654999832,
37.0478801293361
],
[
-122.21650507808,
37.048229539295
],
[
-122.216380564567,
37.0486979420522
],
[
-122.217363654324,
37.0502905626941
],
[
-122.218710906301,
37.0500680820453
],
[
-122.219204626212,
37.0495471137844
],
[
-122.219489441802,
37.0491470408239
],
[
-122.219641764977,
37.0488667388367
],
[
-122.219976037302,
37.0488288247378
],
[
-122.220333106698,
37.0485030894839
],
[
-122.220576834413,
37.0483628263685
],
[
-122.220746569922,
37.0480728397139
],
[
-122.221032669268,
37.0477292988667
],
[
-122.221105185636,
37.0475585660009
],
[
-122.221577592597,
37.0473913642988
],
[
-122.221874356937,
37.0470005341446
],
[
-122.222183163405,
37.0468781571541
],
[
-122.22256780049,
37.0469855908012
],
[
-122.22254151,
37.0466089509962
],
[
-122.222095326478,
37.0461253988294
],
[
-122.222028403299,
37.0460274154958
],
[
-122.221983160475,
37.0457113557735
],
[
-122.221919745882,
37.0452981913118
],
[
-122.222410976167,
37.0452771424834
],
[
-122.222348091616,
37.045098982908
],
[
-122.222568365286,
37.0444500732181
],
[
-122.222637880315,
37.0438929311031
],
[
-122.222630847749,
37.0435866993828
],
[
-122.222590931289,
37.0433846365526
],
[
-122.222392869885,
37.0432084718519
]
]
]
}
}
Actual result: false
Expected result: true
E.g. use following polygon:
[[179, -1],[-179, -1],[-179, 1],[179, 1],[179, -1]]
and test for inclusion of points:
[179.5, 0.5], [-179.5, -0.5]
they lie within the polygon but are not detected as such.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.