led_karte/get_coordinates.py

103 lines
3.4 KiB
Python
Raw Normal View History

2020-01-26 18:04:52 +00:00
#!/usr/bin/python
2020-01-26 22:30:03 +00:00
import requests, json, pickle, os.path, svgwrite, math
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-26 22:30:03 +00:00
blacklist = ["Chaostreff Salzburg", "DevLoL", "CCC Basel", "Chaostreff Zürich", "ChaosStuff"]
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))
def conflict(targetlist, node):
returner = None
for element in targetlist:
if dist(node, targetlist[element]) < threshold:
returner = element
continue
return returner
def merge(n1, n2):
lat = (n1[0] + n2[0]) / 2
lon = (n1[1] + n2[1]) / 2
returner = []
returner.append(lat)
returner.append(lon)
return returner
2020-01-26 18:04:52 +00:00
if os.path.isfile('locations.bin'):
print ("using offline data...")
with open("locations.bin", "rb") as f:
locations = pickle.load(f)
else:
print ("offline data not available, downloading...,")
r = requests.get(url = URL)
data = r.json()
for space in data:
spacerequest = None
try:
spacerequest = requests.get(url = data[space], timeout=1)
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"]
locations[space] = [float(lat), float(lon)]
for space in locations:
print (space + " " + str(locations[space]))
with open("locations.bin", "wb") as f:
pickle.dump(locations, f, pickle.HIGHEST_PROTOCOL)
print ("Removing non-german points...")
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-26 22:30:03 +00:00
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:
finallist.update({n1 : german_locations[n1]})
del german_locations[n1]
else:
mergenode = merge(german_locations[n1], finallist[conflictnode])
del german_locations[n1]
del finallist[conflictnode]
german_locations.update({"MERGED: " + n1 + " " + conflictnode : mergenode})
for space in finallist:
print(space + " " + str(finallist[space][0]) + " " + str(finallist[space][1]))
2020-01-26 19:30:56 +00:00
dwg = svgwrite.Drawing('svgwrite-example.svg', profile='tiny')
2020-01-26 22:30:03 +00:00
#dwg.add(svgwrite.image.Image(href="Karte_Deutschland.svg", size=(586, 793)))
2020-01-26 19:30:56 +00:00
dwg.add(dwg.circle(center=(0,0), r=3, fill='black'))
2020-01-26 22:30:03 +00:00
for space in finallist:
ypoint = (793 - (((finallist[space][0] - SOUTHERNMOST) / YSPAN)* 793))
dwg.add(dwg.circle(center=(((finallist[space][1] - WESTERNMOST) / XSPAN) *
586, ypoint), r=5, fill='green'))
2020-01-26 19:30:56 +00:00
dwg.save()