2015-11-11 07:03:42 +00:00
|
|
|
import requests
|
|
|
|
import csv
|
|
|
|
from io import StringIO
|
2015-11-06 21:34:19 +00:00
|
|
|
|
2015-11-16 19:04:54 +00:00
|
|
|
from .model import Device, AddressOrigin
|
2015-11-06 21:34:19 +00:00
|
|
|
|
|
|
|
from geopy.geocoders import Nominatim
|
|
|
|
|
2015-11-11 07:03:42 +00:00
|
|
|
DDB_URL = "http://ddb.glidernet.org/download"
|
2015-11-06 21:34:19 +00:00
|
|
|
|
2015-11-15 07:51:40 +00:00
|
|
|
|
2016-01-05 23:23:45 +00:00
|
|
|
address_prefixes = {'F': 'FLR',
|
|
|
|
'O': 'OGN',
|
|
|
|
'I': 'ICA'}
|
2015-12-01 18:11:31 +00:00
|
|
|
|
2015-11-06 21:34:19 +00:00
|
|
|
|
2015-11-11 07:03:42 +00:00
|
|
|
def get_ddb(csvfile=None):
|
|
|
|
if csvfile is None:
|
|
|
|
r = requests.get(DDB_URL)
|
|
|
|
rows = '\n'.join(i for i in r.text.splitlines() if i[0] != '#')
|
|
|
|
address_origin = AddressOrigin.ogn_ddb
|
|
|
|
else:
|
|
|
|
r = open(csvfile, 'r')
|
|
|
|
rows = ''.join(i for i in r.readlines() if i[0] != '#')
|
2015-12-09 01:39:29 +00:00
|
|
|
address_origin = AddressOrigin.user_defined
|
2015-11-11 07:03:42 +00:00
|
|
|
|
|
|
|
data = csv.reader(StringIO(rows), quotechar="'", quoting=csv.QUOTE_ALL)
|
2015-11-06 21:34:19 +00:00
|
|
|
|
2015-11-11 07:03:42 +00:00
|
|
|
devices = list()
|
|
|
|
for row in data:
|
2015-11-16 19:04:54 +00:00
|
|
|
flarm = Device()
|
2015-11-11 07:03:42 +00:00
|
|
|
flarm.address_type = row[0]
|
|
|
|
flarm.address = row[1]
|
|
|
|
flarm.aircraft = row[2]
|
|
|
|
flarm.registration = row[3]
|
|
|
|
flarm.competition = row[4]
|
|
|
|
flarm.tracked = row[5] == 'Y'
|
|
|
|
flarm.identified = row[6] == 'Y'
|
|
|
|
|
|
|
|
flarm.address_origin = address_origin
|
|
|
|
|
2015-11-06 21:34:19 +00:00
|
|
|
devices.append(flarm)
|
|
|
|
|
|
|
|
return devices
|
|
|
|
|
|
|
|
|
2015-12-01 18:11:31 +00:00
|
|
|
def get_trackable(ddb):
|
2016-01-05 23:23:45 +00:00
|
|
|
l = []
|
|
|
|
for i in ddb:
|
|
|
|
if i.tracked and i.address_type in address_prefixes:
|
|
|
|
l.append('{}{}'.format(address_prefixes[i.address_type], i.address))
|
|
|
|
return l
|
2015-12-01 18:11:31 +00:00
|
|
|
|
|
|
|
|
2015-11-06 21:34:19 +00:00
|
|
|
def get_country_code(latitude, longitude):
|
|
|
|
geolocator = Nominatim()
|
|
|
|
location = geolocator.reverse("%f, %f" % (latitude, longitude))
|
2015-11-12 07:38:43 +00:00
|
|
|
try:
|
|
|
|
country_code = location.raw["address"]["country_code"]
|
|
|
|
except KeyError:
|
|
|
|
country_code = None
|
2015-11-06 21:34:19 +00:00
|
|
|
return country_code
|
2016-01-28 19:29:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def wgs84_to_sphere(receiver_beacon, aircraft_beacon):
|
|
|
|
from math import pi, asin, sqrt, sin, cos, atan2
|
|
|
|
deg2rad = pi / 180
|
|
|
|
rad2deg = 180 / pi
|
|
|
|
|
|
|
|
lat1 = receiver_beacon.latitude * deg2rad
|
|
|
|
lon1 = receiver_beacon.longitude * deg2rad
|
|
|
|
alt1 = receiver_beacon.altitude
|
|
|
|
|
|
|
|
lat2 = aircraft_beacon.latitude * deg2rad
|
|
|
|
lon2 = aircraft_beacon.longitude * deg2rad
|
|
|
|
alt2 = aircraft_beacon.altitude
|
|
|
|
|
|
|
|
distance = 6366000 * 2 * asin(sqrt((sin((lat1 - lat2) / 2))**2 + cos(lat1) * cos(lat2) * (sin((lon1 - lon2) / 2))**2))
|
|
|
|
theta = atan2(alt2 - alt1, distance) * rad2deg
|
|
|
|
phi = atan2(sin(lon1 - lon2) * cos(lat2), cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon1 - lon2)) * rad2deg
|
|
|
|
|
|
|
|
radius = sqrt(distance**2 + (alt2 - alt1)**2)
|
|
|
|
return radius, theta, phi
|