三角测量公式(3个参考+距离)

时间:2015-04-03 11:46:34

标签: c# algorithm triangulation

我正在尝试实现一个函数,该函数将给出GEO位置(Lat,Long),给定3个GEO参考点和距离每个点的半径。

我正在寻找的功能的签名是:

public static GeoLocation Triangle(GeoLocation pos1, double r1, GeoLocation pos2,
                                   double r2, GeoLocation pos3, double r3)

例如,3个朋友在某处秘密见面。每个人只能告诉我他/她住在哪里(GeoLocation = lat,long)以及他们与家人会面的距离(r =半径)。鉴于3个这样的参考点(来自所有3个朋友),我应该有足够的信息来计算这个秘密会面点作为GeoLocation。

此问题非常类似于移动/塔式问题,您可以通过测量来自少数塔的各个信号强度来对移动设备进行三角测量。

我已经尝试在线找到公式很长一段时间了,这就是为什么我在Stack Overflow上发布我的问题。

如果你能帮我填写公式(三角法),我将不胜感激。谢谢。

我到目前为止的代码:

public class GeoLocation
{
    private double _latitude;
    private double _longitude;

    public GeoLocation(double latitude, double longitude)
    {
        this._latitude = latitude;
        this._longitude = longitude;
    }

    //Tested and working!
    public double DistanceToKm(GeoLocation loc)
    {
        double lat1, lon1, lat2, lon2;
        lat1 = this._latitude;
        lon1 = this._longitude;
        lat2 = loc._latitude;
        lon2 = loc._longitude;
        var R = 6371; // Radius of the earth in km
        var dLat = deg2rad(lat2 - lat1); // deg2rad below
        var dLon = deg2rad(lon2 - lon1);
        var a =
            Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(deg2rad(lat1))*Math.Cos( deg2rad(lat2))*
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
            ;
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var d = R*c; // Distance in km
        return d;
    }
}

我认为不需要的代码,但代码是值得的:

public static Coords ToCoord(GeoLocation pos)
{
    var x = Math.Cos(pos._longitude) * Math.Cos(pos._latitude);
    var y = Math.Sin( pos._longitude) * Math.Cos(pos._latitude);
    var z = Math.Sin(pos._latitude);
    return new Coords(x,y,z);
}

class Coords
{
    public double x;
    public double y;
    public double z;

    public Coords(double x, double y, double z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

1 个答案:

答案 0 :(得分:0)

似乎这是解决方案。

https://gis.stackexchange.com/questions/66/trilateration-using-3-latitude-and-longitude-points-and-3-distances

......远比学校几何学@DrKoch

复杂得多

这是Python解决方案:

yC = earthR *(math.cos(math.radians(LatC)) * math.sin(math.radians(LonC)))
zC = earthR *(math.sin(math.radians(LatC)))

P1 = array([xA, yA, zA])
P2 = array([xB, yB, zB])
P3 = array([xC, yC, zC])

#from wikipedia
#transform to get circle 1 at origin
#transform to get circle 2 on x axis
ex = (P2 - P1)/(numpy.linalg.norm(P2 - P1))
i = dot(ex, P3 - P1)
ey = (P3 - P1 - i*ex)/(numpy.linalg.norm(P3 - P1 - i*ex))
ez = numpy.cross(ex,ey)
d = numpy.linalg.norm(P2 - P1)
j = dot(ey, P3 - P1)

#from wikipedia
#plug and chug using above values
x = (pow(DistA,2) - pow(DistB,2) + pow(d,2))/(2*d)
y = ((pow(DistA,2) - pow(DistC,2) + pow(i,2) + pow(j,2))/(2*j)) - ((i/j)*x)

# only one case shown here
z = sqrt(pow(DistA,2) - pow(x,2) - pow(y,2))

#triPt is an array with ECEF x,y,z of trilateration point
triPt = P1 + x*ex + y*ey + z*ez

#convert back to lat/long from ECEF
#convert to degrees
lat = math.degrees(math.asin(triPt[2] / earthR))
lon = math.degrees(math.atan2(triPt[1],triPt[0]))

print lat, lon`