double calcpolygonarea(List<PointLatLngAlt> polygon)
{
// should be a closed polygon
// coords are in lat long
// need utm to calc area
if (polygon.Count == 0)
{
CustomMessageBox.Show("Please define a polygon!");
return 0;
}
// close the polygon
if (polygon[0] != polygon[polygon.Count - 1])
polygon.Add(polygon[0]); // make a full loop
CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory();
GeographicCoordinateSystem wgs84 = GeographicCoordinateSystem.WGS84;
int utmzone = (int)((polygon[0].Lng - -186.0) / 6.0);
IProjectedCoordinateSystem utm = ProjectedCoordinateSystem.WGS84_UTM(utmzone, polygon[0].Lat < 0 ? false : true);
ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(wgs84, utm);
double prod1 = 0;
double prod2 = 0;
for (int a = 0; a < (polygon.Count - 1); a++)
{
double[] pll1 = { polygon[a].Lng, polygon[a].Lat };
double[] pll2 = { polygon[a + 1].Lng, polygon[a + 1].Lat };
double[] p1 = trans.MathTransform.Transform(pll1);
double[] p2 = trans.MathTransform.Transform(pll2);
prod1 += p1[0] * p2[1];
prod2 += p1[1] * p2[0];
}
double answer = (prod1 - prod2) / 2;
if (polygon[0] == polygon[polygon.Count - 1])
polygon.RemoveAt(polygon.Count - 1); // unmake a full loop
return Math.Abs(answer);
}