将纬度/经度转换为X / Y坐标 [英] Convert Lat/Longs to X/Y Co-ordinates

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

问题描述

我有一个小区域的纬度/长度值在墨尔本; -37.803134,145.132377,还有一个从openstreet地图(Osmarender Image)导出的平面图像。
图片宽度:1018和高度:916

I have the Lat/Long value of an small area in Melbourne; -37.803134,145.132377 and also a flat image of that,that I exported from openstreet map( Osmarender Image ). Image width : 1018 and Height:916

我想要能够使用C ++将Lat / Long转换为X,Y坐标该点将反映位置。

I would like to be able to convert, using C++, the Lat/Long to an X,Y coordinate where the point would reflect the location.

我使用了我在web中找到的各种公式,如下所示,但没有任何帮助。

I used various formula's that I found in web like one given below but nothing helps.

var y = ((-1 * lat) + 90) * (MAP_HEIGHT / 180);
var x = (lon + 180) * (MAP_WIDTH / 360);

如果任何一个人给我明确的解释如何做这将是很大的帮助。

It would be of great help if any one give me clear explanation of how to do this . Any code would be much appreciated.

推荐答案

您需要更多的信息,而不只是一个纬度/经度对,才能做到这一点

You need more information than just a single lat/lon pair to be able to do this.

在这个阶段,您提供的信息缺少两件事:

At this stage, the information you have provided is missing two things:


  • 你的图像覆盖面积有多大(在纬度/经度方面)?根据您提供的内容,我不知道图片是否显示一米宽或一公里宽的区域。

  • 您的图片上的什么地方的参考坐标(-37.803134 ,145.132377)是它的角落之一吗?在中间某处?

我也假设你的图像是北/南对齐的 - 例如, t有北指向左上角。

I'm also going to assume that your image is aligned north/south - for example, it doesn't have north pointing towards the top left corner. That would tend to complicate things.

最简单的方法是确定哪些lat / lon坐标对应于(0,0)像素,以及(1017,915 )像素。然后,您可以通过插值法找出与给定纬度/经度坐标对应的像素。

The easiest approach is to figure out exactly what lat/lon coordinates correspond to the (0, 0) pixel and the (1017, 915) pixel. Then you can figure out the pixel corresponding to a given lat/lon coordinate through interpolation.

要简要说明这个过程,假设你的(-37.803134,145.132377)lat / lon对应于你的(0,0)像素,你发现你的(1017,915)到lat / lon(-37.798917,145.138535)。假设在左下角有像素(0,0)的通常惯例,这意味着北是在图像中。

To briefly outline that process, imagine that your (-37.803134, 145.132377) lat/lon corresponds to your (0, 0) pixel, and that you've discovered that your (1017, 915) pixel corresponds to the lat/lon (-37.798917, 145.138535). Assuming the usual convention with pixel (0, 0) in the bottom-left corner, this means north is up in the image.

然后,如果你对目标坐标(-37.801465,145.134984),你可以计算出相应的像素数量,如下:

Then, if you are interested in the target coordinate (-37.801465, 145.134984), you can figure out the corresponding number of pixels up the image it is as follows:

pixelY = ((targetLat - minLat) / (maxLat - minLat)) * (maxYPixel - minYPixel)
       = ((-37.801465 - -37.803134) / (-37.798917 - -37.803134)) * (915 - 0)
       = 362.138

也就是说,对应的像素距离图像底部362像素。

That is, the corresponding pixel is 362 pixels from the bottom of the image. You can then do exactly the same for the horizontal pixel placement, but using longitudes and X pixels instead.

部分((targetLat - minLat) /(maxLat - minLat))计算出你在两个参考坐标之间的距离,给出0表示你在第一个参考坐标,1表示第二个参考坐标,以指示其间的位置。因此,例如,它将产生0.25,表示您是两个参考坐标之间北方的25%。最后一位将其转换为等效像素。

The part ((targetLat - minLat) / (maxLat - minLat)) works out how far you are between the two reference coordinates, and gives 0 to indicate that you are at the first one, 1 to indicate the second one, and numbers in between to indicate locations in between. So for example, it would produce 0.25 to indicate that you are 25% of the way north between the two reference coordinates. The last bit converts that into the equivalent pixels.

HTH!

好的,根据你的评论,我可以更具体一点。鉴于您似乎使用左上角作为主要参考点,我将使用以下定义:

EDIT Ok, based on your comment I can be a little more specific. Given that you seem to be using the top left corner as your primary reference point, I'll use the following definitions:

minLat = -37.803134
maxLat = -37.806232
MAP_HEIGHT = 916

我们使用示例坐标(-37.804465,145.134984),相对于左上角的相应像素的Y坐标是:

Then, if we use an example coordinate of (-37.804465, 145.134984), the Y coordinate of the corresponding pixel, relative to the top-left corner, is:

pixelY = ((targetLat - minLat) / (maxLat - minLat)) * (MAP_HEIGHT - 1)
       = ((-37.804465 - -37.803134) / (-37.806232 - -37.803134)) * 915
       = 393.11

因此,对应的像素从顶部向下393像素。我会让你自己完成水平等效 - 它基本上是一样的。 注意 -1 MAP_HEIGHT 是因为如果您从零开始,像素数为915,而不是916.

Therefore, the corresponding pixel is 393 pixels down from the top. I'll let you work out the horizontal equivalent for yourself - it's basically the same. NOTE The -1 with the MAP_HEIGHT is because if you start at zero, the maximum pixel number is 915, not 916.

编辑:我想借此机会指出,近似。实际上,由于许多原因,包括在绘制地图时使用的投影,以及地球不是完美球体的事实,纬度和经度坐标与其他形式的笛卡尔坐标之间不存在简单的线性关系。在小区域中,这种近似足够接近,使得没有显着差异,但是在较大尺度上,差异可能变得明显。根据您的需要,YMMV。 (我感谢uray,下面的回答让我想起了这种情况)。

One thing I'd like to take the opportunity to point out is that this is an approximation. In reality, there isn't a simple linear relationship between latitude and longitude coordinates and other forms of Cartesian coordinates for a number of reasons including the projections that are used while making maps, and the fact that the Earth is not a perfect sphere. In small areas this approximation is close enough as to make no significant difference, but on larger scales discrepancies may become evident. Depending on your needs, YMMV. (My thanks to uray, whose answer below reminded me that this is the case).

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

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