python中X和Y的经纬度

2024-05-02 22:47:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是谷歌地图上的一张图片,大约200x200英尺(房子大小和它的财产大小)。我的目标是有一个输入坐标(例如[37.211817,-86.682670]),它可以在我用自己的数学方法拍摄的谷歌地图图片上放置一个标记。我已经研究并尝试了许多方法。我只想取一个lat/lon,然后按比例放在一个X和Y的正方形中。在


Tags: 方法标记目标地图图片比例lonlat
2条回答

为了简单起见,我们假设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。这是lat lon,而不是lon-lat

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

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)

相关问题 更多 >