python 中 X 和 Y 的纬度和经度

Latitude and Longitude to X and Y in python

我正在使用来自 Google 地图的图片,大小大约为 200x200 英尺(房子的大小,它是 属性)。我的目标是拥有一个输入坐标(例如 [37.211817,-86.682670]),它可以使用我自己的数学在我拍摄的 Google 地图照片上放置一个标记。我看过并尝试了很多方法。我只是想简单地取一个纬度/经度,然后按比例把它放在一个正方形的X和Y中。

为了简单起见,我们假设 GPS 坐标可以线性缩放到另一个坐标系。您需要图像上最左上角和最右下角的 GPS 坐标:

伪代码:

input: gps_marker

let gps1 = lat/lon of location corresponding to top left of image.
let gps2 = lat/lon of location corresponding to bottom right of image.

let x_offset = (gps_marker.lon - gps1.lon)/(gps2.lon - gps1.lon) * image.width
let y_offset = (gps_marker.lat - gps1.lat)/(gps2.lat - gps1.lat) * image.height

// x_offset and y_offset are the x,y for your marker within the image.

好的,我找到了答案,我会分享它,因为它看起来比我想象的要复杂。解决方案是将 GPS 坐标顺时针旋转 90°,然后在 Y 轴上进行反射。 -> (y, -x) +> (x, -y)。

编辑

所以,是的,所要做的就是翻转 x 和 y。是经纬度,不是经纬度。

然后,这是一个简单的缩放公式,以适应您的屏幕。代码如下:

top_left_raw = GPS_COORD
bottom_right_raw = GPS_COORD
maprect = [0,0,400,500] # Picture of map's width and height

def translate(pos):
    #rot = (pos[1], pos[0]*-1)
    #reflect = (rot[0], rot[1]*-1)
    #return reflect
    return (pos[1], pos[0])

def gps_to_coord(pos):
    pos1 = translate((pos[0]-top_left_raw[0], pos[1]-top_left_raw[1]))
    pos2 = translate((bottom_right_raw[0] - top_left_raw[0], bottom_right_raw[1] - top_left_raw[1]))
    x = (maprect[2]*pos1[0]) / pos2[0]
    y = (maprect[3]*pos1[1]) / pos2[1]
    return (x,y)

gps_to_coord(GPS_COORD)