led_karte/get_coordinates.py

64 lines
2.4 KiB
Python
Raw Normal View History

2020-01-26 18:04:52 +00:00
#!/usr/bin/python
2020-01-26 19:30:56 +00:00
import requests, json, pickle, os.path, svgwrite
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 19:30:56 +00:00
YSPAN = NORTHERNMOST - SOUTHERNMOST
XSPAN = EASTERNMOST - WESTERNMOST
2020-01-26 18:04:52 +00:00
locations = {}
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
print ("kicked " + space + " too far north")
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
print ("kicked " + space + " too far south")
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
print ("kicked " + space + " too far west")
elif locations[space][1] > EASTERNMOST:
2020-01-26 19:30:56 +00:00
del german_locations[space]
2020-01-26 18:04:52 +00:00
print ("kicked " + space + " too far east")
2020-01-26 19:30:56 +00:00
dwg = svgwrite.Drawing('svgwrite-example.svg', profile='tiny')
dwg.add(svgwrite.image.Image(href="Karte_Deutschland.svg", size=(572,770)))
dwg.add(dwg.circle(center=(0,0), r=3, fill='black'))
for space in german_locations:
ypoint = (770 - (((german_locations[space][0] - SOUTHERNMOST) / YSPAN)* 770))
dwg.add(dwg.circle(center=(((locations[space][1] - WESTERNMOST) / XSPAN) *
572, ypoint), r=1, fill='black'))
dwg.save()