Given a series of GPS coordinate pairs, I need to calculate the area of a polygon (n-gon). This is relatively small (not larger than 50,000 sqft). The geocodes are created by applying an affine transform with data from a world file.
I have tried to use a two step approach by doing converting the geocodes to cartesian coordinates:
double xPos = (lon-lonAnchor)*( Math.toRadians( 6378137 ) )*Math.cos( latAnchor );
double yPos = (lat-latAnchor)*( Math.toRadians( 6378137 ) );
then I use a cross product calculation to determine the area.
The issue is that the results are a bit off in accuracy (around 1%). Is there anything I can look into to improve this?
Thanks.
I checked on internet for various polygon area formulas(or code) but did not find any one good or easy to implement.
Now I have written the code snippet to calculate area of a polygon drawn on earth surface. The polygon can have n vertices with each vertex has having its own latitude longitude.
Few Important Points
The output area will have unit of square metres
private static double CalculatePolygonArea(IList<MapPoint> coordinates)
{
double area = 0;
if (coordinates.Count > 2)
{
for (var i = 0; i < coordinates.Count - 1; i++)
{
MapPoint p1 = coordinates[i];
MapPoint p2 = coordinates[i + 1];
area += ConvertToRadian(p2.Longitude - p1.Longitude) * (2 + Math.Sin(ConvertToRadian(p1.Latitude)) + Math.Sin(ConvertToRadian(p2.Latitude)));
}
area = area * 6378137 * 6378137 / 2;
}
return Math.Abs(area);
}
private static double ConvertToRadian(double input)
{
return input * Math.PI / 180;
}
I am modifying a Google Map so that a user can calculate the area of a polygon by clicking the vertices. It wasn't giving correct areas until I made sure the Math.cos(latAnchor) was in radians first
So:
double xPos = (lon-lonAnchor)*( Math.toRadians( 6378137 ) )*Math.cos( latAnchor );
became:
double xPos = (lon-lonAnchor)*( 6378137*PI/180 ) )*Math.cos( latAnchor*PI/180 );
where lon, lonAnchor and latAnchor are in degrees. Works like a charm now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With