用经度和纬度计算多边形的面积 [英] Calculate the area of a polygon with latitude and longitude
问题描述
我有这样的代码,用这样写的: source1 和这个:来源2 a>
public static double CalculatePolygonArea(IList< GpsLocation>坐标)
{
double area = 0 ;
if(coordinates.Count> 2)
{
for(var i = 0; i< coordinates.Count-1; i ++)
{
GpsLocation p1,p2;
p1 = coordinates [i];
p2 =坐标[i + 1];
area + = ToRad(p2.Longitude - p1.Longitude)*(2 + Math.Sin(ToRad(p1.Latitude))
+ Math.Sin(ToRad(p2.Latitude)));
面积=面积* R * R / 2;
}
}
return Math.Abs(area);
}
以下是我的测试代码:
[Fact]
public void GpsPolygonAreaTest()
{
var poly = new List< GpsLocation>();
var p1 = new GpsLocation(0,0);
poly.Add(p1);
var p2 = GpsHelper.CreateLocationBasedOnBearingDistance(p1,5,100);
poly.Add(p2);
var p3 = GpsHelper.CreateLocationBasedOnBearingDistance(p2,95,100);
poly.Add(p3);
var p4 = GpsHelper.CreateLocationBasedOnBearingDistance(p3,185,100);
poly.Add(p4);
poly.Add(p1);
var area = GpsHelper.CalculatePolygonArea(poly);
area.Should()。Be(10000);
}
我确认我的多边形是100米x 100米,请参阅图片: 任何想法我的代码有什么问题? 应放置在顶点循环之后,而不是在每次迭代中重复。 I have this code, written using this: source1 and this: source 2 Here is my test code: I confirmed that my polygon is 100m x 100m, see image:
My test result is: Expected value to be 10000, but found 1.28153883377486E+48. Any ideas what wrong with my code? I'm pretty sure this statement: should be placed after the loop over the vertices, rather than repeated on each iteration. 这篇关于用经度和纬度计算多边形的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
area = area * R * R / 2;
public static double CalculatePolygonArea(IList<GpsLocation> coordinates)
{
double area = 0;
if (coordinates.Count > 2)
{
for (var i = 0; i < coordinates.Count-1; i++)
{
GpsLocation p1, p2;
p1 = coordinates[i];
p2 = coordinates[i + 1];
area += ToRad(p2.Longitude - p1.Longitude) * (2 + Math.Sin(ToRad(p1.Latitude))
+ Math.Sin(ToRad(p2.Latitude)));
area = area * R * R / 2;
}
}
return Math.Abs(area);
}
[Fact]
public void GpsPolygonAreaTest()
{
var poly = new List<GpsLocation>();
var p1 = new GpsLocation(0, 0);
poly.Add(p1);
var p2 = GpsHelper.CreateLocationBasedOnBearingDistance(p1, 5, 100);
poly.Add(p2);
var p3 = GpsHelper.CreateLocationBasedOnBearingDistance(p2, 95, 100);
poly.Add(p3);
var p4 = GpsHelper.CreateLocationBasedOnBearingDistance(p3, 185, 100);
poly.Add(p4);
poly.Add(p1);
var area = GpsHelper.CalculatePolygonArea(poly);
area.Should().Be(10000);
}
area = area * R * R / 2;