Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isPointInside fails on polygon that transits the anti-meridian #38

Open
rbarkas opened this issue Aug 11, 2014 · 3 comments
Open

isPointInside fails on polygon that transits the anti-meridian #38

rbarkas opened this issue Aug 11, 2014 · 3 comments

Comments

@rbarkas
Copy link

rbarkas commented Aug 11, 2014

isPointInside does not appear to work correctly when the polygon straddles the anti-meridian.
I maintain a system with lots of polygons that do straddle the antimeridian (I'm in Australia :-) so it's important to me.

see the test code below that illustrates the problem when it's run:

var geolib = require('geolib');

// -------------
// Test 1 This succeeds
// -------------
var polygon = [
    {latitude: -46, longitude: 1},
    {latitude: -46, longitude: 2},
    {latitude: -47, longitude: 2},
    {latitude: -47, longitude: 1}
];
console.log('Test 1: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:-46.5, longitude:1.5}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:-47.9, longitude:1.2}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:-46.8, longitude:1.1}, polygon))
console.log('------------');

// -------------
// Test 2 This succeeds
// -------------
var polygon = [
    {latitude: 10, longitude: -10},
    {latitude: 10, longitude: 10},
    {latitude: 20, longitude: 10},
    {latitude: 20, longitude: -10}
];
console.log('Test 2: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-5}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-15}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-5}, polygon))

// ----------
// Test 3 : AntiMeridian 1: This fails
// ----------
var polygon = [
    {latitude: 10, longitude: 175},
    {latitude: 10, longitude: -175},
    {latitude: 20, longitude: -175},
    {latitude: 20, longitude: 175}
];
console.log('Test 3: Antimeridian 1: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:177}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-170}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-176}, polygon))
console.log('------------');

// ----------
// Test 4: Antimeridian 2: This fails
// ----------
var polygon = [
    {latitude: 10, longitude: -175},
    {latitude: 10, longitude: 175},
    {latitude: 20, longitude: 175},
    {latitude: 20, longitude: -175}
];
console.log('Test 4: Antimeridian 2: Polygon:',polygon);
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:177}, polygon))
console.log('should be false', geolib.isPointInside({ latitude:15, longitude:-170}, polygon))
console.log('should be true', geolib.isPointInside({ latitude:15, longitude:-176}, polygon))
console.log('------------');
@manuelbieh
Copy link
Owner

To be honest: I currently have no idea how to fix this 😕

@mixmix
Copy link

mixmix commented May 18, 2017

Can confirm this is a problem, I just had a painful experience on this too.
My crappy solution was an interface warning !

selection_280

Possible (ugly) solution : if the box straddles the anti-meridian, pretend the whole situation is on the other side of the globe? i.e. shuffle all the longitudes +180 degrees? (and then for any degrees over 180, map that to appropriate negative value e.g. 350 - 360 = -10 )

so mapping would be like :

Original Mapped
+170 170 + 180 = 350 = -10
-170 -170 + 180 = 10

@leandroz
Copy link

leandroz commented Aug 3, 2017

Same problem here :(, maybe checking Google Maps implementation? that one works as expected.

@manuelbieh manuelbieh added the v3 label May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants