-
Notifications
You must be signed in to change notification settings - Fork 2
/
tileutils.py
41 lines (35 loc) · 1.09 KB
/
tileutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
# This file is part of Kuona - crowdsourcing application for OSM HOT
#
# Copyright (c) 2013 Nicolás Alvarez
#
# This program is free software; see the LICENSE file for details.
import math
def tms2quad(x,y,zoom):
quadkey=""
for i in reversed(range(0,zoom)):
num = 0
mask = 1<<i
if x & mask:
num |= 1
if y & mask:
num |= 2
quadkey += str(num)
return quadkey
def tms2latlon(xtile,ytile,zoom):
n = 2**zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)
def latlon2tms(lat,lon,zoom):
"""
Converts lat/lon (in degrees) to tile x/y coordinates in the given zoomlevel.
Note that it doesn't give integers! If you need integers, truncate
the result yourself.
"""
n = 2**zoom
lat_rad = math.radians(lat)
xtile = (lon+ 180.0) / 360.0 * n
ytile = (1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n
return (xtile, ytile)