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

Calculate area with higher accuracy #2643

Closed
vTechGIS opened this issue Jul 7, 2024 · 4 comments · Fixed by #2683
Closed

Calculate area with higher accuracy #2643

vTechGIS opened this issue Jul 7, 2024 · 4 comments · Fixed by #2683

Comments

@vTechGIS
Copy link

vTechGIS commented Jul 7, 2024

I use the latest version of Turf to calculate the area of ​​a geojson polygon.
The area result from Turf is: 22.13 m2 and Geographic Lib (https://geographiclib.sourceforge.io/) 22.06 m2. I also checked on QGIS the area result is 22.03 m2

So Geographic Lib has higher accuracy than Turf Lib. Can you improve this issue?

Thank you so much.

Geojson data source:
{"type":"Polygon","coordinates":[[[106.66416522963165,14.579907369919486],[106.6641609900968,14.579919026608949],[106.66415731659645,14.579932022843553],[106.66414116820758,14.579927094033418],[106.66413907381092,14.579900138182057],[106.66411935271576,14.579868331778414],[106.66412579474931,14.5798490375216],[106.66414456560182,14.579868433595195],[106.66415768266069,14.579875838613062],[106.66416266097694,14.579878985318168],[106.66416175432456,14.579893036877686],[106.66416522963165,14.579907369919486]]]}

@twelch
Copy link
Collaborator

twelch commented Jul 7, 2024

Thanks @vTechGIS. I think this type of request has come up in different forms in the past. I think that Turf does not try to perfectly match QGIS, or its underlying GEOS library, or any other 3rd party, as the source of truth. I think Turf methods were chosen for speed and accuracy, and possibly even simplicity. And any differences between libraries may be within the bounds of error of the approximations they are making. I think this could be documented better.

That said, the questions that come to mind for me is:

  • Why are they different?
  • Is Earth's shape being approximated differently?
  • Is the area calculation method different?
  • If they are different and QGIS method is actually more accurate, does it sacrifice speed to get it?
  • If they are the same, is there a difference in the precision of the calculations?

I did a little digging on the turf side:

Related issues that provide some insight:

@Armer7
Copy link

Armer7 commented Jul 8, 2024

@twelch Why don't you use the common WGS84/GRS80 standard for earth radius (6378137.0 m)? I have a large area calculation going on for agricultural plots. For the calculation on the server is done using postgis. The calculations are very different from what I get when calculating the area while editing the contour. The server calculates 139.29 ha (same values I got on QGIS), with turf I get an area of 138.8 ha, which is a very big difference for hectares.

@twelch
Copy link
Collaborator

twelch commented Jul 18, 2024

@Armer7 It looks like 6371008.8 is the mean radius

For Earth, the arithmetic mean radius is 6,371.0088 km (3,958.7613 mi)

And 6378137.0 is the equatorial radius.

The Earth's equatorial radius a, or semi-major axis,[8]: 11  is the distance from its center to the equator and equals 6,378.1370 km

It looks like mean radius was chosen by Turf, because it is seen as a better approximation of radius for the haversine distance formula, which is what turf.distance uses.

When applying these examples in real applications, it is better to use the mean earth radius, 6371 km. This value is recommended by the International Union of Geodesy and Geophysics and it minimizes the RMS relative error between the great circle and geodesic distance.

I can't tell you if the "better approximation" suggestion holds true for the geodesic area algorithm turf uses, but it's worth noting that OpenLayers also uses the mean radius value for all of the distance, area, length, etc. calculation. In addition, they support passing your own radius value. This would allow someone to match their library of choice if the same algorithm otherwise is used. There is in fact an open Turf ticked to support this.

#1176 seems like a reasonable way forward here. I think more convincing would be needed that switching to equatorial radius by default in turf, at least for turf.area, is the right choice.

I think Turf needs to better document the algorithms it uses for distance/area, the nature of them (geodesic), and the earth radius assumed. So that people can make a more informed decision.

@twelch
Copy link
Collaborator

twelch commented Aug 7, 2024

Here's the original paper for the area algorithm, took me a while to track it down - https://dataverse.jpl.nasa.gov/dataset.xhtml?persistentId=hdl:2014/41271. It doesn't specify a radius value to use

2007_ChamberlainDuquette_SomeAlgorithmsForPolygonsOnASphere.pdf

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

Successfully merging a pull request may close this issue.

4 participants