public class PolygonArea extends Object
This computes the area of a geodesic polygon using the method given Section 6 of
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 and Description |
|---|
PolygonArea(Geodesic earth,
boolean polyline)
Constructor for PolygonArea.
|
| Modifier and Type | Method and Description |
|---|---|
void |
AddEdge(double azi,
double s)
Add an edge to the polygon or polyline.
|
void |
AddPoint(double lat,
double lon)
Add a point to the polygon or polyline.
|
void |
Clear()
Clear PolygonArea, allowing a new polygon to be started.
|
PolygonResult |
Compute()
Return the results so far.
|
PolygonResult |
Compute(boolean reverse,
boolean sign)
Return the results so far.
|
Pair |
CurrentPoint()
Report the previous vertex added to the polygon or polyline.
|
double |
Flattening() |
double |
MajorRadius() |
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.
|
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.
|
public PolygonArea(Geodesic earth, boolean polyline)
earth - the Geodesic object to use for geodesic calculations.polyline - if true that treat the points as defining a polyline
instead of a polygon.public void Clear()
public void AddPoint(double lat,
double lon)
lat - the latitude of the point (degrees).lon - the latitude of the point (degrees).
lat should be in the range [−90°, 90°].
public void AddEdge(double azi,
double s)
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.
public PolygonResult Compute()
Counter-clockwise traversal counts as a positive area.
public PolygonResult Compute(boolean reverse, boolean sign)
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.public PolygonResult TestPoint(double lat, double lon, boolean reverse, boolean sign)
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.lat should be in the range [−90°, 90°].
public PolygonResult TestEdge(double azi, double s, boolean reverse, boolean sign)
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.public double MajorRadius()
public double Flattening()
public Pair CurrentPoint()
If no points have been added, then Double.NaN is returned. Otherwise, lon will be in the range [−180°, 180°).
Copyright © 2015. All Rights Reserved.