Polyhoods: Introduce database support and read functions

Polyhoods need to be read manually. Running
scripts/readpolyhoods.py will erase both tables and reread data.

At the moment, the URL points to a test setup.

This requires changes to the MySQL database!

This is meant for later use and does NOT add any data to the
Monitoring at the moment.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-11-24 02:02:37 +01:00
parent b8409d3abe
commit bb61351fbf
3 changed files with 79 additions and 0 deletions

View File

@ -75,6 +75,43 @@ mysql.execute("""
ADD KEY `lng` (`lng`)
""")
mysql.execute("""
CREATE TABLE `polygons` (
`id` int(10) UNSIGNED NOT NULL,
`polyid` int(10) UNSIGNED NOT NULL,
`lat` double NOT NULL,
`lon` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
""")
mysql.execute("""
ALTER TABLE `polygons`
ADD PRIMARY KEY (`id`),
ADD KEY `polyid` (`polyid`)
""")
mysql.execute("""
ALTER TABLE `polygons`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT
""")
mysql.execute("""
CREATE TABLE `polyhoods` (
`polyid` int(10) UNSIGNED NOT NULL,
`hoodid` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
""")
mysql.execute("""
ALTER TABLE `polyhoods`
ADD PRIMARY KEY (`polyid`)
""")
mysql.execute("""
ALTER TABLE `polyhoods`
MODIFY `polyid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT
""")
mysql.commit()
mysql.close()

View File

@ -41,6 +41,33 @@ def update_hoods_v2(mysql):
except urllib.error.HTTPError as e:
return
def update_hoods_poly(mysql):
try:
#with urllib.request.urlopen("http://keyserver.freifunk-franken.de/v2/hoods.php") as url:
with urllib.request.urlopen("https://lauch.org/keyxchange/hoods.php") as url:
hoodskx = json.loads(url.read().decode())
mysql.execute("DELETE FROM polygons",())
mysql.execute("DELETE FROM polyhoods",())
for kx in hoodskx:
for polygon in kx.get("polygons",()):
mysql.execute("""
INSERT INTO polyhoods (hoodid)
VALUES (%s)
""",(kx["id"],))
newid = mysql.cursor().lastrowid
vertices = []
for p in polygon:
vertices.append((newid,p["lat"],p["lon"],))
mysql.executemany("""
INSERT INTO polygons (polyid, lat, lon)
VALUES (%s, %s, %s)
""",vertices)
except urllib.error.HTTPError as e:
return
def update_hoods_v1(mysql):
try:
with urllib.request.urlopen("http://keyserver.freifunk-franken.de/fff/hoods.php") as url:

15
scripts/readpolyhoods.py Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/python3
# Execute manually
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ffmap.hoodtools import update_hoods_poly
from ffmap.mysqltools import FreifunkMySQL
mysql = FreifunkMySQL()
update_hoods_poly(mysql)
mysql.commit()
mysql.close()