Jozef
Jozef

Reputation: 31

Transform x,y pixel values into lat and long

I have latitude, longitude and zoom of center of my map window. I have dimension map window. I also have pixel positions of specific events on the map (mercator projection -openstreetmap ) 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 ? general case of my problem

Upvotes: 1

Views: 1915

Answers (1)

Jozef
Jozef

Reputation: 31

I have found the solution using this formula (https://en.wikipedia.org/wiki/Web_Mercator_projection ) enter image description here

  1. calculate the x,y position of my center point

  2. trasform coordinate system of window to global

  3. inverse transformation

      def LatLontoXY(lat_center,lon_center,zoom):
         C =(256/(2*pi) )* 2**zoom
    
         x=C*(math.radians(lon_center)+pi)
         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):
    
         xcenter,ycenter=LatLontoXY(lat_center,lon_center,zoom)
    
         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
    

Upvotes: 2

Related Questions