-
Notifications
You must be signed in to change notification settings - Fork 8
/
tms.py
1432 lines (1273 loc) · 47.3 KB
/
tms.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import io
import re
import time
import math
import json
import html
import random
import sqlite3
import weakref
import logging
import warnings
import datetime
import itertools
import collections
import configparser
import collections.abc
import concurrent.futures
import pycurl
import prcoords
import tornado.web
import tornado.gen
import tornado.locks
import tornado.ioloop
import tornado.httpclient
import tornado.httpserver
import tornado.curl_httpclient
from PIL import Image
try:
import certifi
CA_CERTS = certifi.where()
except ImportError:
CA_CERTS = None
try:
from pyproj import Transformer
from pyproj.crs import CRS
PROJ_AVAILABLE = True
except ImportError:
PROJ_AVAILABLE = False
logging.basicConfig(
#level=logging.INFO,
level=logging.WARNING,
format='%(asctime)s [%(levelname)s] %(message)s')
logging.captureWarnings(True)
# warnings.simplefilter("ignore")
# Earth mean radius
R_EARTH = 6371000
RES_3857 = 40075016.685578486153
ORIGIN_3857 = 20037508.342789243077
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG = {}
APIS = None
re_user_agent = re.compile(r'Mozilla/5.0 \(.+(Chrome|Gecko|AppleWebKit|Safari|Edg)')
HEADERS_WHITELIST = {
'Accept-Encoding',
'Upgrade-Insecure-Requests',
'Dnt',
'Cookie',
'User-Agent',
'Accept',
'Accept-Language'
}
DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0'
HTTP_CLIENT = tornado.curl_httpclient.CurlAsyncHTTPClient()
def prepare_curl_socks5(curl):
curl.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
def projection_3857(lon, lat):
x = math.radians(lon) * 6378137
y = math.asinh(math.tan(math.radians(lat))) * 6378137
return (x, y)
def projection_4326(lon, lat):
return (lon, lat)
class DBTileCache(collections.abc.MutableMapping):
def __init__(self, filename, maxsize, ttl):
self.maxsize = maxsize
self.ttl = ttl
self.db = sqlite3.connect(filename, isolation_level=None)
self.db.execute('PRAGMA journal_mode=WAL')
self.db.execute('CREATE TABLE IF NOT EXISTS cache ('
'key TEXT PRIMARY KEY,'
'expire INTEGER,'
'updated INTEGER,'
'mime TEXT,'
'img BLOB'
') WITHOUT ROWID')
self.db.execute('CREATE TABLE IF NOT EXISTS metadata ('
'key TEXT PRIMARY KEY,'
'value TEXT'
') WITHOUT ROWID')
def __contains__(self, key):
r = self.db.execute(
'SELECT 1 FROM cache WHERE key=? AND expire>=?', (key, time.time())
).fetchone()
return bool(r)
def __getitem__(self, key):
r = self.db.execute(
'SELECT img, mime FROM cache WHERE key=? AND expire>=?', (key, time.time())
).fetchone()
if r:
return r
else:
raise KeyError(key)
def save(self, key, value, ttl=None):
if ttl is None:
ttl = self.ttl
now = int(time.time())
self.db.execute('REPLACE INTO cache VALUES (?,?,?,?,?)',
(key, now+ttl, now, value[1], value[0]))
self.expire()
def set_meta(self, key, value):
self.db.execute('REPLACE INTO metadata VALUES (?,?)',
(key, json.dumps(value)))
def get_meta(self, key):
row = self.db.execute(
'SELECT value FROM metadata WHERE key=?', (key,)).fetchone()
if row is None:
return None
return json.loads(row[0])
def __setitem__(self, key, value):
self.save(key, value)
def __delitem__(self, key):
self.db.execute('DELETE FROM cache WHERE key=?', (key,))
def __iter__(self):
for row in self.db.execute('SELECT key FROM cache'):
yield row[0]
def __len__(self):
r = self.db.execute(
'SELECT count(*) FROM cache WHERE expire>=?', (time.time(),)).fetchone()
return r[0]
def expire(self, etime=None):
"""Remove expired items from the cache."""
self.db.execute('DELETE FROM cache WHERE key NOT IN ('
'SELECT key FROM cache WHERE expire>=? ORDER BY updated DESC LIMIT ?)',
(etime or time.time(), self.maxsize))
def clear(self):
self.db.execute('DELETE FROM cache')
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
class MemoryTileCache(collections.abc.MutableMapping):
def __init__(self, maxsize, ttl):
self.maxsize = maxsize
self.ttl = ttl
self.cache = collections.OrderedDict()
self.metadata = {}
def __contains__(self, key):
if key not in self.cache:
return False
return self.cache[key][0] >= time.time()
def __getitem__(self, key):
row = self.cache[key]
if row[0] < time.time():
del self.cache[key]
raise KeyError(key)
return row[1:]
def save(self, key, value, ttl=None):
if ttl is None:
ttl = self.ttl
now = int(time.time())
self.cache[key] = (now+ttl, value[0], value[1])
self.expire()
def set_meta(self, key, value):
self.metadata[key] = value
def get_meta(self, key):
return self.metadata.get(key)
def __setitem__(self, key, value):
self.save(key, value)
def __delitem__(self, key):
del self.cache[key]
def __iter__(self):
return iter(self.cache)
def __len__(self):
return len(self.cache)
def expire(self, etime=None):
"""Remove expired items from the cache."""
orig_len = len(self.cache)
if orig_len < self.maxsize:
return
keys = tuple(self.cache.keys())
i = 0
now = time.time()
while (i < orig_len and (
self.cache[keys[i]][0] < now or len(self.cache) > self.maxsize
)):
self.cache.popitem(last=False)
i += 1
def clear(self):
self.cache.clear()
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
class KeyedAsyncLocks(collections.abc.Collection):
"""
asyncio.Lock with names.
Automatically create and delete locks for specified names.
"""
def __init__(self):
self.locks = weakref.WeakValueDictionary()
def __len__(self) -> int:
return len(self.locks)
def __getitem__(self, item) -> 'tornado.locks.Lock':
lock = self.locks.get(item)
if lock is None:
self.locks[item] = lock = tornado.locks.Lock()
return lock
def __delitem__(self, key) -> None:
try:
del self.locks[key]
except KeyError:
pass
def __iter__(self):
return iter(self.locks)
def __contains__(self, item):
return item in self.locks
def keys(self):
return self.locks.keys()
def items(self):
return self.locks.items()
class TileProvider:
sgn = (1, 1)
has_metadata = False
retry_on_error = False
projection_fn = projection_3857
def __init__(self, name, url, url2x=None, servers=None, cache=None, attrs=None):
self.name = name
self.url = url
self.url2x = url2x
self.servers = servers
self.cache = cache
self.attrs = attrs or {}
def __contains__(self, item):
return item in self.attrs
def __getitem__(self, key):
return self.attrs[key]
def get(self, key, default=None):
return self.attrs.get(key, default)
def fast_offset_check(self, z):
return True
async def check_metadata(self, headers=None, no_cache=False):
return {}
def offset(self, x, y, z):
return (x, y, z)
def url_params(self, x, y, z):
return {
's': (random.choice(self.servers) if self.servers else ''),
'x': x, 'y': y, 'z': z,
't': int(time.time() * 1000),
}
def get_url(self, x, y, z, retina=False):
url = self.url2x if retina and self.url2x else self.url
return url.format(**self.url_params(x, y, z))
def load_projection(self, crs):
if crs == 3857:
self.projection_fn = projection_3857
return
elif crs == 4326:
self.projection_fn = projection_4326
return
elif not PROJ_AVAILABLE:
raise RuntimeError("Unsupported CRS: %s" % crs)
if isinstance(crs, int):
target_crs = CRS('EPSG:%s' % crs)
else:
target_crs = CRS(crs)
self._transformer = Transformer.from_crs(
CRS("+proj=longlat +datum=WGS84 +no_defs"),
target_crs)
self.projection_fn = self._transformer.transform
class TMSTileProvider(TileProvider):
sgn = (1, -1)
def fast_offset_check(self, z):
return False
def offset(self, x, y, z):
return (x, 2**z - 1 - y, z)
class GCJTileProvider(TileProvider):
def fast_offset_check(self, z):
return (z < 8)
def offset(self, x, y, z):
if z < 8:
return (x, y, z)
realpos = prcoords.wgs_gcj(num2deg(x, y, z), True)
realx, realy = deg2num(zoom=z, *realpos)
return (realx, realy, z)
class QQTileProvider(TileProvider):
sgn = (1, -1)
def fast_offset_check(self, z):
return False
def url_params(self, x, y, z):
params = super().url_params(x, y, z)
params['x4'] = (x>>4)
params['y4'] = (y>>4)
return params
def offset(self, x, y, z):
if z < 8:
return (x, 2**z - 1 - y, z)
realpos = prcoords.wgs_gcj(num2deg(x, y+1, z), True)
realx, realy = deg2num(zoom=z, *realpos)
return (realx, 2**z - realy, z)
class BaiduTileProvider(TileProvider):
sgn = (1, -1)
def fast_offset_check(self, z):
return False
def url_params(self, x, y, z):
params = super().url_params(x, y, z)
params['xm'] = str(x).replace('-', 'M')
params['ym'] = str(y).replace('-', 'M')
params['d'] = datetime.date.today().strftime('%Y%m%d')
return params
@staticmethod
def bd_merc(lat, lon):
# Baidu uses EPSG:7008, Clarke 1866
# PROJ:
# +proj=merc +a=6378206.4 +b=6356583.8 +lat_ts=0.0 +lon_0=0.0
# +x_0=0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
a = 6378206.4
e = 0.08227185422300325876963654309
x = math.radians(lon) * a
phi = math.radians(lat)
y = (math.asinh(math.tan(phi)) - e * math.atanh(e * math.sin(phi))) * a
return (x, y)
def offset(self, x, y, z):
realpos = prcoords.wgs_bd(num2deg(x, y+1, z), True)
if 'upscale' not in self.attrs:
z += 1
x, y = self.bd_merc(*realpos)
factor = 2**(z - 18 - 8)
return (x * factor, y * factor, z)
class ArcGISMapServerProvider(TileProvider):
sgn = (1, 1)
has_metadata = True
no_offset = True
def __init__(self, name, url, cache=None, attrs=None):
self.name = name
self.url = url
self.cache = cache
self.attrs = attrs or {}
self.metadata = None
self.metadata_lock = tornado.locks.Lock()
def fast_offset_check(self, z):
if not self.metadata or not self.no_offset:
return False
return bool(self.metadata.get('no_offset'))
def get_srid(self, tile_info):
spref = tile_info.get('spatialReference', {})
srid = spref.get('latestWkid', spref.get('wkid', 4326))
if srid in (102113, 900913, 3587, 54004, 41001, 102100, 3785):
srid = 3857
return srid
async def get_metadata(self, headers=None, no_cache=False):
if not no_cache:
if self.metadata is not None:
return
cached_metadata = self.cache.get_meta(self.name)
if cached_metadata is not None:
self.metadata = cached_metadata
self.load_projection(self.metadata['srid'])
return
client = tornado.curl_httpclient.CurlAsyncHTTPClient()
response = await client.fetch(
self.url + '?f=json',
headers=headers, connect_timeout=10, ca_certs=CA_CERTS,
proxy_host=CONFIG.get('proxy_host'), proxy_port=CONFIG.get('proxy_port'),
prepare_curl_callback=(prepare_curl_socks5
if 'socks5' == CONFIG.get('proxy_type') else None)
)
if response.code != 200:
raise RuntimeError("Can't get metadata: code %s" % response.code)
d = json.loads(response.body.decode('utf-8', errors='ignore'))
if not d.get('singleFusedMapCache'):
raise ValueError("Not tiled map")
tile_info = d['tileInfo']
srid = self.get_srid(tile_info)
# if srid not in self.supported_srid:
# raise RuntimeError("Unsupported SRID: %s" % self.metadata['srid'])
self.load_projection(srid)
self.metadata = {
'size': (tile_info['cols'], tile_info['rows']),
'srid': srid,
'origin': (tile_info['origin']['x'], tile_info['origin']['y']),
'levels': sorted(
(level['level'], level['resolution']*tile_info['rows'])
for level in tile_info['lods']
),
'no_offset': False
}
if (srid == 3857 and self.no_offset and
math.isclose(-ORIGIN_3857, self.metadata['origin'][0]) and
math.isclose(ORIGIN_3857, self.metadata['origin'][1])):
no_offset = True
for level, resolution in self.metadata['levels']:
req_resolution = RES_3857 / 2 ** level
if not math.isclose(resolution, req_resolution, rel_tol=1/256):
no_offset = False
break
self.metadata['no_offset'] = no_offset
self.cache.set_meta(self.name, self.metadata)
async def check_metadata(self, headers=None, no_cache=False):
async with self.metadata_lock:
await self.get_metadata(headers, no_cache)
return {}
def convert_z(self, x, y, z, srid):
if srid == 3857:
req_resolution = RES_3857 / 2 ** z
else:
lat0, lon0 = num2deg(x, y, z)
lat1, lon1 = num2deg(x + 1, y + 1, z)
if srid == 4326:
x0 = lon0
y0 = lat0
x1 = lon1
y1 = lat1
else:
x0, y0 = self.projection_fn(lon0, lat0)
x1, y1 = self.projection_fn(lon1, lat1)
req_resolution = math.sqrt(abs(x1 - x0) * abs(y1 - y0))
level = resolution = up_res = None
for level, resolution in self.metadata['levels']:
if abs(resolution - req_resolution) / req_resolution < 1/256:
return level, resolution
elif resolution < req_resolution:
break
up_res = (level, resolution)
if 'upscale' in self.attrs and up_res is not None:
return up_res
return level, resolution
@staticmethod
def offset_latlon(lat, lon):
return (lat, lon)
def offset(self, x, y, z):
realpos_ll = tuple(reversed(self.offset_latlon(*num2deg(x, y, z))))
realpos_xy = self.projection_fn(*realpos_ll)
tilez, resolution = self.convert_z(x, y, z, self.metadata['srid'])
if tilez is None:
return (None, None, None)
ox, oy = self.metadata['origin']
tilex = ((realpos_xy[0] - ox) / resolution)
tiley = -((realpos_xy[1] - oy) / resolution)
return (tilex, tiley, tilez)
def get_url(self, x, y, z, retina=False):
return '{url}/tile/{z}/{y}/{x}'.format(url=self.url, x=x, y=y, z=z)
class TiandituTileProvider(TileProvider):
has_metadata = True
retry_on_error = True
re_ticket = re.compile(r'tk=([0-9A-Za-z]+)')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.metadata = None
self.metadata_lock = tornado.locks.Lock()
async def get_metadata(self, headers=None, no_cache=False):
if not no_cache:
if self.metadata is not None:
return self.metadata['cookies']
cached_metadata = self.cache.get_meta(self.name)
if cached_metadata is not None:
self.metadata = cached_metadata
return self.metadata['cookies']
client = tornado.curl_httpclient.CurlAsyncHTTPClient()
response = await client.fetch(
'https://map.tianditu.gov.cn/2020/',
headers=headers, connect_timeout=10, ca_certs=CA_CERTS,
proxy_host=CONFIG.get('proxy_host'), proxy_port=CONFIG.get('proxy_port'),
prepare_curl_callback=(prepare_curl_socks5
if 'socks5' == CONFIG.get('proxy_type') else None)
)
if response.code != 200:
raise RuntimeError("Can't get metadata: code %s" % response.code)
match = self.re_ticket.search(
response.body.decode('utf-8', errors='ignore'))
if not match:
raise RuntimeError("Can't find ticket")
cookies = []
for cookie in response.headers.get_list("Set-Cookie"):
cookies.append(cookie.rsplit(';', 1)[0].strip())
self.metadata = {'ticket': match.group(1), 'cookies': cookies}
self.cache.set_meta(self.name, self.metadata)
return self.metadata['cookies']
async def check_metadata(self, headers=None, no_cache=False):
async with self.metadata_lock:
cookies = await self.get_metadata(headers, no_cache)
if headers and headers.get('Cookie'):
cookies.insert(0, headers['Cookie'])
return {
'Cookie': '; '.join(cookies),
'Referer': 'https://map.tianditu.gov.cn/'
}
def get_url(self, x, y, z, retina=False):
return self.url.format(
s=(random.choice(self.servers) if self.servers else ''),
x=x, y=y, z=z, tk=self.metadata['ticket'])
class GCJMapServerProvider(ArcGISMapServerProvider):
no_offset = False
@staticmethod
def offset_latlon(lat, lon):
return prcoords.wgs_gcj((lat, lon), True)
class TiandituShanghaiMapServerProvider(ArcGISMapServerProvider):
no_offset = False
@staticmethod
def offset_latlon(lat, lon):
o_lon = 121.3
# o_lat = 31.2
bK = 1.006
V = 1.0
bN = 0.99
bm = 0.0
x1 = 0.5
x2 = 1.0
v = (V - bK) / (x1 - bm)
bk = V - v * x1
j = (bN - V) / (x2 - x1)
bo = bN - j * (x2 - x1)
x_sgn = 1 if lon > o_lon else -1
d_lon = abs(lon - o_lon)
if d_lon < x1:
f = v * d_lon + bk
d_lon = f * d_lon
x = o_lon + d_lon * x_sgn
lon = x
elif d_lon > x1 and d_lon < x2:
d_lon = d_lon - x1
f = j * d_lon + bo
d_lon = f * d_lon
lon = o_lon + x1 * x_sgn + d_lon * x_sgn
return (lat, lon)
class TiandituShanghaiMapServerSHCorsProvider(ArcGISMapServerProvider):
no_offset = False
def get_srid(self, tile_info):
return -100001
def load_projection(self, crs):
self._transformer = Transformer.from_crs(
CRS("+proj=longlat +datum=WGS84 +no_defs"),
CRS("+proj=tmerc +lat_0=31.235443609882868 +lon_0=121.46464574117608 +k=1 +x_0=-238.2131268606429 +y_0=0 +ellps=GRS80 +units=m +no_defs"))
self.projection_fn = self._transformer.transform
SRC_TYPE = {
'xyz': TileProvider,
'tms': TMSTileProvider,
'gcj': GCJTileProvider,
'qq': QQTileProvider,
'bd': BaiduTileProvider,
'baidu': BaiduTileProvider,
'arcgis': ArcGISMapServerProvider,
'arcgis_gcj': GCJMapServerProvider,
'tianditu': TiandituTileProvider,
'shtdt': TiandituShanghaiMapServerProvider,
'shtdt_shcors': TiandituShanghaiMapServerSHCorsProvider,
}
TILE_SOURCE_CACHE = {}
TILE_GET_LOCKS = KeyedAsyncLocks()
def load_config(config_file, tmsapi_file):
global APIS, TILE_SOURCE_CACHE, CONFIG
if APIS:
return
config = configparser.ConfigParser(interpolation=None)
config.read(config_file, 'utf-8')
cfg = dict(config['CONFIG'])
cfg['port'] = int(cfg['port'])
cfg['cache_size'] = int(cfg['cache_size'])
cfg['cache_ttl'] = int(cfg['cache_ttl'])
cfg['cache_realtime_ttl'] = int(cfg.get('cache_realtime_ttl', 60))
if 'proxy_port' in cfg:
cfg['proxy_port'] = int(cfg['proxy_port'])
CONFIG = cfg
if cfg.get('cache_db'):
TILE_SOURCE_CACHE = DBTileCache(
cfg['cache_db'], cfg['cache_size'], cfg['cache_ttl'])
else:
TILE_SOURCE_CACHE = MemoryTileCache(cfg['cache_size'], cfg['cache_ttl'])
APIS = collections.OrderedDict()
api_config = configparser.ConfigParser(interpolation=None)
api_config.read(tmsapi_file, 'utf-8')
for name, cfgsection in api_config.items():
if name in ('DEFAULT', 'CONFIG'):
continue
section = dict(cfgsection)
src_type = section.get('type', section.get('offset', 'xyz'))
cls = SRC_TYPE.get(src_type)
if cls is None:
raise ValueError('unknown source API type: %s' % src_type)
kwargs = {'url': section.pop('url'), 'cache': TILE_SOURCE_CACHE}
if 'url2x' in section:
kwargs['url2x'] = section.pop('url2x')
if 's' in section:
kwargs['servers'] = section.pop('s').split(',')
kwargs['attrs'] = section
APIS[name] = cls(name, **kwargs)
def num2deg(xtile, ytile, zoom):
n = 2 ** zoom
lat = math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))))
lon = xtile / n * 360 - 180
return (lat, lon)
def deg2num(lat, lon, zoom):
n = 2 ** zoom
xtile = ((lon + 180) / 360 * n)
ytile = (1 - math.asinh(math.tan(math.radians(lat))) / math.pi) * n / 2
return (xtile, ytile)
def is_empty(im):
extrema = im.getextrema()
if len(extrema) >= 3:
if len(extrema) > 3 and extrema[-1] == (0, 0):
return True
for ext in extrema[:3]:
if ext != (0, 0):
return False
return True
else:
return extrema[0] == (0, 0)
def stitch_tiles(tiles, corners, bbox, grid, sgnxy, name, target_format=None):
ims = []
size = orig_format = orig_mode = None
for b, _ in tiles:
if b:
im = Image.open(io.BytesIO(b))
ims.append(im)
size = im.size
orig_format = im.format
orig_mode = im.mode
else:
ims.append(None)
if not size:
return None, None
target_format = target_format or orig_format
mode = 'RGB' if orig_mode == 'RGB' else 'RGBA'
newim = Image.new(mode, (
size[0]*(bbox[2]-bbox[0]), size[1]*(bbox[3]-bbox[1])))
mesh = calc_pil_mesh(sgnxy, size, bbox, grid)
for i, xy in enumerate(corners):
if ims[i] is None:
continue
xy0 = (size[0]*xy[0][0], size[1]*xy[1][0])
if mode == 'RGB':
newim.paste(ims[i], xy0)
else:
im = ims[i]
if im.mode != mode:
im = im.convert(mode)
if is_empty(im):
newim.paste((0,0,0,0), xy0 + (xy0[0]+size[0], xy0[1]+size[1]))
else:
newim.paste(im, xy0)
ims[i].close()
del ims
retim = newim.transform(size,
Image.Transform.MESH, mesh, resample=Image.Resampling.BICUBIC)
if retim.mode == 'RGBA' and retim.getextrema()[3][0] >= 252:
retim = retim.convert('RGB')
newim.close()
del newim
retb = io.BytesIO()
mime_type = tiles[0][1]
if target_format == 'JPEG':
if retim.mode == 'RGBA':
new_im = Image.new("RGBA", retim.size, "WHITE")
new_im.paste(retim, (0, 0), retim)
retim = new_im.convert('RGB')
retim.save(retb, 'JPEG', quality=93)
mime_type = 'image/jpeg'
else:
if orig_mode == 'P' and target_format == 'PNG':
retim = retim.quantize(colors=256)
retim.save(retb, target_format)
mime_type = 'image/' + target_format.lower()
retim.close()
del retim
return retb.getvalue(), mime_type
async def get_tile(source, z, x, y, retina=False, client_headers=None):
api = APIS[source]
x = int(x)
y = int(y)
cache_key = '%s%s/%d/%d/%d' % (source, '@2x' if retina else '', z, x, y)
async with TILE_GET_LOCKS[cache_key]:
res = TILE_SOURCE_CACHE.get(cache_key)
if res:
return res
realtime = api.get('realtime')
req_headers = client_headers.copy() or {}
client_kwargs = {
'headers': req_headers,
'connect_timeout': 10,
'request_timeout': 10,
'ca_certs': CA_CERTS,
'proxy_host': CONFIG.get('proxy_host'),
'proxy_port': CONFIG.get('proxy_port'),
'prepare_curl_callback': (
prepare_curl_socks5 if 'socks5' == CONFIG.get('proxy_type')
else None
)
}
if 'referer' in api:
req_headers['Referer'] = api['referer']
if api.has_metadata:
req_headers.update(await api.check_metadata(req_headers))
url = api.get_url(x, y, z, retina)
try:
response = await HTTP_CLIENT.fetch(url, **client_kwargs)
except tornado.httpclient.HTTPClientError as ex:
if ex.code == 404:
return (None, None)
elif 400 <= ex.code < 500:
raise
elif api.retry_on_error or ex.code == 503:
if api.has_metadata:
req_headers.update(await api.check_metadata(req_headers, True))
response = await HTTP_CLIENT.fetch(url, **client_kwargs)
elif ex.code >= 500:
raise
res = (response.body, response.headers['Content-Type'])
del response
if realtime:
TILE_SOURCE_CACHE.save(
cache_key, res, CONFIG.get('cache_realtime_ttl', 0))
else:
TILE_SOURCE_CACHE[cache_key] = res
return res
def calc_grid(x, y, z, sgnxy, off_fn, grid_num=8):
sgnx, sgny = sgnxy
sx0, sx1 = sorted((x, x+sgnx))
sy0, sy1 = sorted((y, y+sgny))
bbox = [float('inf'), float('inf'), float('-inf'), float('-inf')]
grid = []
tz = z
for i in range(grid_num+1):
gx = sx0 + i / grid_num
column = []
for j in range(grid_num+1):
gy = sy0 + j / grid_num
tx, ty, tz = off_fn(gx, gy, z)
column.append((gx - sx0, gy - sy0, tx, ty))
if i == 0 or i == grid_num:
bbox[0] = min(bbox[0], tx)
bbox[2] = max(bbox[2], tx)
if j == 0 or j == grid_num:
bbox[1] = min(bbox[1], ty)
bbox[3] = max(bbox[3], ty)
grid.append(column)
bbox = [
math.floor(bbox[0]), math.floor(bbox[1]),
math.ceil(bbox[2]), math.ceil(bbox[3]),
]
return bbox, grid, tz
def calc_pil_mesh(sgnxy, size, bbox, grid):
sgnx, sgny = sgnxy
szx, szy = size
dx = -bbox[0] if sgnx == 1 else bbox[2]
dy = -bbox[1] if sgny == 1 else bbox[3]
pil_mesh = []
for i, column in enumerate(grid[1:], 1):
for j, coords in enumerate(column[1:], 1):
sx0, sy0, tx0, ty0 = grid[i-1][j-1]
sx1, sy1, tx1, ty1 = coords
pil_mesh.append((
(int(sx0 * szx), int(sy0 * szy),
int(sx1 * szx), int(sy1 * szy)),
((tx0 * sgnx + dx) * szx, (ty0 * sgny + dy) * szy,
(tx0 * sgnx + dx) * szx, (ty1 * sgny + dy) * szy,
(tx1 * sgnx + dx) * szx, (ty1 * sgny + dy) * szy,
(tx1 * sgnx + dx) * szx, (ty0 * sgny + dy) * szy)
))
return pil_mesh
async def draw_tile(
source, z, x, y, retina=False, client_headers=None, img_format=None
):
api = APIS[source]
retina = ('url2x' in api and retina)
if api.fast_offset_check(z):
res = await get_tile(source, z, x, y, retina, client_headers)
return res
else:
if api.has_metadata:
await api.check_metadata(client_headers)
sgnxy = api.sgn
with warnings.catch_warnings():
warnings.simplefilter("ignore")
bbox, grid, realz = calc_grid(x, y, z, sgnxy, api.offset)
if (bbox[2] - bbox[0] == 1) and (bbox[3] - bbox[1] == 1):
realx = bbox[0] if sgnxy[0] == 1 else bbox[2] - 1
realy = bbox[1] if sgnxy[1] == 1 else bbox[3] - 1
res = await get_tile(source, realz, realx, realy,
retina, client_headers)
return res
futures = []
if sgnxy[0] == 1:
x_range = enumerate(range(bbox[0], bbox[2]))
else:
x_range = enumerate(range(bbox[2] - 1, bbox[0] - 1, -1))
if sgnxy[1] == 1:
y_range = enumerate(range(bbox[1], bbox[3]))
else:
y_range = enumerate(range(bbox[3] - 1, bbox[1] - 1, -1))
corners = tuple(itertools.product(x_range, y_range))
for x1, y1 in corners:
futures.append(get_tile(
source, realz, x1[1], y1[1], retina, client_headers))
tiles = await tornado.gen.multi(futures)
del futures
ioloop = tornado.ioloop.IOLoop.current()
result = await ioloop.run_in_executor(None, stitch_tiles,
tiles, corners, bbox, grid, sgnxy, source,
img_format or api.get('format'))
del tiles, corners, bbox, grid
return result
class PageHandler(tornado.web.RequestHandler):
def base_url(self):
return '%s://%s' % (self.request.protocol, self.request.host)
class TileHandler(PageHandler):
def initialize(self, *args, **kwargs):
self.headers = self.request.headers
async def get_tile_img(self, name, z, x, y, retina, img_format=None):
if name not in APIS:
raise tornado.web.HTTPError(404)
try:
client_headers = {
k:v for k,v in self.request.headers.items()
if k in HEADERS_WHITELIST
}
if not re_user_agent.match(client_headers.get('User-Agent', '')):
client_headers['User-Agent'] = DEFAULT_USER_AGENT
res = await draw_tile(
name, int(z), int(x), int(y), bool(retina),
client_headers, img_format
)
#except tornado.httpclient.HTTPError as ex:
# raise tornado.web.HTTPError(502)
#except KeyError:
#raise tornado.web.HTTPError(404)
except ValueError:
raise tornado.web.HTTPError(400)
except Exception:
raise
if not res[0]:
raise tornado.web.HTTPError(404)
if APIS[name].get('realtime'):
cache_ttl = CONFIG.get('cache_realtime_ttl', 0)
else:
cache_ttl = CONFIG['cache_ttl']
self.set_header('Content-Type', res[1])
self.set_header('Cache-Control', 'max-age=%d' % cache_ttl)
self.write(res[0])
class MainHandler(PageHandler):
HTML_TEMPLATE = """<!DOCTYPE html>
<html><head><title>TMS Tile Proxy Server</title>
</head><body><h1>TMS Tile Proxy Server</h1>
<h2><a href="/map">Demo</a></h2>
<h2>WMTS</h2><blockquote>{baseurl}/wmts</blockquote>
<h2>TMS Endpoints</h2>
<dl>{endpoints}</dl>
</body></html>"""
def get(self):
base_url = self.base_url()
html = self.HTML_TEMPLATE.format(
endpoints=''.join((
'<dt>{title}</dt>'
'<dd>{baseurl}/{name}/{{z}}/{{x}}/{{y}}{hidpi}</dd>'
).format(
title=APIS[s].get('name', s),