This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
100 lines (88 loc) · 1.46 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from math import sin, cos, acos, radians
import sqlite3
DATABASE_NAME = 'isdal-geo.sqlite'
DATA_DIRECTORY = 'data'
GENERATED_DIRECTORY = 'generated'
AIRPORTS = 'airports-extended.dat'
TRAINSTATIONS = 'european-train-stations.csv'
GEONAMES = 'allCountries.zip'
GEONAMES_DATA = 'allCountries.txt'
# Description: http://www.geonames.org/export/codes.html
geonames_allowed_types = set(['P'])
MINIMAL_KM_STEPS = 100
MAXIMAL_KM_STEPS = 3000
interesting_country_codes = set([
'AD',
'AL',
'AT',
'AX',
'BA',
'BE',
'BG',
'BY',
'CH',
'CS',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FO',
'FR',
'GB',
'GG',
'GI',
'GR',
'HR',
'HU',
'IE',
'IM',
'IS',
'IT',
'JE',
'LI',
'LT',
'LU',
'LV',
'MC',
'MD',
'ME',
'MK',
'MT',
'NL',
'NO',
'PL',
'PT',
'RO',
'RS',
'RU',
'SE',
'SI',
'SJ',
'SK',
'SM',
'UA',
'VA',
'XK',
])
def distance(p1_lat,p1_long,p2_lat,p2_long):
# Based on https://gist.github.com/willperkins/1051879
if p1_lat == p2_lat and p1_long == p2_long:
return 0.0
multiplier = 6371.0
return (multiplier *
acos(
cos(radians(p1_lat)) *
cos(radians(p2_lat)) *
cos(radians(p2_long) - radians(p1_long)) +
sin(radians(p1_lat)) * sin(radians(p2_lat)))
)
def sqlite_with_distance():
conn = sqlite3.connect(DATABASE_NAME)
sqlite3.enable_callback_tracebacks(True)
conn.execute('pragma journal_mode=wal')
conn.create_function('distance', 4, distance)
conn.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')
return conn