-
Notifications
You must be signed in to change notification settings - Fork 0
/
owmdb2gsdb.py
executable file
·74 lines (63 loc) · 2.35 KB
/
owmdb2gsdb.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
#!/usr/bin/python
# (c) 2013 Trever L. Adams
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
# This is a simple translation of a function found at:
# http://en.wikipedia.org/wiki/Maidenhead_Locator_System
# (c) 2012 Chris Ruvolo. Licensed under a 2-clause BSD license.
def latlon2gs(lat, lon):
grid = ""
five60 = float(5)/float(60)
lon = lon + 180
lat = lat + 90
grid += chr(ord('A') + int(lon / 20))
grid += chr(ord('A') + int(lat / 10))
grid += chr(ord('0') + int((lon % 20)/2))
grid += chr(ord('0') + int((lat % 10)/1))
grid += chr(ord('a') + int((lon - (int(lon/2)*2)) / (five60)))
grid += chr(ord('a') + int((lat - (int(lat/1)*1)) / (2.5/60)))
return grid
db_files = {}
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
ensure_dir("./gsdb/")
f=open("db.csv", "r")
line = f.readline() # Trash CSV header
line = f.readline()
while line:
words = line.split()
try:
gs = latlon2gs(float(words[1]), float(words[2]))
except:
print line + "If the above line started with bssid, it is safe to ignore, if not contact the author of the program\n"
gs = gs[0:6]
try:
if(db_files[gs]):
db_files[gs].write(words[0] + "\t" + words[1] + "\t" + words[2] + "\n")
except:
if(len(db_files) > 100):
for key, item in db_files.items():
db_files[key].close()
del db_files[key]
if os.path.exists("./gsdb/" + gs + ".csv"):
db_files[gs] = open("./gsdb/" + gs + ".csv", "a+b")
db_files[gs].write(words[0] + "\t" + words[1] + "\t" + words[2] + "\n")
else:
db_files[gs] = open("./gsdb/" + gs + ".csv", "w+b")
db_files[gs].truncate
db_files[gs].write("bssid\tlat\tlon\n")
db_files[gs].write(words[0] + "\t" + words[1] + "\t" + words[2] + "\n")
line = f.readline()
f.close()