]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - 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
diff --git a/src/tim/prune/function/srtm/viewfinder/build-db.py b/src/tim/prune/function/srtm/viewfinder/build-db.py
new file mode 100755 (executable)
index 0000000..40e8d1c
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/python3
+
+import os
+import urllib.request
+
+from html.parser import HTMLParser
+
+COMMON_PREFIX = "http://viewfinderpanoramas.org/"
+COMMON_SUFFIX = ".zip"
+
+def fetch_new_map():
+    fetch_to_file(COMMON_PREFIX + "Coverage%20map%20viewfinderpanoramas_org3.htm",
+                  "coverage-dem3.html")
+    fetch_to_file(COMMON_PREFIX + "Coverage%20map%20viewfinderpanoramas_org1.htm",
+                  "coverage-dem1.html")
+
+def fetch_to_file(url, fname):
+    if os.path.exists(fname):
+        return
+    req = urllib.request.urlopen(url)
+    assert req.getcode() == 200, req.read()
+    with open(fname, "w") as f:
+        f.write(req.read().decode("utf-8"))
+
+fetch_new_map()
+
+class CoverageDEM3MapParser(HTMLParser):
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.coords_to_link = {}
+        self.image_scale = 0
+
+    @staticmethod
+    def _get_attr(attrs, attrname):
+        return next(val for key, val in attrs if key == attrname)
+
+    def handle_starttag(self, tag, attrs):
+        if tag == "area":
+            coordattr = self._get_attr(attrs, "coords")
+            coords = tuple(int(c) for c in coordattr.split(", "))
+            assert len(coords) == 4, "Unexpected tag: %s, %s" % (tag, attrs)
+
+            link = self._get_attr(attrs, "href")
+            assert link.startswith(COMMON_PREFIX)
+            link = link[len(COMMON_PREFIX):]
+            assert link.endswith(COMMON_SUFFIX)
+            link = link[:-len(COMMON_SUFFIX)]
+
+            self.coords_to_link[coords] = link
+        if tag == "img":
+            width = int(self._get_attr(attrs, "width"))
+            height = int(self._get_attr(attrs, "height"))
+            assert width == 2 * height
+            self.image_scale = width / 360
+
+def populate_links(fname, lats_to_link):
+    parser = CoverageDEM3MapParser()
+    with open(fname) as f:
+        parser.feed(f.read())
+
+    scale = parser.image_scale
+    for coords in parser.coords_to_link:
+        xmin, ymin, xmax, ymax = coords
+        for x in range(int(xmin / scale), int(xmax / scale)):
+            for y in range(int(ymin / scale) + 1, int(ymax / scale) + 1):
+                lats_to_link[90 - y, x - 180] = parser.coords_to_link[coords]
+
+def self_test_dem3(lats_to_link):
+    lat = 44
+    while lat <= 47:
+        lng = 0
+        while lng <= 5:
+            assert "L31" in lats_to_link[lat, lng]
+            lng += 1
+        lat += 1
+
+def self_test_dem1(lats_to_link):
+    assert "TATRA" in lats_to_link[49, 19]
+    for lat, lng in lats_to_link:
+        assert -90 <= lat <= 89
+        assert -180 <= lng <= 179, lng
+
+def write_db(dest, lats_to_link):
+    with open(dest, "w") as f:
+        for lat in range(-90, 90):
+            for lng in range(-180, 180):
+                if (lat, lng) in lats_to_link:
+                    print("%d %d %s" % (lat, lng,
+                                        lats_to_link.get((lat, lng))),
+                          file=f)
+
+lats_to_link = {}
+populate_links("coverage-dem3.html", lats_to_link)
+self_test_dem3(lats_to_link)
+populate_links("coverage-dem1.html", lats_to_link)
+self_test_dem1(lats_to_link)
+
+write_db("tiles.dat", lats_to_link)