Class PolygonArea


  • public class PolygonArea
    extends Object
    Polygon areas.

    This computes the area of a geodesic polygon using the method given Section 6 of

    Arbitrarily complex polygons are allowed. In the case self-intersecting of polygons the area is accumulated "algebraically", e.g., the areas of the 2 loops in a figure-8 polygon will partially cancel.

    This class lets you add vertices one at a time to the polygon. The area and perimeter are accumulated at two times the standard floating point precision to guard against the loss of accuracy with many-sided polygons. At any point you can ask for the perimeter and area so far. There's an option to treat the points as defining a polyline instead of a polygon; in that case, only the perimeter is computed.

    Example of use:

     
     // Compute the area of a geodesic polygon.
    
     // This program reads lines with lat, lon for each vertex of a polygon.
     // At the end of input, the program prints the number of vertices,
     // the perimeter of the polygon and its area (for the WGS84 ellipsoid).
    
     import java.util.*;
     import net.sf.geographiclib.*;
    
     public class Planimeter {
       public static void main(String[] args) {
         PolygonArea p = new PolygonArea(Geodesic.WGS84, false);
         try {
           Scanner in = new Scanner(System.in);
           while (true) {
             double lat = in.nextDouble(), lon = in.nextDouble();
             p.AddPoint(lat, lon);
           }
         }
         catch (Exception e) {}
         PolygonResult r = p.Compute();
         System.out.println(r.num + " " + r.perimeter + " " + r.area);
       }
     }
    • Constructor Detail

      • PolygonArea

        public PolygonArea​(Geodesic earth,
                           boolean polyline)
        Constructor for PolygonArea.

        Parameters:
        earth - the Geodesic object to use for geodesic calculations.
        polyline - if true that treat the points as defining a polyline instead of a polygon.
    • Method Detail

      • Clear

        public void Clear()
        Clear PolygonArea, allowing a new polygon to be started.
      • AddPoint

        public void AddPoint​(double lat,
                             double lon)
        Add a point to the polygon or polyline.

        Parameters:
        lat - the latitude of the point (degrees).
        lon - the latitude of the point (degrees).

        lat should be in the range [−90°, 90°].

      • AddEdge

        public void AddEdge​(double azi,
                            double s)
        Add an edge to the polygon or polyline.

        Parameters:
        azi - azimuth at current point (degrees).
        s - distance from current point to next point (meters).

        This does nothing if no points have been added yet. Use PolygonArea.CurrentPoint to determine the position of the new vertex.

      • Compute

        public PolygonResult Compute()
        Return the results so far.

        Returns:
        PolygonResult(num, perimeter, area) where num is the number of vertices, perimeter is the perimeter of the polygon or the length of the polyline (meters), and area is the area of the polygon (meters2) or Double.NaN of polyline is true in the constructor.

        Counter-clockwise traversal counts as a positive area.

      • Compute

        public PolygonResult Compute​(boolean reverse,
                                     boolean sign)
        Return the results so far.

        Parameters:
        reverse - if true then clockwise (instead of counter-clockwise) traversal counts as a positive area.
        sign - if true then return a signed result for the area if the polygon is traversed in the "wrong" direction instead of returning the area for the rest of the earth.
        Returns:
        PolygonResult(num, perimeter, area) where num is the number of vertices, perimeter is the perimeter of the polygon or the length of the polyline (meters), and area is the area of the polygon (meters2) or Double.NaN of polyline is true in the constructor.

        More points can be added to the polygon after this call.

      • TestPoint

        public PolygonResult TestPoint​(double lat,
                                       double lon,
                                       boolean reverse,
                                       boolean sign)
        Return the results assuming a tentative final test point is added; however, the data for the test point is not saved. This lets you report a running result for the perimeter and area as the user moves the mouse cursor. Ordinary floating point arithmetic is used to accumulate the data for the test point; thus the area and perimeter returned are less accurate than if AddPoint and Compute are used.

        Parameters:
        lat - the latitude of the test point (degrees).
        lon - the longitude of the test point (degrees).
        reverse - if true then clockwise (instead of counter-clockwise) traversal counts as a positive area.
        sign - if true then return a signed result for the area if the polygon is traversed in the "wrong" direction instead of returning the area for the rest of the earth.
        Returns:
        PolygonResult(num, perimeter, area) where num is the number of vertices, perimeter is the perimeter of the polygon or the length of the polyline (meters), and area is the area of the polygon (meters2) or Double.NaN of polyline is true in the constructor.

        lat should be in the range [−90°, 90°].

      • TestEdge

        public PolygonResult TestEdge​(double azi,
                                      double s,
                                      boolean reverse,
                                      boolean sign)
        Return the results assuming a tentative final test point is added via an azimuth and distance; however, the data for the test point is not saved. This lets you report a running result for the perimeter and area as the user moves the mouse cursor. Ordinary floating point arithmetic is used to accumulate the data for the test point; thus the area and perimeter returned are less accurate than if AddPoint and Compute are used.

        Parameters:
        azi - azimuth at current point (degrees).
        s - distance from current point to final test point (meters).
        reverse - if true then clockwise (instead of counter-clockwise) traversal counts as a positive area.
        sign - if true then return a signed result for the area if the polygon is traversed in the "wrong" direction instead of returning the area for the rest of the earth.
        Returns:
        PolygonResult(num, perimeter, area) where num is the number of vertices, perimeter is the perimeter of the polygon or the length of the polyline (meters), and area is the area of the polygon (meters2) or Double.NaN of polyline is true in the constructor.
      • EquatorialRadius

        public double EquatorialRadius()
        Returns:
        a the equatorial radius of the ellipsoid (meters). This is the value inherited from the Geodesic object used in the constructor.
      • Flattening

        public double Flattening()
        Returns:
        f the flattening of the ellipsoid. This is the value inherited from the Geodesic object used in the constructor.
      • CurrentPoint

        public Pair CurrentPoint()
        Report the previous vertex added to the polygon or polyline.

        Returns:
        Pair(lat, lon), the current latitude and longitude.

        If no points have been added, then Double.NaN is returned. Otherwise, lon will be in the range [−180°, 180°].