question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

turf.area gives incorrect result

See original GitHub issue

Please provide the following when reporting an issue:

I’m trying to find the area of the same polygon, via Turf and PostGIS. I Get varying outputs from both.

Turf gives: 2822907.8214477254 while PostGIS gives 2816602.27320234 for spherical area

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Scoppiocommented, Jul 28, 2022

Turfjs Area is wrong to the point that it is actively causing harm in multiple applications where I work and we are having to delegate area calculation to other libraries. The area calculation seems very close to the result given by projecting to ESRI 54009, but still, the precision fluctuates alot.

1reaction
dylelcommented, Aug 24, 2022

I needed this area calculation to be the same as QGIS/MSQL/POSTGRES for work so I’ve tried a bunch of things and eventually found this post:

Post About Postgres

Which references this library geographiclib-js.

Appears to be written by the author referenced in the code ( Charles Karney) to this article/document article

So temporarily i’m using this


// similar to turf, summing up rings (i.e. multipolygons)
// this may not be necessary if the geographic polygon can support multipolygons
function polygonArea(coords: number[][][][]) {
  let total = 0;
  if (coords && coords.length > 0) {
    for (let i = 0; i < coords.length; i++) {
      const area = geographicLibArea(coords[i][0]);
      total += area;
    }
  }
  return total;
}

const geographicLibArea = (coordinates: Array<Coord>) => {
// the geodesic format of my lat, lng coordinates
// you can specify your own if you have the appropriate constants
  const geod = GeographicLib.Geodesic.WGS84;


// build polygon in geographic
  let poly = geod.Polygon(false);
  for (let i = 0; i < coordinates.length; ++i) {
    poly.AddPoint(coordinates[i][1], coordinates[i][0]);
  }

  poly = poly.Compute(false, true);

  return Math.abs(poly.area.toFixed(0));
};

Docs can be found here: GeographicLib Js Docs

It would be great if we could get the formula derivatives implemented in turf. I’ve followed several algorithms before (shoelace, gauss, vincenty) but having a bit of trouble with the referenced document in terms of the formulas actually needed to get this done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Turf.js length gives incorrect result - GIS Stack Exchange
I'm trying to calculate the distance between 2 points given by GPS coordinates using the turf.js library.
Read more >
Turf.js and PostGIS 'difference' function giving unexpected result
1 Answer 1 · I found that turf/truncate still gave me the odd northmost & southmost point. I ended up doing a string...
Read more >
7 Problems With Artificial Grass and How to Solve Them
Improper installation is the most common culprit for holes and tears in your turf. Water buildup from a lack of drainage, noticeable seams, ......
Read more >
18 Common Mistakes DIY Turf Installers Make - MPR Supply
Failure to remove soft spots may potentially result in certain areas of your lawn sinking. Solution: Remove any soft spots during the excavation...
Read more >
18 Artificial Grass Installation Mistakes (and How to Solve Them)
After excavating your existing lawn, check the subgrade/soil for soft spots. Soft spots are areas in which the ground begins to sink under...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found