[OSM-dev] Projection code

Raphael Jacquot sxpert at esitcom.org
Fri Apr 28 19:30:59 BST 2006


Graham Wall wrote:
> Can someone point me to the code to project (lat, lon)  to (x,y)
> please? I've had a look around on the svn, but can't download it here
> to search.
> 
> Graham

this is my python version ;D

class MercatorTransform:
         def __init__(self, center, x, y, width, height, degree_per_pixel):
                 self.center = center
                 self.x = x
                 self.y = y
                 self.width = width
                 self.height = height
                 self.degree_per_pixel = degree_per_pixel
                 self.delta_y = 0
                 self.delta_y = self.calc_merc_y(center)

         def get_x(self, point):
                 return ( point[LONGITUDE] - self.center[LONGITUDE] ) / 
self.degree_per_pixel + self.width/2 + self.x

         def calc_merc_y(self, point):
                 sinphi = sin(radians(point[LATITUDE]))
                 return degrees(0.5*log((1+sinphi)/(1-sinphi))) / 
self.degree_per_pixel

         def get_y(self, point):
                 return self.delta_y - self.calc_merc_y(point) + 
self.height/2 + self.y

         def get_lon(self, point):
                 return self.degree_per_pixel * (point['x'] - 
self.width/2- self.x)  + self.center[LONGITUDE]

         def calc_merc_lat (self, y):
                 return atan(sinh(radians(y*self.degree_per_pixel)))

         def get_lat(self, point):
                 yt = (point['y']-self.y - self.height/2)
                 y = self.delta_y - yt
                 lat_rad =  self.calc_merc_lat( y )
                 lat_deg = degrees(lat_rad)
                 return lat_deg




More information about the dev mailing list