-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_legacy.py
59 lines (49 loc) · 2.18 KB
/
core_legacy.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
from PIL import Image
import json
import requests
from urllib.parse import urlencode, quote
from io import BytesIO
from math import ceil
def get_features(id):
params = {
"bild_id": str(id)
}
response = requests.get(f"https://nea.geofly.eu/api.php/getFeature?{urlencode(params)}")
content = response.content.decode()
return dict(json.loads(content))
def construct_all_meta(feature, for_zoom_level):
"""Use 1 specific feature from json["data"]["images"]["features"] (list) for parameter "feature" """
min_zoom = int(feature["properties"]["image_minzoom"])
max_zoom = int(feature["properties"]["image_maxzoom"])
if for_zoom_level < min_zoom or for_zoom_level > max_zoom:
raise ValueError(f"Invalid zoom level. max = {max_zoom}; min = {min_zoom}")
image_tile_base_id = feature["properties"]["bildflugnummer"]
divisor = 2 ** (max_zoom - for_zoom_level)
total_width = int(feature["properties"]["image_width"] / divisor)
total_height = int(feature["properties"]["image_height"] / divisor)
imagepath = str(feature["properties"]["imagepath"])
TILE_WIDTH, TILE_HEIGHT = (256, 256)
result = []
for y in range(ceil(total_height / TILE_HEIGHT)):
for x in range(ceil(total_width / TILE_WIDTH)):
url = f"https://nea.geofly.eu/tiles/{quote(str(image_tile_base_id))}/{quote(imagepath)}/{quote(str(for_zoom_level))}/{x}/{y}.jpg"
result.append((x * TILE_WIDTH, y * TILE_HEIGHT, url))
return ((total_width, total_height), result)
def download_all(meta):
image_size, tile_list = meta
img = Image.new("RGBA", image_size, 0)
try:
for i, (x, y, url) in enumerate(tile_list):
print(f"Downloading Tile #{i+1} of {len(tile_list)} [{url}]")
tile_response = requests.get(url)
try:
with Image.open(BytesIO(tile_response.content)) as tile:
img.paste(tile, box=(x, y))
except KeyboardInterrupt as ki:
raise ki
except Exception as e:
print(f"Tile #{i+1} failed: {e}")
pass
except KeyboardInterrupt:
print("Interrupting download.")
return img