]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/viewfinder/build-db.py
Use data from viewfinderpanoramas.org
[GpsPrune.git] / src / tim / prune / function / srtm / viewfinder / build-db.py
1 #!/usr/bin/python3
2
3 import os
4 import urllib.request
5
6 from html.parser import HTMLParser
7
8 COMMON_PREFIX = "http://viewfinderpanoramas.org/"
9 COMMON_SUFFIX = ".zip"
10
11 def fetch_new_map():
12     fetch_to_file(COMMON_PREFIX + "Coverage%20map%20viewfinderpanoramas_org3.htm",
13                   "coverage-dem3.html")
14     fetch_to_file(COMMON_PREFIX + "Coverage%20map%20viewfinderpanoramas_org1.htm",
15                   "coverage-dem1.html")
16
17 def fetch_to_file(url, fname):
18     if os.path.exists(fname):
19         return
20     req = urllib.request.urlopen(url)
21     assert req.getcode() == 200, req.read()
22     with open(fname, "w") as f:
23         f.write(req.read().decode("utf-8"))
24
25 fetch_new_map()
26
27 class CoverageDEM3MapParser(HTMLParser):
28     def __init__(self, *args, **kwargs):
29         super().__init__(*args, **kwargs)
30         self.coords_to_link = {}
31         self.image_scale = 0
32
33     @staticmethod
34     def _get_attr(attrs, attrname):
35         return next(val for key, val in attrs if key == attrname)
36
37     def handle_starttag(self, tag, attrs):
38         if tag == "area":
39             coordattr = self._get_attr(attrs, "coords")
40             coords = tuple(int(c) for c in coordattr.split(", "))
41             assert len(coords) == 4, "Unexpected tag: %s, %s" % (tag, attrs)
42
43             link = self._get_attr(attrs, "href")
44             assert link.startswith(COMMON_PREFIX)
45             link = link[len(COMMON_PREFIX):]
46             assert link.endswith(COMMON_SUFFIX)
47             link = link[:-len(COMMON_SUFFIX)]
48
49             self.coords_to_link[coords] = link
50         if tag == "img":
51             width = int(self._get_attr(attrs, "width"))
52             height = int(self._get_attr(attrs, "height"))
53             assert width == 2 * height
54             self.image_scale = width / 360
55
56 def populate_links(fname, lats_to_link):
57     parser = CoverageDEM3MapParser()
58     with open(fname) as f:
59         parser.feed(f.read())
60
61     scale = parser.image_scale
62     for coords in parser.coords_to_link:
63         xmin, ymin, xmax, ymax = coords
64         for x in range(int(xmin / scale), int(xmax / scale)):
65             for y in range(int(ymin / scale) + 1, int(ymax / scale) + 1):
66                 lats_to_link[90 - y, x - 180] = parser.coords_to_link[coords]
67
68 def self_test_dem3(lats_to_link):
69     lat = 44
70     while lat <= 47:
71         lng = 0
72         while lng <= 5:
73             assert "L31" in lats_to_link[lat, lng]
74             lng += 1
75         lat += 1
76
77 def self_test_dem1(lats_to_link):
78     assert "TATRA" in lats_to_link[49, 19]
79     for lat, lng in lats_to_link:
80         assert -90 <= lat <= 89
81         assert -180 <= lng <= 179, lng
82
83 def write_db(dest, lats_to_link):
84     with open(dest, "w") as f:
85         for lat in range(-90, 90):
86             for lng in range(-180, 180):
87                 if (lat, lng) in lats_to_link:
88                     print("%d %d %s" % (lat, lng,
89                                         lats_to_link.get((lat, lng))),
90                           file=f)
91
92 lats_to_link = {}
93 populate_links("coverage-dem3.html", lats_to_link)
94 self_test_dem3(lats_to_link)
95 populate_links("coverage-dem1.html", lats_to_link)
96 self_test_dem1(lats_to_link)
97
98 write_db("tiles.dat", lats_to_link)