Skip to content

Commit

Permalink
Query all cache databases first before trying online services.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan.ramseyer committed Dec 12, 2018
1 parent 1c01279 commit cffa51e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 72 deletions.
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Underneath the hood, the following data sources are used in descending order:
1. [OpenCellId: offline database](https://www.opencellid.org/downloads.php)
2. [Mozilla Location Service: offline database](https://location.services.mozilla.com/downloads)
3. Google GLM MMAP: self created cache database
4. [Google GLM MMAP: online service](https://github.com/kolonist/bscoords)
5. OpenCellId (effectively a fallback to UnwiredLabs): self created cache database
4. OpenCellId (effectively a fallback to UnwiredLabs): self created cache database
5. [Google GLM MMAP: online service](https://github.com/kolonist/bscoords)
6. [OpenCellId (effectively a fallback to UnwiredLabs): online service](http://wiki.opencellid.org/wiki/API)
7. Default location (Latitude = 46.909009, Longitude = 7.360584 and Range = 4294967295)

Expand Down Expand Up @@ -52,7 +52,7 @@ Use environment variables PORT and IP for different port/host. F.e.:

PORT=1337 OPENCELLID_API_KEY=<YOURS> node cell-geolocation.js

## Queries and Responses (as of 6. Dec. 2018)
## Queries and Responses (as of December 2018)

Query which can be answered by using the OpenCellId database (1):

Expand All @@ -61,48 +61,53 @@ Query which can be answered by using the OpenCellId database (1):

Query which can be answered by using the Mozilla Location Service database (2):

curl -s 'http://localhost:5265/?mcc=222&mnc=10&lac=16085&cellid=26855411'
{"lat":44.4104554,"lon":8.8969816,"range":275}
curl -s 'http://localhost:5265/?mcc=204&mnc=4&lac=203&cellid=48045204'
{"lat":51.4649645,"lon":4.3089691,"range":47}

Query which can be answered by using the Google GLM MMAP online service (4):
Query which can be answered by using the Google GLM MMAP online service (5):

curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3001&cellid=66836061'
{"lat":51.088892,"lon":4.456987,"range":1492}
curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3034&cellid=65927425'
{"lat":51.183995,"lon":4.360154,"range":1204}

Query which can now be answered by using the Google GLM MMAP cache database (3):

curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3001&cellid=66836061'
curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3034&cellid=65927425'
{"lat":51.088892,"lon":4.456987,"range":1492}

Query which can be answered by using the OpenCellId online service (6):

curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3011&cellid=66991912'
{"lat":52.326586,"lon":5.093599,"range":4545}

Query which can now be answered by using the OpenCellId cache database (5):
curl -s 'http://localhost:5265/?mcc=204&mnc=4&lac=207&cellid=48051720'
{"lat":50.970299,"lon":5.79788,"range":4545}

curl -s 'http://localhost:5265/?mcc=206&mnc=1&lac=3011&cellid=66991912'
{"lat":52.326586,"lon":5.093599,"range":4545}
Query which can now be answered by using the OpenCellId cache database (4):

Query with existing cell tower which can only be answered by using the default location (7):

curl -s 'http://localhost:5265/?mcc=999&mnc=1&lac=3011&cellid=66991912'
{"lat":46.909009,"lon":7.360584,"range":4294967295}
curl -s 'http://localhost:5265/?mcc=204&mnc=4&lac=207&cellid=48051720'
{"lat":50.970299,"lon":5.79788,"range":4545}

Query with non-existing cell tower which can only be answered by using the default location (8):
Query with non-existing cell tower which can only be answered by using the default location (7):

curl -s 'http://localhost:5265/?mcc=999&mnc=4&lac=207&cellid=48051720'
curl -s 'http://localhost:5265/?mcc=3100&mnc=41&lac=42971&cellid=9906077'
{"lat":46.909009,"lon":7.360584,"range":4294967295}

The output is always a JSON object that has lat, lon and range.

## Maintenance

Remove entries in Google GLM MMAP cache database which are present in OpenCellId database or Mozilla Location Service database:

node glm_cells-cleanup.js
echo "VACUUM;" | sqlite3 glm_cells.sqlite

Remove entries in OpenCellId cache database which are present in OpenCellId database, Mozilla Location Service database, Google GLM MMAP cache database or online service:

node uwl-cells-cleanup.js
echo "VACUUM;" | sqlite3 uwl_cells.sqlite

Remove default locations in the OpenCellId cache database (useful when assuming that the corresponding cells are now known by either Google GLM MMAP or OpenCellId):

sqlite3 uwl_cells.sqlite
DELETE FROM cells WHERE range=4294967295;
VACUUM;
echo "DELETE FROM cells WHERE range=4294967295;" sqlite3 uwl_cells.sqlite
echo "VACUUM;" | sqlite3 uwl_cells.sqlite

## Resources

Expand Down
98 changes: 49 additions & 49 deletions cell-geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ http.createServer(function(req, res) {
if (!url.query.mcc || !url.query.mnc || !url.query.lac || !url.query.cellid) {
res.writeHead(400);
return res.end('Need mcc, mnc, lac, cellid passed in as query parameters');
} else if (url.query.mcc > 999) {
} else if ((url.query.mcc > 999) || (url.query.mnc > 999) || (url.query.lac > 65535) || (url.query.cellid > 268435455)) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(util.format('{"lat":%d,"lon":%d,"range":%d}',
defaultLatitude, defaultLongitude, defaultRange));
Expand Down Expand Up @@ -85,49 +85,49 @@ http.createServer(function(req, res) {
return;
}

// -4- if Google GLM MMAP cache database did not have a match, query Google GLM MMAP
// -4- if Google GLM MMAP cache database did not have a match, query OpenCellId cache database
if (typeof row == 'undefined') {
request.glm(url.query.mcc,url.query.mnc,url.query.lac,url.query.cellid).then(coords => {
glmDb.run('INSERT INTO cells (mcc, mnc, lac, cellid, lat, lon, range) VALUES(?,?,?,?,?,?,?)', {
1: url.query.mcc,
2: url.query.mnc,
3: url.query.lac,
4: url.query.cellid,
5: coords.lat,
6: coords.lon,
7: coords.range
}, function(err, result) {
if (err) {
console.error('Error inserting queried location into Google GLM MMAP cache database');
res.writeHead(500);
res.end(JSON.stringify(err));
return;
}
console.log(util.format('Req#%d/%d: Queried Google GLM MMAP for %s: %s, %s, %s, %s -> %s, %s, %s',
numValidRequests, numUnknownCells, req.connection.remoteAddress,
url.query.mcc, url.query.mnc, url.query.lac, url.query.cellid,
coords.lat, coords.lon, coords.range));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(util.format('{"lat":%d,"lon":%d,"range":%d}', coords.lat, coords.lon, coords.range));
uwlDb.get('SELECT lat, lon, range FROM cells WHERE mcc = ? AND mnc = ? AND lac = ? AND cellid = ?', {
1: url.query.mcc,
2: url.query.mnc,
3: url.query.lac,
4: url.query.cellid
}, function(err, row) {
if (err) {
console.error('Error querying OpenCellId cache database');
res.writeHead(500);
res.end(JSON.stringify(err));
return;
});
}).catch(err => {
// -5- Google GLM MMAP did not have a match, query OpenCellId cache database
uwlDb.get('SELECT lat, lon, range FROM cells WHERE mcc = ? AND mnc = ? AND lac = ? AND cellid = ?', {
1: url.query.mcc,
2: url.query.mnc,
3: url.query.lac,
4: url.query.cellid
}, function(err, row) {
if (err) {
console.error('Error querying OpenCellId cache database');
res.writeHead(500);
res.end(JSON.stringify(err));
return;
}
}

// -6- if OpenCellId cache database did not have a match, query OpenCellId
if (typeof row == 'undefined') {
// -5- if OpenCellId cache database did not have a match, query Google GLM MMAP online service
if (typeof row == 'undefined') {
request.glm(url.query.mcc,url.query.mnc,url.query.lac,url.query.cellid).then(coords => {
glmDb.run('INSERT INTO cells (mcc, mnc, lac, cellid, lat, lon, range) VALUES(?,?,?,?,?,?,?)', {
1: url.query.mcc,
2: url.query.mnc,
3: url.query.lac,
4: url.query.cellid,
5: coords.lat,
6: coords.lon,
7: coords.range
}, function(err, result) {
if (err) {
console.error('Error inserting queried location into Google GLM MMAP cache database');
res.writeHead(500);
res.end(JSON.stringify(err));
return;
}
console.log(util.format('Req#%d/%d: Queried Google GLM MMAP for %s: %s, %s, %s, %s -> %s, %s, %s',
numValidRequests, numUnknownCells, req.connection.remoteAddress,
url.query.mcc, url.query.mnc, url.query.lac, url.query.cellid,
coords.lat, coords.lon, coords.range));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(util.format('{"lat":%d,"lon":%d,"range":%d}', coords.lat, coords.lon, coords.range));
return;
});
}).catch(err => {
// -6- if Google GLM MMAP did not have a match, query OpenCellId online service
request.oci(url.query.mcc,url.query.mnc,url.query.lac,url.query.cellid, OPENCELLID_API_KEY).then(coords => {
if (coords.statusCode == 200) { // ok
uwlDb.run('INSERT INTO cells (mcc, mnc, lac, cellid, lat, lon, range) VALUES(?,?,?,?,?,?,?)', {
Expand Down Expand Up @@ -195,23 +195,23 @@ http.createServer(function(req, res) {
res.end(JSON.stringify(err));
return;
});
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(row));
}
});
});
} else { // -4- OpencellId cache database did have a match
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(row));
}
});
} else {
} else { // -3- Google GLM MMAP cache database did have a match
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(row));
}
});
} else {
} else { // -2- Mozilla Location Service database did have a match
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(row));
}
});
} else {
} else { // -1- OpenCellId database did have a match
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(row));
}
Expand Down
Binary file modified overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified overview.vsdx
Binary file not shown.

0 comments on commit cffa51e

Please sign in to comment.