Hi I have a little problem here. I have latitude, longitude and zoom of center of my map window (openstreet map). I have dimension map window. I also have pixel positions of specific events on the map in the window. Can Anyone help me how to convert these pixel positions into latitude and longitude coordinates or any other coordinate system that I will be able to visualize on different map later ? Here is an illustration of my problem. alt text

asked 09 Jul '20, 08:24

Jozef's gravatar image

accept rate: 0%

Thank I have found the solution using this formula (https://en.wikipedia.org/wiki/Web_Mercator_projection ) alt text

  1. calculate the x,y position of my center point
  2. transform coordinate system of window to global
  3. inverse transformation

code below

def LatLontoXY(lat_center,lon_center,zoom):
    C =(256/(2*pi) )* 2**zoom

    y=C*(pi-math.log( math.tan(  (pi/4) + math.radians(lat_center)/2    )  ))

    return x,y

def xy2LatLon(lat_center,lon_center,zoom,width_internal,height_internal,pxX_internal,pxY_internal):


    xPoint=xcenter- (width_internal/2-pxX_internal)
    ypoint=ycenter -(height_internal/2-pxY_internal)

    C = (256 / (2 * pi)) * 2 ** zoom
    M = (xPoint/C)-pi
    N =-(ypoint/C) + pi

    lon_Point =math.degrees(M)
    lat_Point =math.degrees( (math.atan( math.e**N)-(pi/4))*2 )

    return lat_Point,lon_Point
permanent link

answered 09 Jul '20, 15:43

Jozef's gravatar image

accept rate: 0%

The projection you are displaying the map in is the so called spherical Mercator projection, you can convert Mercator coordinates to WGS84 lat/lon with the formulas from https://wiki.openstreetmap.org/wiki/Mercator (you will need to scale from you screen pixels or whatever to Mercator meters but that is a simple exercise left for the reader).

Further note: Mercator latitude coordinates do not actually cover -90° to 90° degrees as your image would suggest.

permanent link

answered 09 Jul '20, 12:19

SimonPoole's gravatar image

SimonPoole ♦
accept rate: 19%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 09 Jul '20, 08:24

question was seen: 814 times

last updated: 09 Jul '20, 15:43

powered by OSQA