简体   繁体   中英

Latitude and Longitude to X and Y in python

I am using a picture from Google Maps, roughly 200x200 feet in size (the size of a house and it's property). My goal is to have an input coordinate (Eg [37.211817, -86.682670]) that can place a marker on my Google Maps picture I took, using my own math. I have looked and tried many methods. I just simply want to take a lat / lon, and proportionally put it in a square X and Y big.

Ok, I found the answer, and I will share it as it seems more complicated than I ever anticipated. The solution was to rotate the GPS coordinates 90° clockwise, then perform a reflection over the Y-Axis. -> (y, -x) +> (x, -y).

EDIT

So yea, all that has to happen is flip the x and y. It's lat-lon, not lon-lat.

Then, it's a simple scaling formula to fit it to your screen. Heres the code:

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)

Let's assume for the sake of simplicity that GPS coordinates can scale to another coordinate system linearly. You'll need the GPS coordinates of the top left-most point on the image and the bottom right-most point:

Pseudocode:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM