Turfjs / turf

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

turf.area gives incorrect result #1366

Closed devdattaT closed 6 years ago

devdattaT commented 6 years ago

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

rowanwins commented 6 years ago

Hi @devdattaT

Yeah it doesn't surprise me that you get fractionally different areas. It'll depend on what projection PostGIS is calculating area on, as well as things like the radius of earth that PostGIS uses. You can see the reference for our calculation here.

Cheers

rowanwins commented 6 years ago

Closing as I don't think it's really a bug and haven't heard back, but feel free to reopen if required

perliedman commented 4 years ago

Hi, sorry to bring this up, but I have a similar problem but with much larger difference between Turf and PostGIS. The example polygon is from turf's example polygon for the area function.

Turf:

area({ type: 'Polygon', coordinates: [[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]})

Gives 3339946239196.927

PostGIS:

select ST_Area(ST_GeomFromGeoJSON('{ "type": "Polygon", "coordinates": [[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]}'), false) as area;

Gives 3715352131501.34

The PostGIS result is about 11% larger than turf's, which is well beyond what I would expect from differences in constants like earth radius etc.

chingchai commented 3 years ago

Hi, I have a similar to the above issue for calculate area of polygon in square meters. I compared the results of the calculations between Turf , PostGIS and QGIS. The calculation results obtained from Turf are not equal to PostGIS and QGIS. Turf: 17285.5369 PostGIS: 17164.6452 QGIS: 17164.6452

Scoppio commented 2 years ago

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.

stebogit commented 2 years ago

@Scoppio what alternative library are you using? Would you please post some specific examples showing the different results? Also, do you know what exact earth's radius value is used by that library?

dylel commented 2 years ago

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.

grinvaldsjanis commented 11 months ago

In my project where areas are less than 1ha, it gives about 40% larger area. I found snippet in java, tried it and it works.

MohamedBenTaher commented 10 months ago

calculation here

same issue here , but still no solution

razzdasari commented 10 months ago

Use

turf.area(plygon_trf)*0.993699583 I got exact value matching with QGIS Area