计算地图上一块区域的面积
在地图上唯一不变的坐标是经纬度这里用经纬度来计算。
保存经纬度的结构体:
struct EarthPos
{
EncsPos():L(0.0), B(0.0), H(0.0){}
EncsPos(double l, double b, double h=0.0):
L(l), B(b), H(h){}
double L; // 经度 度.度
double B; // 纬度 度.度
double H; // 高程 度.度
};
计算函数:
double PolarTriangleArea(float tan1, float lng1, float tan2, float lng2)
{
float deltaLng = lng1 - lng2;
float t = tan1 * tan2;
return 2 * atan2(t * sin(deltaLng), 1 + t * cos(deltaLng));
}
//函数接受一个指定区域坐标点的集合
double GetArea(std::vector<EarthPos> &vecPoint)
{
if (vecPoint.size() < 3)
{
return 0.0;
}
float radius = 6371009;
float total = 0;
EncsPos prev = vecPoint[vecPoint.size() - 1]; //获取最后一个
float prevTanLat = tan(((PI / 2 - prev.B / 180 * PI) / 2));
float prevLng = (prev.L) / 180 * PI;
std::vector<EarthPos>::iterator iterPoint = vecPoint.begin();
for (; iterPoint != vecPoint.end(); iterPoint++)
{
float tanLat = tan((PI / 2 - (iterPoint->B) / 180 * PI) / 2);
float lng = (iterPoint->L) / 180 * PI;
total += PolarTriangleArea(tanLat, lng, prevTanLat, prevLng);
prevTanLat = tanLat;
prevLng = lng;
}
return abs(total * (radius * radius)) / 1000000;
}
GetArea 函数接受一个指定区域的集合,返回的单位是平方千米(km²)。