I've written the following conversion functions to convert from Lat/Long coordinates to OSM pixel x,y coordinates for a given zoom level.
Friend Sub LatLongToPixelXYOSM(ByVal latitude As Double, ByVal longitude As Double, ByVal zoomLevel As Integer, ByRef pixelX As Integer, ByRef pixelY As Integer)
Dim MinLatitude = -85.05112878
Dim MaxLatitude = 85.05112878
Dim MinLongitude = -180
Dim MaxLongitude = 180
Dim mapSize = Math.Pow(2, zoomLevel) * 256
latitude = Clip(latitude, MinLatitude, MaxLatitude)
longitude = Clip(longitude, MinLongitude, MaxLongitude)
Dim p As PointF = New Point()
p.X = CSng((longitude + 180.0) / 360.0 * (1 << zoomLevel))
p.Y = CSng((1.0 - Math.Log(Math.Tan(latitude * Math.PI / 180.0) + 1.0 / Math.Cos(toRadians(latitude))) / Math.PI) / 2.0 * (1 << zoomLevel))
Dim tilex As Integer = CInt(Math.Truncate(p.X))
Dim tiley As Integer = CInt(Math.Truncate(p.Y))
pixelX = ClipByRange((tilex * 256) + ((p.X - tilex) * 256), mapSize - 1)
pixelY = ClipByRange((tiley * 256) + ((p.Y - tiley) * 256), mapSize - 1)
End Sub
Friend Sub PixelXYToLatLongOSM(ByVal pixelX As Integer, ByVal pixelY As Integer, ByVal zoomLevel As Integer, ByRef latitude As Double, ByRef longitude As Double)
Dim mapSize = Math.Pow(2, zoomLevel) * 256
Dim tileX As Integer = Math.Truncate(pixelX / 256)
Dim tileY As Integer = Math.Truncate(pixelY / 256)
Dim p As PointF = New Point()
Dim n As Double = Math.PI - ((2.0 * Math.PI * (ClipByRange(pixelY, mapSize - 1) / 256)) / Math.Pow(2.0, zoomLevel))
longitude = CSng(((ClipByRange(pixelX, mapSize - 1) / 256) / Math.Pow(2.0, zoomLevel) * 360.0) - 180.0)
latitude = CSng(180.0 / Math.PI * Math.Atan(Math.Sinh(n)))
End Sub
Private Function ClipByRange(ByVal n As Double, ByVal range As Double)
Return n Mod range
End Function
Private Function Clip(ByVal n As Double, ByVal minValue As Double, ByVal maxValue As Double)
Return Math.Min(Math.Max(n, minValue), maxValue)
End Function
answered
14 Nov '11, 10:27
johnmeilak
25●1
accept rate:
0%
related to the question about coordinates to pixels