led_karte/draw_map.py

126 lines
4.0 KiB
Python
Raw Normal View History

2020-01-28 23:04:08 +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
2020-01-28 23:04:08 +00:00
URL = "https://directory.spaceapi.io/"
NORTHERNMOST = 55.05
EASTERNMOST = 15.033333
SOUTHERNMOST = 47.270108
WESTERNMOST = 5.866667
threshold = 0.10
YSPAN = NORTHERNMOST - SOUTHERNMOST
XSPAN = EASTERNMOST - WESTERNMOST
locations = {}
doorstati = {}
blacklist = ["Chaostreff Salzburg", "DevLoL", "CCC Basel", "Chaostreff Zürich",
2020-01-28 23:14:04 +00:00
"ChaosStuff", "Level2", "Bastli", "Maakplek", "TkkrLab", "Hack42",
"Hackerspace Nijmegen", "TDvenlo", "ACKspace"]
2020-01-28 23:04:08 +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-28 23:04:08 +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-28 23:04:08 +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)
returner.append(n1[2] or n2[2])
return returner
2020-01-28 23:14:04 +00:00
2020-01-28 23:04:08 +00:00
if os.path.isfile('locations.bin'):
2020-01-28 23:14:04 +00:00
print("using offline data...")
2020-01-28 23:04:08 +00:00
with open("locations.bin", "rb") as f:
locations = pickle.load(f)
else:
2020-01-28 23:14:04 +00:00
print("offline data not available, downloading...,")
r = requests.get(url=URL)
2020-01-28 23:04:08 +00:00
data = r.json()
for space in data:
spacerequest = None
try:
2020-01-28 23:14:04 +00:00
spacerequest = requests.get(url=data[space], timeout=1)
2020-01-28 23:04:08 +00:00
except requests.exceptions.RequestException as e: # This is the correct syntax
continue
try:
spacedata = spacerequest.json()
except json.JSONDecodeError as jde:
continue
if "location" in spacedata:
if "lat" in spacedata["location"]:
lat = spacedata["location"]["lat"]
lon = spacedata["location"]["lon"]
if "state" in spacedata:
if "open" in spacedata["state"]:
2020-01-28 23:14:04 +00:00
locations[space] = [float(lat), float(
lon), spacedata["state"]["open"]]
2020-01-28 23:04:08 +00:00
for space in locations:
2020-01-28 23:14:04 +00:00
print(space + " " + str(locations[space]))
2020-01-28 23:04:08 +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...")
2020-01-28 23:04:08 +00:00
german_locations = locations.copy()
for space in locations:
if locations[space][0] > NORTHERNMOST:
del german_locations[space]
elif locations[space][0] < SOUTHERNMOST:
del german_locations[space]
elif locations[space][1] < WESTERNMOST:
del german_locations[space]
elif locations[space][1] > EASTERNMOST:
del german_locations[space]
for space in blacklist:
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-28 23:04:08 +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-28 23:04:08 +00:00
for space in finallist:
#print(space + " " + str(finallist[space][0]) + " " + str(finallist[space][1]))
print(str(finallist[space][0]) + " " + str(finallist[space][1]) + " {" +
2020-01-28 23:14:04 +00:00
space + "} Doorstatus: " + str(finallist[space][2]))
2020-01-28 23:04:08 +00:00
dwg = svgwrite.Drawing('svgwrite-example.svg', profile='tiny')
dwg.add(svgwrite.image.Image(href="Karte_Deutschland.svg", size=(592, 801)))
2020-01-28 23:14:04 +00:00
dwg.add(dwg.circle(center=(0, 0), r=3, fill='black'))
2020-01-28 23:04:08 +00:00
for space in finallist:
2020-01-28 23:14:04 +00:00
ypoint = (801 - (((finallist[space][0] - SOUTHERNMOST) / YSPAN) * 801))
2020-01-28 23:04:08 +00:00
xpoint = ((finallist[space][1] - WESTERNMOST) / XSPAN) * 592
if finallist[space][2]:
dwg.add(dwg.circle(center=(xpoint, ypoint), r=5, fill='green'))
else:
dwg.add(dwg.circle(center=(xpoint, ypoint), r=5, fill='green'))
dwg.save()