Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript
https://turfjs.org/
MIT License
9.16k stars 934 forks source link

Calculate area with higher accuracy #2643

Closed vTechGIS closed 2 weeks ago

vTechGIS commented 1 month ago

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 commented 1 month ago

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:

I did a little digging on the turf side:

Related issues that provide some insight:

Armer7 commented 1 month ago

@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 commented 1 month ago

@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 commented 3 weeks ago

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