2

So, I've got a method that returns the area of a shape defined by its points (given in CCW or CW order, it doesn't really matter). I tested it with some shapes and it seems to be working.

The problem is, I want to use this method with GPS coordinates, and I want to return the result in m² or km², but that's definitly not what happens. In fact, I don't even know in which unit the result is when I use this method with that kind of coordinates.

So the question is, how to convert the result I have into m² or km² ? I tried some things, but either it does not work or it's inaccurate.

Here's my method, if you want to check :

public static double getArea(List<Vector2D> points) {

    double firstSum = 0, secondSum = 0;

    for (int i = 0 ; i < points.size()-1 ; i++) {
        firstSum += points.get(i).x * points.get(i+1).y;
        secondSum += points.get(i).y * points.get(i+1).x;
    }
    firstSum += points.get( points.size()-1 ).x * points.get(0).y;
    secondSum += points.get( points.size()-1 ).y * points.get(0).x;

    return Math.abs((firstSum-secondSum)/2);

}

(Vector2D is the class I use for points, with x as the latitude and y as the longitude)

Emix
  • 23
  • 5
  • @AndyTurner Are you sure it's a duplicate? The "duplicate" is about [tag:google-maps]. – lexicore Apr 05 '18 at 07:48
  • If you want a result in m² or km² you first have to convert your GPS coordinates (which are probably lon/lat) to some metric coordinate reference system. – lexicore Apr 05 '18 at 07:50
  • Oh, you're right, sorry. I'll remove this question. Thanks by the way ! – Emix Apr 05 '18 at 07:50
  • I think the question is OK. – lexicore Apr 05 '18 at 07:51
  • Is it still a duplicate, so ? – Emix Apr 05 '18 at 07:53
  • The [previous duplicate question](https://stackoverflow.com/questions/36022883/calculate-the-area-of-a-polygon-with-latitude-and-longitude) contains code to calculate the area of a polygon in lat/lng. It's not in Java, but it's hardly a stretch to convert it. Several other questions on other stackexchange sites can be found easily by searching on your favorite search engine. – Andy Turner Apr 05 '18 at 07:53
  • @AndyTurner *"The [previous duplicate question](https://stackoverflow.com/questions/36022883/calculate-the-area-of-a-polygon-with-latitude-and-longitude) contains code to calculate the area of a polygon in lat/lng."* - that code is basically `GpsHelper.CalculatePolygonArea(poly)` which you can't really convert to Java without diving into the implementation of `GpsHelper.CalculatePolygonArea`. This is why I object it being a duplicate. – lexicore Apr 05 '18 at 08:09
  • @lexicore "which you can't really convert to Java without diving into the implementation" the implementation that's right there, you mean? Just change the types, change the trig methods to the `java.lang.Math` equivalents... it's really not hard. – Andy Turner Apr 05 '18 at 08:23

1 Answers1

1

The problem is that you're not taking the (approximately) spherical nature of the Earth into account. For a start, you need to take into account the radius of the Earth - you could have the same list of latitude and longitude on a smaller (or bigger) planet, but the radius would be different, and consequently the area would be different too.

You can use the approach in this question. It's trivial to convert that to Java:

public static double CalculatePolygonArea(List<Vector2D> coordinates)
{
    double area = 0;

    if (coordinates.size() > 2)
    {
        for (int i = 0; i < coordinates.size()-1; i++)
        {
            Vector2D p1, p2;
            p1 = coordinates.get(i);
            p2 = coordinates.get(i + 1);
            area += Math.toRadians(p2.x - p1.x) * (2 + Math.sin(Math.toRadians(p1.y))
               + Math.sin(Math.toRadians(p2.y)));

        }
        area = area * R * R / 2;
    }

    return Math.abs(area);
}

(assuming Vector2D.x is the longitude and Vector2D.y is the latitude).

R is the radius of the Earth. Use a value in the unit you want the area result to be in (e.g. 6_371_000 metres for square metres, 6_371 km for square km, 3_959 miles for square miles...)

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Yes, I've been doing this since you mentionned the other question, I'm currently testing this with my values – Emix Apr 05 '18 at 08:34