使用python将纬度和经度转换为x和y网格系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24617013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:52:50  来源:igfitidea点击:

convert latitude and longitude to x and y grid system using python

pythonlatitude-longitudecoordinate-systems

提问by learner

I have file with latitude and longitude values, that i want to convert the x and y in km I want to measure the distance from each point.

我有纬度和经度值的文件,我想以公里为单位转换 x 和 y 我想测量每个点的距离。

for instance I make the first points of latitude and longitude(which are 51.58, -124.6 respectfully)

例如我制作纬度和经度的第一个点(分别为51.58,-124.6)

to (0,0) in my x and y system so than basically i want to find out what the other points are and their location from the origin so i want to find what 51.56(lat) -123.64(long) is in (x,y) in km and so on for the rest of the file.

到我的 x 和 y 系统中的 (0,0) 所以基本上我想找出其他点是什么以及它们从原点的位置所以我想找到 51.56(lat) -123.64(long) 在 (x ,y) 在 km 等文件的其余部分。

I want to do this all in python, is there some sort code ?

我想在 python 中完成这一切,是否有某种代码?

I found sites online , for instance

例如,我在网上找到了网站

http://www.whoi.edu/marine/ndsf/cgi-bin/NDSFutility.cgi?form=0&from=LatLon&to=XY

http://www.whoi.edu/marine/ndsf/cgi-bin/NDSFutility.cgi?form=0&from=LatLon&to=XY

does exactly want i want to do, I just don't know how they do it.

确实是我想做的,我只是不知道他们是怎么做的。

采纳答案by Floris

The following gets you pretty close (answer in km). If you need to be better than this, you have to work harder at the math - for example by following some of the links given.

以下让您非常接近(以公里为单位回答)。如果你需要比这更好,你必须在数学上更加努力 - 例如通过遵循一些给定的链接。

import math
dx = (lon1-lon2)*40000*math.cos((lat1+lat2)*math.pi/360)/360
dy = (lat1-lat2)*40000/360

Variable names should be pretty obvious. This gives you

变量名应该很明显。这给你

dx = 66.299 km (your link gives 66.577)
dy = 2.222 km (link gives 2.225)

Once you pick coordinates (for example, lon1, lat1) as your origin, it should be easy to see how to compute all the other XY coordinates.

一旦您选择坐标(例如,lon1, lat1)作为原点,就应该很容易了解如何计算所有其他 XY 坐标。

Note - the factor 40,000 is the circumference of the earth in km (measured across the poles). This gets you close. If you look at the source of the link you provided (you have to dig around a bit to find the javascript which is in a separate file) you find that they use a more complex equation:

注意 - 因子 40,000 是以公里为单位的地球周长(跨两极测量)。这让你接近。如果您查看您提供的链接的来源(您必须仔细研究才能找到位于单独文件中javascript),您会发现它们使用了一个更复杂的等式:

function METERS_DEGLON(x)
{  
   with (Math)
   {
      var d2r=DEG_TO_RADIANS(x);
      return((111415.13 * cos(d2r))- (94.55 * cos(3.0*d2r)) + (0.12 * cos(5.0*d2r)));
   }
}

function METERS_DEGLAT(x)
{
   with (Math)
   {
      var d2r=DEG_TO_RADIANS(x);
      return(111132.09 - (566.05 * cos(2.0*d2r))+ (1.20 * cos(4.0*d2r)) - (0.002 * cos(6.0*d2r)));
   }
}

It looks to me like they are actually taking account of the fact that the earth is not exactly a sphere... but even so when you are making the assumption you can treat a bit of the earth as a plane you are going to have some errors. I'm sure with their formulas the errors are smaller...

在我看来,他们实际上是在考虑到地球不完全是一个球体的事实……但即便如此,当您做出假设时,您可以将地球的一部分视为一个平面,您将拥有一些错误。我相信他们的公式误差更小......

回答by dabhaid

You can get the distance between GPS points using the Great Circle Distance formula. Latitude and longitude are in an geodectic coordinate system, so you can't just convert to a flat 2D grid and use euclidean distances. You canconvert sufficiently close points to an approximate grid by taking an arbitrary point like your (X,Y), setting it to the origin (like you've done) and then using great circle distance together with bearingto plot the points relative to each other on the plane, but it's an approximation.

您可以使用大圆距离公式获得 GPS 点之间的距离。纬度和经度位于测地坐标系中,因此您不能仅转换为平面 2D 网格并使用欧几里得距离。您可以通过获取像 (X,Y) 这样的任意点,将其设置为原点(就像您所做的那样),然后使用大圆距离和方位角来绘制相对于彼此在飞机上,但这是一个近似值。

回答by ScottO

UTM projections are in meters. So you could use something like the utm lib at this link:

UTM 投影以米为单位。所以你可以在这个链接上使用类似 utm lib 的东西:

https://pypi.python.org/pypi/utm

https://pypi.python.org/pypi/utm

Googling python lat lon to UTM will point to several options.

谷歌搜索 python lat lon 到 UTM 将指向几个选项。

UTM zones are 6 degrees of longitude wide and start from 0 at the prime meridian. The origin of each UTM zone is on the equator (x-axis) with the y-axis at the western most degree of longitude. This makes the grid positive to the north and east. You could calculate your distance from these results. Values are most accurate in the middle of the UTM zone.

UTM 区域的宽度为 6 度经度,从本初子午线的 0 开始。每个 UTM 区域的原点位于赤道(x 轴)上,y 轴位于最西经度。这使得网格向北和向东为正。您可以根据这些结果计算距离。UTM 区域中间的值最准确。

You also should know what datum your original lat lon values are based on and use the same datum in your conversion.

您还应该知道原始 lat lon 值基于什么基准,并在转换中使用相同的基准。

回答by qrtLs

if you were to use a 3D system, these functions will do:

如果您要使用 3D 系统,这些功能将执行以下操作:

def arc_to_deg(arc):
    """convert spherical arc length [m] to great circle distance [deg]"""
    return float(arc)/6371/1000 * 180/math.pi

def deg_to_arc(deg):
    """convert great circle distance [deg] to spherical arc length [m]"""
    return float(deg)*6371*1000 * math.pi/180

def latlon_to_xyz(lat,lon):
    """Convert angluar to cartesian coordiantes

    latitude is the 90deg - zenith angle in range [-90;90]
    lonitude is the azimuthal angle in range [-180;180] 
    """
    r = 6371 # https://en.wikipedia.org/wiki/Earth_radius
    theta = math.pi/2 - math.radians(lat) 
    phi = math.radians(lon)
    x = r * math.sin(theta) * math.cos(phi) # bronstein (3.381a)
    y = r * math.sin(theta) * math.sin(phi)
    z = r * math.cos(theta)
    return [x,y,z]

def xyz_to_latlon (x,y,z):
    """Convert cartesian to angular lat/lon coordiantes"""
    r = math.sqrt(x**2 + y**2 + z**2)
    theta = math.asin(z/r) # https://stackoverflow.com/a/1185413/4933053
    phi = math.atan2(y,x)
    lat = math.degrees(theta)
    lon = math.degrees(phi)
    return [lat,lon]