Search code examples
gislatitude-longitudeprojection

Calculate (x,y) from (Lat,Lon) in a cropped equirectangular projection


I have a piece of a map in equirectangular projection. I know (lat,lon) in left-top and bottom-right positions. Now I know I can calculate (x,y) like this:

x = ((lon + 180) * (map_width  / 360))
y = (((lat * -1) + 90) * (map_height  / 180))

However, it seems that this produces wrong coordinates. My guess is that I have to take into account the left-top and bottom-right (lat,lon) for my image.

So I tried this way (where left, top, right, bottom_lon respresent my image boundaries):

x = ((lon + left_lon) * (map_width  / (right_lon - left_lon)))
y = (((lat * -1) + top_lat) * (map_height  / (top_lat - bottom_lat)))

But I still don't get the right result. What am I doing wrong?


Solution

  • Answering my own question with these two Python functions:

    def lon2x(lon, w):
        return int(img_w * ((lon - start_lon) / (end_lon - start_lon)))
    
    def lat2y(lat, h):
        return int(img_h * ((lat - start_lat) / (end_lat - start_lat)))