-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtiles_to_tiff.py
94 lines (73 loc) · 2.74 KB
/
tiles_to_tiff.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
import urllib.request
import os
import glob
import shutil
from tile_convert import bbox_to_xyz, tile_edges
from osgeo import gdal
temp_dir = os.path.join(os.path.dirname(__file__), 'temp')
def fetch_tile(x, y, z, tile_source):
url = tile_source.replace(
"{x}", str(x)).replace(
"{y}", str(y)).replace(
"{z}", str(z))
if not tile_source.startswith("http"):
return url.replace("file:///", "")
path = f'{temp_dir}/{x}_{y}_{z}.png'
req = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) tiles-to-tiff/1.0 (+https://github.com/jimutt/tiles-to-tiff)'
}
)
g = urllib.request.urlopen(req)
with open(path, 'b+w') as f:
f.write(g.read())
return path
def merge_tiles(input_pattern, output_path):
vrt_path = temp_dir + "/tiles.vrt"
gdal.BuildVRT(vrt_path, glob.glob(input_pattern))
gdal.Translate(output_path, vrt_path)
def georeference_raster_tile(x, y, z, path, tms):
bounds = tile_edges(x, y, z, tms)
gdal.Translate(os.path.join(temp_dir, f'{temp_dir}/{x}_{y}_{z}.tif'),
path,
outputSRS='EPSG:4326',
outputBounds=bounds,
rgbExpand='rgb')
def convert(tile_source, output_dir, bounding_box, zoom):
lon_min, lat_min, lon_max, lat_max = bounding_box
# Script start:
# extact {y} from tile_source, if -y then tms is True
parts = tile_source.split("/")
tms = parts[-1].split(".")[0].strip('{}')
if tms.startswith("-"):
#print("The string starts with '-'")
tile_source = tile_source.replace("-", "")
tms = True
#print(f"tile_source is {tile_source}")
else:
#print("The string does not start with '-'")
tms = False
#print(f"tms is {tms}")
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
x_min, x_max, y_min, y_max = bbox_to_xyz(
lon_min, lon_max, lat_min, lat_max, zoom, tms)
print(f"Fetching & georeferencing {(x_max - x_min + 1) * (y_max - y_min + 1)} tiles")
for x in range(x_min, x_max + 1):
for y in range(y_min, y_max + 1):
try:
png_path = fetch_tile(x, y, zoom, tile_source)
print(f"{x},{y} fetched")
georeference_raster_tile(x, y, zoom, png_path, tms)
except OSError:
print(f"Error, failed to get {x},{y}")
pass
print("Resolving and georeferencing of raster tiles complete")
print("Merging tiles")
merge_tiles(temp_dir + '/*.tif', output_dir + '/merged.tif')
print("Merge complete")
shutil.rmtree(temp_dir)