led_karte/get_coordinates.py

142 lines
4.2 KiB
Python
Raw Normal View History

2020-01-26 18:04:52 +00:00
#!/usr/bin/python
2020-01-28 23:14:04 +00:00
import requests
import json
import pickle
import os.path
import svgwrite
import math
import sys
import concurrent.futures
2020-01-26 18:04:52 +00:00
URL = "https://directory.spaceapi.io/"
NORTHERNMOST = 55.05
EASTERNMOST = 15.033333
SOUTHERNMOST = 47.270108
WESTERNMOST = 5.866667
2020-01-26 22:30:03 +00:00
threshold = 0.10
2020-01-26 19:30:56 +00:00
YSPAN = NORTHERNMOST - SOUTHERNMOST
XSPAN = EASTERNMOST - WESTERNMOST
2020-01-26 18:04:52 +00:00
locations = {}
2020-01-28 23:14:04 +00:00
ignorelist = ["Chaostreff Salzburg", "DevLoL", "CCC Basel", "Chaostreff Zürich",
"ChaosStuff", "Level2", "Bastli", "Maakplek", "TkkrLab", "Hack42",
"Hackerspace Nijmegen", "TDvenlo", "ACKspace"]
2020-01-26 22:30:03 +00:00
def dist(n1, n2):
y = n1[0] - n2[0]
x = n1[1] - n2[1]
return math.sqrt(math.pow(x, 2) + math.pow(y, 2))
2020-01-28 23:14:04 +00:00
2020-01-26 22:30:03 +00:00
def conflict(targetlist, node):
returner = None
for element in targetlist:
if dist(node, targetlist[element]) < threshold:
returner = element
continue
return returner
2020-01-28 23:14:04 +00:00
2020-01-26 22:30:03 +00:00
def merge(n1, n2):
lat = (n1[0] + n2[0]) / 2
lon = (n1[1] + n2[1]) / 2
returner = []
returner.append(lat)
returner.append(lon)
2020-01-30 00:53:33 +00:00
for i in range(2, len(n1)):
returner.append(n1[i])
for i in range(2, len(n2)):
returner.append(n2[i])
2020-01-26 22:30:03 +00:00
return returner
2020-01-28 23:14:04 +00:00
def get_space_location(space):
try:
spacerequest = requests.get(url=data[space], timeout=1)
spacedata = spacerequest.json()
2020-01-30 00:53:33 +00:00
except requests.exceptions.RequestException as _:
2020-01-28 23:14:04 +00:00
return
except json.JSONDecodeError as _:
return
if "location" in spacedata:
if "lat" in spacedata["location"]:
lat = spacedata["location"]["lat"]
lon = spacedata["location"]["lon"]
2020-01-30 00:53:33 +00:00
return [float(lat), float(lon), data[space]]
2020-01-28 23:14:04 +00:00
2020-01-26 18:04:52 +00:00
if os.path.isfile('locations.bin'):
2020-01-28 23:14:04 +00:00
print("using offline data...", file=sys.stderr)
2020-01-26 18:04:52 +00:00
with open("locations.bin", "rb") as f:
locations = pickle.load(f)
else:
2020-01-30 00:53:33 +00:00
print("offline data not available, downloading...", file=sys.stderr)
2020-01-28 23:14:04 +00:00
r = requests.get(url=URL)
2020-01-26 18:04:52 +00:00
data = r.json()
2020-01-28 23:14:04 +00:00
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
futures = {}
for space in data:
futures[space] = executor.submit(get_space_location, space)
for space in data:
location = futures[space].result()
if location:
locations[space] = location
print("done loading space data", file=sys.stderr)
2020-01-26 18:04:52 +00:00
for space in locations:
2020-01-28 23:14:04 +00:00
print(space + " " + str(locations[space]))
2020-01-26 18:04:52 +00:00
with open("locations.bin", "wb") as f:
pickle.dump(locations, f, pickle.HIGHEST_PROTOCOL)
2020-01-28 23:14:04 +00:00
print("Removing non-german points...", file=sys.stderr)
2020-01-26 19:30:56 +00:00
german_locations = locations.copy()
2020-01-26 18:04:52 +00:00
for space in locations:
if locations[space][0] > NORTHERNMOST:
2020-01-26 19:30:56 +00:00
del german_locations[space]
2020-01-26 18:04:52 +00:00
elif locations[space][0] < SOUTHERNMOST:
2020-01-26 19:30:56 +00:00
del german_locations[space]
2020-01-26 18:04:52 +00:00
elif locations[space][1] < WESTERNMOST:
2020-01-26 19:30:56 +00:00
del german_locations[space]
2020-01-26 18:04:52 +00:00
elif locations[space][1] > EASTERNMOST:
2020-01-26 19:30:56 +00:00
del german_locations[space]
2020-01-28 23:14:04 +00:00
for space in ignorelist:
2020-01-26 22:30:03 +00:00
del german_locations[space]
finallist = {}
while german_locations:
n1 = next(iter(german_locations))
conflictnode = conflict(finallist, german_locations[n1])
if conflictnode == None:
2020-01-28 23:14:04 +00:00
finallist.update({n1: german_locations[n1]})
2020-01-26 22:30:03 +00:00
del german_locations[n1]
else:
mergenode = merge(german_locations[n1], finallist[conflictnode])
del german_locations[n1]
del finallist[conflictnode]
2020-01-28 23:14:04 +00:00
german_locations.update(
{"MERGED: " + n1 + " " + conflictnode: mergenode})
2020-01-26 22:30:03 +00:00
for space in finallist:
2020-01-28 23:04:08 +00:00
print(str(finallist[space][0]) + " " + str(finallist[space][1]) + " {" +
2020-01-28 23:14:04 +00:00
space + "}")
2020-01-28 23:04:08 +00:00
2020-01-30 00:53:33 +00:00
dwg = svgwrite.Drawing('dots.svg', profile='tiny')
2020-01-28 23:04:08 +00:00
dwg.add(svgwrite.image.Image(href="Karte_Deutschland.svg", size=(586, 793)))
2020-01-26 22:30:03 +00:00
for space in finallist:
2020-01-28 23:14:04 +00:00
ypoint = (793 - (((finallist[space][0] - SOUTHERNMOST) / YSPAN) * 793))
2020-01-28 23:04:08 +00:00
xpoint = ((finallist[space][1] - WESTERNMOST) / XSPAN) * 586
dwg.add(dwg.circle(center=(xpoint, ypoint), r=5, fill='green'))
2020-01-28 23:14:04 +00:00
2020-01-26 19:30:56 +00:00
dwg.save()
2020-01-30 00:53:33 +00:00
ledconf = open("conf.yml", "w")
i = 0
for space in finallist:
2020-01-30 20:05:55 +00:00
ledconf.write("#led" + str(i) + ":\n-\n")
2020-01-30 00:53:33 +00:00
for u in range(2, len(finallist[space])):
ledconf.write(" - " + finallist[space][u] + "\n")
i = i + 1