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.

Polygon area calculation doesn't match other tools like google earth engine or geojson.io

See original GitHub issue

Polygon area calculation doesn’t seem to match up with other similar tools such as Geojson.io or Google Earth Engine. For example:

coords = [(-97.59238135821987, 43.47456565304017),
 (-97.59244690469288, 43.47962399877412),
 (-97.59191951546768, 43.47962728271748),
 (-97.59185396090983, 43.47456565304017),
 (-97.59238135821987, 43.47456565304017)]

projection = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'), pyproj.Proj(init='epsg:3857'))
shapely.ops.transform(projection, shapely.geometry.Polygon(sample_coords)).area

yields an area of 45573.993884405005 m^2 while Google Earth Engine and Geojson.io report 23944.14737277293 and 23997.77, respectively.

The area in shapely is calculated here quoting a method whose link is broken, while the Geojson.io is calculated here via a method described on starting on page 3 of this document.

I’ve converted this briefly to python with the argument expecting the same coordinate scheme as above (list of coordinates):

def ringArea(coords):
    #var p1, p2, p3, lowerIndex, middleIndex, upperIndex, i,
    area = 0
    wgs84_radius = 6378137 # earth's equitorial radius in wgs84 = espg 4326
    coordsLength = len(coords)

    if coordsLength > 2:
        for i in range(coordsLength):
            if i == coordsLength - 2:
                lowerIndex = coordsLength - 2
                middleIndex = coordsLength - 1
                upperIndex = 0
            elif i == coordsLength - 1:
                lowerIndex = coordsLength - 1
                middleIndex = 0
                upperIndex = 1
            else:
                lowerIndex = i
                middleIndex = i + 1
                upperIndex = i + 2
                
            p1 = coords[lowerIndex]
            p2 = coords[middleIndex]
            p3 = coords[upperIndex]
            area += (np.deg2rad(p3[0]) - np.deg2rad(p1[0]) ) * np.sin(np.deg2rad(p2[1]))

        area = area * wgs84_radius * wgs84_radius / 2
    return area

This method returns an area of 23997.769250450423 m^2.

My main question is: Is the current method preferred for some reason? If so, can the link to the cited be fixed (i’m not sure where it’s supposed to go). If not, is it possible to conform (or add the option) to the method described int he JPL doc?

I’d be happy to contribute to this if the option for this other method would like to be added but I didn’t see any contributing guidelines.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
dnomadbcommented, Jun 5, 2019

As far as I understand, shapely assumes all points are cartesian, and calculates area in whatever coordinates it is given; in the example above wgs coordinates are converted to web mercator epsg3857 to calculate area, but web mercator is not an equal area projection.

If we substitute conversion to an equal area coordinate system such as world mollweide (https://spatialreference.org/ref/esri/54009/) , the area calculation is performed as expected:

coords = [(-97.59238135821987, 43.47456565304017),
 (-97.59244690469288, 43.47962399877412),
 (-97.59191951546768, 43.47962728271748),
 (-97.59185396090983, 43.47456565304017),
 (-97.59238135821987, 43.47456565304017)]

projection = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'), pyproj.Proj(init='esri:54009'))
shapely.ops.transform(projection, shapely.geometry.Polygon(coords)).area

Which gives 23997.775332830173.

We use this over in https://github.com/mapbox/fio-area. Hope this helps!

1reaction
brentonmallen1commented, Jun 7, 2019

In case anyone else comes across the above issue w.r.t. re-projecting to ESRI:54009, here is a solution https://github.com/pyproj4/pyproj/issues/341

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polygon area calculation doesn't match other tools like ...
Polygon area calculation doesn't seem to match up with other similar tools such as Geojson.io or Google Earth Engine.
Read more >
Shapely/PyProj area calculation doesn't match Google ...
When calculating the area for a polygon, the result using shapely/pyproj is drastically different when compared to google earth engine or ...
Read more >
Calculating the area of polygons in Google Earth Engine
This tutorial demonstrates polygon creation, perimeter and area calculations, and visualization using the JavaScript interface to Google ...
Read more >
Uncertainty in estimating a polygon area in GEE
I have 2 methods to compute the area of a polygon: Using the dedicated method: ee.Feature.area(); Summing the area of each pixels of...
Read more >
Searching for Imagery with Data API - Planet Developer Center
Overview. This API Quickstart guide will walk you through how to search for imagery using the Planet Data API.
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