-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.js
70 lines (65 loc) · 1.73 KB
/
geo.js
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
var settings=require('./settings.js');
var arrl_section=require('./arrl_sections.json');
var qrz=require('./qrz.js');
var random_bounds=function(min,max){
return(Math.random()*(max-min)+min);
};
var random_center=function(center,range){
return((Math.random()*2-1)*range+center);
};
//Load prefix data into memory
var prefixData={}
exports.init=function(callback){
var sqlite3=require('sqlite3').verbose();
var db=new sqlite3.Database(settings.n1mm_admin_db);
db.serialize(function(){
db.each("select * from Prefixes left join CTYDAT on CTYDAT.MasterPrefix=Prefixes.MasterPrefix",function(err,row){
if(err) throw(err);
row.Longitude*=-1;
prefixData[row.Prefix]=row;
},function(){
qrz.init(function(err,result){
if(err) {
console.log("QRZ Init Failed: "+err)
}
callback(null,result);
});
});
});
db.close();
};
exports.resolve=function(row,callback){
qrz.geo(row.Call,function(err,result){
if(result && result.lat && result.lon && (result.geoloc=='user' || result.geoloc=='geocode' || result.geoloc=='grid' || result.geocode=='zip')){
callback({
'latitude':result.lat,
'longitude':result.lon,
'source':'qrz'
});
return;
}
try{
section=arrl_section[row.Sect.toUpperCase()];
callback({
'latitude':random_bounds(section['lat min'],section['lat max']),
'longitude':random_bounds(section['long min'],section['long max']),
'source':'section'
});
return;
}catch(TypeError){
}
for(var i=row.Call.length;i>0;i--){
data=prefixData[row.Call.slice(0,i)];
if(data){
callback({
'latitude':random_center(data.Latitude,4),
'longitude':random_center(data.Longitude,4),
'source':'prefix'
});
return;
}
}
callback(null);
return;
});
};