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

getAreaOfPolygon returns a number when I pass an unclosed area #302

Open
kasir-barati opened this issue Jul 14, 2023 · 2 comments
Open

getAreaOfPolygon returns a number when I pass an unclosed area #302

kasir-barati opened this issue Jul 14, 2023 · 2 comments

Comments

@kasir-barati
Copy link

kasir-barati commented Jul 14, 2023

Hi dear reader, When I execute this code IDK why but for some reason for the second one also returns a number though it is not even a closed area. So my question is that if this is an expected behaviour and OK. BTW based on my understanding it should not work like that, I mean it is not even a closed are to begin with:

const geolib = require("geolib");

// 1
console.log(
  geolib.getAreaOfPolygon([
    [7.453635617650258, 51.49320556213869],
    [7.454583481047989, 51.49328893754685],
    [7.454778172179346, 51.49240881084831],
    [7.453832678225655, 51.49231619246726],
    [7.453635617650258, 51.49320556213869],
  ])
);

// 2
console.log(
  geolib.getAreaOfPolygon([
    {
      latitude: -72.2781577,
      longitude: 42.9288483,
    },
    {
      latitude: -72.2864358,
      longitude: 42.928252,
    },
    {
      latitude: -72.2877233,
      longitude: 42.9247012,
    },
    {
      latitude: -72.2817152,
      longitude: 42.9237898,
    },
  ])
);

Here is what I meant by not closed area, as you can see in the uploaded image the starting point - the first point on the right top corner - is not even close to the ending point -- the last point in the bottom right corner:
image

Here is my work env conf:

geolib: 3.3.4
node: 20.3.1
npm: 9.6.7
OS: Darwin Kernel Version 22.5.0 arm64
@kasir-barati
Copy link
Author

Can you please let me know if this is a bug or not dear @manuelbieh? IMO it is but still it is a good idea to be sure 😅

@kasir-barati
Copy link
Author

kasir-barati commented Sep 4, 2023

OK, I am coming from the future. In this part of code we are closing it

for (let i = 0; i < points.length; i++) {
if (i === points.length - 2) {
lowerIndex = points.length - 2;
middleIndex = points.length - 1;
upperIndex = 0;
} else if (i === points.length - 1) {
lowerIndex = points.length - 1;
middleIndex = 0;
upperIndex = 1;
} else {
lowerIndex = i;
middleIndex = i + 1;
upperIndex = i + 2;
}
const p1lon = getLongitude(points[lowerIndex]);
const p2lat = getLatitude(points[middleIndex]);
const p3lon = getLongitude(points[upperIndex]);
area += (toRad(p3lon) - toRad(p1lon)) * Math.sin(toRad(p2lat));
}

Though IMO this is not OK, and also the documentation lacks clarity about these kind of underlying implementations.

I hope someone will just address this GitHub issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant