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
|
2016-02-03 22:09:11 +00:00
|
|
|
from geopy.exc import GeopyError
|
2015-11-06 21:34:19 +00:00
|
|
|
|
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:
|
2016-02-03 22:19:25 +00:00
|
|
|
l.append("{}{}".format(address_prefixes[i.address_type], i.address))
|
2016-01-05 23:23:45 +00:00
|
|
|
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()
|
2015-11-12 07:38:43 +00:00
|
|
|
try:
|
2016-02-03 22:19:25 +00:00
|
|
|
location = geolocator.reverse("{}, {}".format(latitude, longitude))
|
|
|
|
country_code = location.raw['address']['country_code']
|
2015-11-12 07:38:43 +00:00
|
|
|
except KeyError:
|
|
|
|
country_code = None
|
2016-02-03 22:09:11 +00:00
|
|
|
except GeopyError:
|
|
|
|
country_code = None
|
2015-11-06 21:34:19 +00:00
|
|
|
return country_code
|
2016-01-28 19:29:08 +00:00
|
|
|
|
|
|
|
|
2016-01-29 04:22:07 +00:00
|
|
|
def haversine_distance(location0, location1):
|
|
|
|
from math import asin, sqrt, sin, cos, atan2, radians, degrees
|
2016-01-28 19:29:08 +00:00
|
|
|
|
2016-01-29 04:22:07 +00:00
|
|
|
lat0 = radians(location0[0])
|
|
|
|
lon0 = radians(location0[1])
|
|
|
|
lat1 = radians(location1[0])
|
|
|
|
lon1 = radians(location1[1])
|
2016-01-28 19:29:08 +00:00
|
|
|
|
2016-01-29 04:22:07 +00:00
|
|
|
distance = 6366000 * 2 * asin(sqrt((sin((lat0 - lat1) / 2))**2 + cos(lat0) * cos(lat1) * (sin((lon0 - lon1) / 2))**2))
|
|
|
|
phi = degrees(atan2(sin(lon0 - lon1) * cos(lat1), cos(lat0) * sin(lat1) - sin(lat0) * cos(lat1) * cos(lon0 - lon1)))
|
2016-01-28 19:29:08 +00:00
|
|
|
|
2016-01-29 04:22:07 +00:00
|
|
|
return distance, phi
|