| 1 |
""" |
|---|
| 2 |
>>> toBinaryString(1) |
|---|
| 3 |
'1' |
|---|
| 4 |
>>> toBinaryString(2) |
|---|
| 5 |
'10' |
|---|
| 6 |
>>> toBinaryString(3) |
|---|
| 7 |
'11' |
|---|
| 8 |
>>> toBinaryString(4) |
|---|
| 9 |
'100' |
|---|
| 10 |
|
|---|
| 11 |
>>> fromBinaryString('1') |
|---|
| 12 |
1 |
|---|
| 13 |
>>> fromBinaryString('11') |
|---|
| 14 |
3 |
|---|
| 15 |
>>> fromBinaryString('101') |
|---|
| 16 |
5 |
|---|
| 17 |
>>> fromBinaryString('1001') |
|---|
| 18 |
9 |
|---|
| 19 |
|
|---|
| 20 |
>>> fromGoogleRoad(0, 0, 16) |
|---|
| 21 |
(0, 0, 1) |
|---|
| 22 |
>>> fromGoogleRoad(10507, 25322, 1) |
|---|
| 23 |
(10507, 25322, 16) |
|---|
| 24 |
>>> fromGoogleRoad(10482, 25333, 1) |
|---|
| 25 |
(10482, 25333, 16) |
|---|
| 26 |
|
|---|
| 27 |
>>> toGoogleRoad(0, 0, 1) |
|---|
| 28 |
(0, 0, 16) |
|---|
| 29 |
>>> toGoogleRoad(10507, 25322, 16) |
|---|
| 30 |
(10507, 25322, 1) |
|---|
| 31 |
>>> toGoogleRoad(10482, 25333, 16) |
|---|
| 32 |
(10482, 25333, 1) |
|---|
| 33 |
|
|---|
| 34 |
>>> fromGoogleAerial('tq') |
|---|
| 35 |
(0, 0, 1) |
|---|
| 36 |
>>> fromGoogleAerial('tqtsqrqtrtttqsqsr') |
|---|
| 37 |
(10507, 25322, 16) |
|---|
| 38 |
>>> fromGoogleAerial('tqtsqrqtqssssqtrt') |
|---|
| 39 |
(10482, 25333, 16) |
|---|
| 40 |
|
|---|
| 41 |
>>> toGoogleAerial(0, 0, 1) |
|---|
| 42 |
'tq' |
|---|
| 43 |
>>> toGoogleAerial(10507, 25322, 16) |
|---|
| 44 |
'tqtsqrqtrtttqsqsr' |
|---|
| 45 |
>>> toGoogleAerial(10482, 25333, 16) |
|---|
| 46 |
'tqtsqrqtqssssqtrt' |
|---|
| 47 |
|
|---|
| 48 |
>>> fromYahooRoad(0, 0, 17) |
|---|
| 49 |
(0, 0, 1) |
|---|
| 50 |
>>> fromYahooRoad(10507, 7445, 2) |
|---|
| 51 |
(10507, 25322, 16) |
|---|
| 52 |
>>> fromYahooRoad(10482, 7434, 2) |
|---|
| 53 |
(10482, 25333, 16) |
|---|
| 54 |
|
|---|
| 55 |
>>> toYahooRoad(0, 0, 1) |
|---|
| 56 |
(0, 0, 17) |
|---|
| 57 |
>>> toYahooRoad(10507, 25322, 16) |
|---|
| 58 |
(10507, 7445, 2) |
|---|
| 59 |
>>> toYahooRoad(10482, 25333, 16) |
|---|
| 60 |
(10482, 7434, 2) |
|---|
| 61 |
|
|---|
| 62 |
>>> fromYahooAerial(0, 0, 17) |
|---|
| 63 |
(0, 0, 1) |
|---|
| 64 |
>>> fromYahooAerial(10507, 7445, 2) |
|---|
| 65 |
(10507, 25322, 16) |
|---|
| 66 |
>>> fromYahooAerial(10482, 7434, 2) |
|---|
| 67 |
(10482, 25333, 16) |
|---|
| 68 |
|
|---|
| 69 |
>>> toYahooAerial(0, 0, 1) |
|---|
| 70 |
(0, 0, 17) |
|---|
| 71 |
>>> toYahooAerial(10507, 25322, 16) |
|---|
| 72 |
(10507, 7445, 2) |
|---|
| 73 |
>>> toYahooAerial(10482, 25333, 16) |
|---|
| 74 |
(10482, 7434, 2) |
|---|
| 75 |
|
|---|
| 76 |
>>> fromMicrosoftRoad('0') |
|---|
| 77 |
(0, 0, 1) |
|---|
| 78 |
>>> fromMicrosoftRoad('0230102122203031') |
|---|
| 79 |
(10507, 25322, 16) |
|---|
| 80 |
>>> fromMicrosoftRoad('0230102033330212') |
|---|
| 81 |
(10482, 25333, 16) |
|---|
| 82 |
|
|---|
| 83 |
>>> toMicrosoftRoad(0, 0, 1) |
|---|
| 84 |
'0' |
|---|
| 85 |
>>> toMicrosoftRoad(10507, 25322, 16) |
|---|
| 86 |
'0230102122203031' |
|---|
| 87 |
>>> toMicrosoftRoad(10482, 25333, 16) |
|---|
| 88 |
'0230102033330212' |
|---|
| 89 |
|
|---|
| 90 |
>>> fromMicrosoftAerial('0') |
|---|
| 91 |
(0, 0, 1) |
|---|
| 92 |
>>> fromMicrosoftAerial('0230102122203031') |
|---|
| 93 |
(10507, 25322, 16) |
|---|
| 94 |
>>> fromMicrosoftAerial('0230102033330212') |
|---|
| 95 |
(10482, 25333, 16) |
|---|
| 96 |
|
|---|
| 97 |
>>> toMicrosoftAerial(0, 0, 1) |
|---|
| 98 |
'0' |
|---|
| 99 |
>>> toMicrosoftAerial(10507, 25322, 16) |
|---|
| 100 |
'0230102122203031' |
|---|
| 101 |
>>> toMicrosoftAerial(10482, 25333, 16) |
|---|
| 102 |
'0230102033330212' |
|---|
| 103 |
""" |
|---|
| 104 |
|
|---|
| 105 |
import math |
|---|
| 106 |
|
|---|
| 107 |
octalStrings = ('000', '001', '010', '011', '100', '101', '110', '111') |
|---|
| 108 |
|
|---|
| 109 |
def toBinaryString(i): |
|---|
| 110 |
""" Return a binary string for an integer. |
|---|
| 111 |
""" |
|---|
| 112 |
return ''.join([octalStrings[int(c)] |
|---|
| 113 |
for c |
|---|
| 114 |
in oct(i)]).lstrip('0') |
|---|
| 115 |
|
|---|
| 116 |
def fromBinaryString(s): |
|---|
| 117 |
""" Return an integer for a binary string. |
|---|
| 118 |
""" |
|---|
| 119 |
s = list(s) |
|---|
| 120 |
e = 0 |
|---|
| 121 |
i = 0 |
|---|
| 122 |
while(len(s)): |
|---|
| 123 |
if(s[-1]) == '1': |
|---|
| 124 |
i += int(math.pow(2, e)) |
|---|
| 125 |
e += 1 |
|---|
| 126 |
s.pop() |
|---|
| 127 |
return i |
|---|
| 128 |
|
|---|
| 129 |
def fromGoogleRoad(x, y, z): |
|---|
| 130 |
""" Return column, row, zoom for Google Road tile x, y, z. |
|---|
| 131 |
""" |
|---|
| 132 |
col = x |
|---|
| 133 |
row = y |
|---|
| 134 |
zoom = 17 - z |
|---|
| 135 |
return col, row, zoom |
|---|
| 136 |
|
|---|
| 137 |
def toGoogleRoad(col, row, zoom): |
|---|
| 138 |
""" Return x, y, z for Google Road tile column, row, zoom. |
|---|
| 139 |
""" |
|---|
| 140 |
x = col |
|---|
| 141 |
y = row |
|---|
| 142 |
z = 17 - zoom |
|---|
| 143 |
return col, row, z |
|---|
| 144 |
|
|---|
| 145 |
googleFromCorners = {'t': '00', 's': '01', 'q': '10', 'r': '11'} |
|---|
| 146 |
googleToCorners = {'00': 't', '01': 's', '10': 'q', '11': 'r'} |
|---|
| 147 |
|
|---|
| 148 |
def fromGoogleAerial(s): |
|---|
| 149 |
""" Return column, row, zoom for Google Aerial tile string. |
|---|
| 150 |
""" |
|---|
| 151 |
row, col = map(fromBinaryString, zip(*[list(googleFromCorners[c]) for c in s])) |
|---|
| 152 |
zoom = len(s) - 1 |
|---|
| 153 |
row = int(math.pow(2, zoom) - row - 1) |
|---|
| 154 |
return col, row, zoom |
|---|
| 155 |
|
|---|
| 156 |
def toGoogleAerial(col, row, zoom): |
|---|
| 157 |
""" Return string for Google Road tile column, row, zoom. |
|---|
| 158 |
""" |
|---|
| 159 |
x = col |
|---|
| 160 |
y = int(math.pow(2, zoom) - row - 1) |
|---|
| 161 |
z = zoom + 1 |
|---|
| 162 |
y, x = toBinaryString(y).rjust(z, '0'), toBinaryString(x).rjust(z, '0') |
|---|
| 163 |
string = ''.join([googleToCorners[y[c]+x[c]] for c in range(z)]) |
|---|
| 164 |
return string |
|---|
| 165 |
|
|---|
| 166 |
def fromYahoo(x, y, z): |
|---|
| 167 |
""" Return column, row, zoom for Yahoo x, y, z. |
|---|
| 168 |
""" |
|---|
| 169 |
zoom = 18 - z |
|---|
| 170 |
row = int(math.pow(2, zoom - 1) - y - 1) |
|---|
| 171 |
col = x |
|---|
| 172 |
return col, row, zoom |
|---|
| 173 |
|
|---|
| 174 |
def toYahoo(col, row, zoom): |
|---|
| 175 |
""" Return x, y, z for Yahoo tile column, row, zoom. |
|---|
| 176 |
""" |
|---|
| 177 |
x = col |
|---|
| 178 |
y = int(math.pow(2, zoom - 1) - row - 1) |
|---|
| 179 |
z = 18 - zoom |
|---|
| 180 |
return x, y, z |
|---|
| 181 |
|
|---|
| 182 |
def fromYahooRoad(x, y, z): |
|---|
| 183 |
""" Return column, row, zoom for Yahoo Road tile x, y, z. |
|---|
| 184 |
""" |
|---|
| 185 |
return fromYahoo(x, y, z) |
|---|
| 186 |
|
|---|
| 187 |
def toYahooRoad(col, row, zoom): |
|---|
| 188 |
""" Return x, y, z for Yahoo Road tile column, row, zoom. |
|---|
| 189 |
""" |
|---|
| 190 |
return toYahoo(col, row, zoom) |
|---|
| 191 |
|
|---|
| 192 |
def fromYahooAerial(x, y, z): |
|---|
| 193 |
""" Return column, row, zoom for Yahoo Aerial tile x, y, z. |
|---|
| 194 |
""" |
|---|
| 195 |
return fromYahoo(x, y, z) |
|---|
| 196 |
|
|---|
| 197 |
def toYahooAerial(col, row, zoom): |
|---|
| 198 |
""" Return x, y, z for Yahoo Aerial tile column, row, zoom. |
|---|
| 199 |
""" |
|---|
| 200 |
return toYahoo(col, row, zoom) |
|---|
| 201 |
|
|---|
| 202 |
microsoftFromCorners = {'0': '00', '1': '01', '2': '10', '3': '11'} |
|---|
| 203 |
microsoftToCorners = {'00': '0', '01': '1', '10': '2', '11': '3'} |
|---|
| 204 |
|
|---|
| 205 |
def fromMicrosoft(s): |
|---|
| 206 |
""" Return column, row, zoom for Microsoft tile string. |
|---|
| 207 |
""" |
|---|
| 208 |
row, col = map(fromBinaryString, zip(*[list(microsoftFromCorners[c]) for c in s])) |
|---|
| 209 |
zoom = len(s) |
|---|
| 210 |
return col, row, zoom |
|---|
| 211 |
|
|---|
| 212 |
def toMicrosoft(col, row, zoom): |
|---|
| 213 |
""" Return string for Microsoft tile column, row, zoom. |
|---|
| 214 |
""" |
|---|
| 215 |
x = col |
|---|
| 216 |
y = row |
|---|
| 217 |
y, x = toBinaryString(y).rjust(zoom, '0'), toBinaryString(x).rjust(zoom, '0') |
|---|
| 218 |
string = ''.join([microsoftToCorners[y[c]+x[c]] for c in range(zoom)]) |
|---|
| 219 |
return string |
|---|
| 220 |
|
|---|
| 221 |
def fromMicrosoftRoad(s): |
|---|
| 222 |
""" Return column, row, zoom for Microsoft Road tile string. |
|---|
| 223 |
""" |
|---|
| 224 |
return fromMicrosoft(s) |
|---|
| 225 |
|
|---|
| 226 |
def toMicrosoftRoad(col, row, zoom): |
|---|
| 227 |
""" Return x, y, z for Microsoft Road tile column, row, zoom. |
|---|
| 228 |
""" |
|---|
| 229 |
return toMicrosoft(col, row, zoom) |
|---|
| 230 |
|
|---|
| 231 |
def fromMicrosoftAerial(s): |
|---|
| 232 |
""" Return column, row, zoom for Microsoft Aerial tile string. |
|---|
| 233 |
""" |
|---|
| 234 |
return fromMicrosoft(s) |
|---|
| 235 |
|
|---|
| 236 |
def toMicrosoftAerial(col, row, zoom): |
|---|
| 237 |
""" Return x, y, z for Microsoft Aerial tile column, row, zoom. |
|---|
| 238 |
""" |
|---|
| 239 |
return toMicrosoft(col, row, zoom) |
|---|
| 240 |
|
|---|
| 241 |
if __name__ == '__main__': |
|---|
| 242 |
import doctest |
|---|
| 243 |
doctest.testmod() |
|---|