-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_tiles.py
74 lines (60 loc) · 2.57 KB
/
generate_tiles.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
import json
import os
import subprocess
class Utils():
@staticmethod
def convert_utf8_to_str(input):
if isinstance(input, dict):
return {Utils.convert_utf8_to_str(key): Utils.convert_utf8_to_str(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [Utils.convert_utf8_to_str(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
@staticmethod
def read_config_file(filename):
file = open(filename)
str = file.read()
str = str.replace('\n', '')
return json.loads(str, object_hook=Utils.convert_utf8_to_str)
class GmapTileGenerator():
_config = None
def __init__(self, cfg_file):
GmapTileGenerator._config = Utils.read_config_file(cfg_file)
@staticmethod
def _check_images_dir():
dir = GmapTileGenerator._config['images-dir']
if not os.path.exists(dir):
os.makedirs(dir)
return True
return False
@staticmethod
def _get_image_filename(row_index, col_index):
return 'tile-' + format(row_index, '02') + '-' + format(col_index, '02') + '.png'
@staticmethod
def generate_tiles():
if not GmapTileGenerator._check_images_dir():
print 'Remove folder \'' + GmapTileGenerator._config['images-dir'] + '\' and retry.'
return
config = GmapTileGenerator._config
zoom = config['zoom']
start_coord = config['start-coord']
end_coord = config['end-coord']
row_index = 0
latitude = start_coord['lat']
while latitude > end_coord['lat']:
longitude = start_coord['long']
col_index = 0
while longitude < end_coord['long']:
url = config['url-template'].replace('__LAT__', str(latitude)).replace('__LONG__', str(longitude)).replace('__ZOOM__', str(zoom))
image_file_path = config['images-dir'] + '/' + GmapTileGenerator._get_image_filename(row_index, col_index)
curl_command = 'curl "' + url + '" > ' + image_file_path
print 'Fetching tile at zoom: ' + str(zoom) + ' with latitude: ' + str(latitude) + ' and longitude: ' + str(longitude) + ', to image file: ' + image_file_path
subprocess.call(curl_command, shell=True)
longitude += config['increment']
col_index += 1
latitude -= config['increment']
row_index += 1
gtg = GmapTileGenerator('config.json')
gtg.generate_tiles()