将经度转换为米 [英] transform longitude latitude into meters

查看:349
本文介绍了将经度转换为米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个将gps位置映射到x/y值的函数,如下所示:

I need a function that maps gps positions to x/y values like this:

getXYpos(GeoPoint relativeNullPoint, GeoPoint p){
   deltaLatitude=p.latitude-relativeNullPoint.latitude;
   deltaLongitude=p.longitude-relativeNullPoint.longitude;
   ...
   resultX=latitude (or west to east) distance in meters from p to relativeNullPoint
   resultY=longitude (or south to north) distance in meters from p to relativeNullPoint
}

我已经看到了两个geoPoint的距离"的一些实现,但是它们都只是计算航空距离. 我认为deltaLongitude可以直接转换为米,但是deltaLatitude取决于经度.有谁知道如何解决这个问题?

i have seen some implementations of "distance of two geoPoints" but they all just calculate the air-line distance. i think the deltaLongitude can be transformed into meters directly but the deltaLatitude depends in the Longitude. does anyone know how this problem can be solved?

推荐答案

首先,我认为您有 latitude 经度颠倒了.经度为X,纬度为Y.

To start with, I think you have your latitude and longitude reversed. Longitude measures X, and latitude measures Y.

纬度很容易变成南北距离.我们知道360度是两极围绕地球的一个完整圆,并且距离是40008000米.只要您不需要考虑由于地球不是完美球形而引起的误差,公式就是deltaLatitude * 40008000 / 360.

The latitude is easy to turn into a north-south distance. We know that 360 degrees is a full circle around the earth through the poles, and that distance is 40008000 meters. As long as you don't need to account for the errors due to the earth being not perfectly spherical, the formula is deltaLatitude * 40008000 / 360.

您怀疑,棘手的部分是将经度转换为X.由于它取决于纬度,因此您需要确定要使用的纬度-您可以选择起点的纬度,目的地的纬度或两者之间的任意点.赤道的周长(纬度0)为40075160米.在给定纬度下,圆的周长与余弦成正比,因此公式为deltaLongitude * 40075160 * cos(latitude) / 360.

The tricky part is converting longitude to X, as you suspected. Since it depends on the latitude you need to decide which latitude you're going to use - you could choose the latitude of your origin, the latitude of your destination, or some arbitrary point in between. The circumference at the equator (latitude 0) is 40075160 meters. The circumference of a circle at a given latitude will be proportional to the cosine, so the formula will be deltaLongitude * 40075160 * cos(latitude) / 360.

编辑:您的评论表明您对经度公式有一些麻烦;您可能在调用cos时使用度数而不是弧度,这是一个常见的菜鸟错误.为了确保没有歧义,这是Python中的工作代码.

Your comment indicates you had some trouble with the longitude formula; you might have used degrees instead of radians in the call to cos, that's a common rookie mistake. To make sure there's no ambiguity, here's working code in Python.

def asRadians(degrees):
    return degrees * pi / 180

def getXYpos(relativeNullPoint, p):
    """ Calculates X and Y distances in meters.
    """
    deltaLatitude = p.latitude - relativeNullPoint.latitude
    deltaLongitude = p.longitude - relativeNullPoint.longitude
    latitudeCircumference = 40075160 * cos(asRadians(relativeNullPoint.latitude))
    resultX = deltaLongitude * latitudeCircumference / 360
    resultY = deltaLatitude * 40008000 / 360
    return resultX, resultY

我选择对X计算使用relativeNullPoint纬度.这样做的好处是,如果转换具有相同经度的多个点,它们将具有相同的X;否则,它们将具有相同的X.南北线将是垂直的.

I chose to use the relativeNullPoint latitude for the X calculation. This has the benefit that if you convert multiple points with the same longitude, they'll have the same X; north-south lines will be vertical.

再次我应该指出,这是一个非常简单的公式,您应该知道其局限性.显然地球不是平坦的,因此将其映射到XY坐标的任何尝试都会涉及一些折衷.当您要转换的区域足够小以至于可以认为是平坦的,并且可以忽略南北线的轻微曲率和不平行性时,上面得出的公式最有效.映射投影是一门完整的科学.如果您希望看到一些可能性,那么维基百科就是一个不错的起点.这种特定的投影称为等角投影,并增加了一些缩放比例.

Edit again: I should have pointed out that this is a very simple formula and you should know its limitations. Obviously the earth is not flat, so any attempt to map it to XY coordinates will involve some compromises. The formula I derived above works best when the area you're converting is small enough to consider flat, and where the slight curvature and non-parallelism of north-south lines can be ignored. There's a whole science to map projections; if you want to see some possibilities a good place to start would be Wikipedia. This specific projection is known as the Equirectangular projection, with some added scaling.

这篇关于将经度转换为米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆