wps_harvest: add gs-loc LTE cell crawler
- Commit
79d0c1542188e36cbe4a3c76555c77e0b1bcde24- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 17:23 UTC (1 month ago)
- Parents
65d8c1f
Diff
scripts/networklocation/README.md
diff --git a/scripts/networklocation/README.md b/scripts/networklocation/README.mdindex 3e9ccf4500..f77a35b8bd 100644--- a/scripts/networklocation/README.md+++ b/scripts/networklocation/README.md@@ -6,9 +6,9 @@ published to `data.maproj.org` and downloaded to device-protected storage on req | Database | Built by | Holds | | --- | --- | --- |-| `geocoder-v3.geodb` | [`geodb_build/`](geodb_build/) | Addresses, named streets, POIs and populated places, from an OSM planet dump |-| `wifi-v2.wpsdb` | [`wps_harvest/`](wps_harvest/) | WiFi BSSID → coordinates + accuracy |-| `cells-v2.wpsdb` | [`wps_harvest/`](wps_harvest/) | Cell tower → coordinates + accuracy |+| `geocoder.db` | [`geodb_build/`](geodb_build/) | Addresses, named streets, POIs and populated places, from an OSM planet dump |+| `wifi-v3.wpsdb` | [`wps_harvest/`](wps_harvest/) | WiFi BSSID → coordinates + accuracy |+| `cells-v3.wpsdb` | [`wps_harvest/`](wps_harvest/) | Cell tower → coordinates + accuracy | Both are self-contained Rust crates with their own READMEs; start there.
scripts/networklocation/wps_harvest/Cargo.toml
diff --git a/scripts/networklocation/wps_harvest/Cargo.toml b/scripts/networklocation/wps_harvest/Cargo.tomlindex af1028d833..9478a1753c 100644--- a/scripts/networklocation/wps_harvest/Cargo.toml+++ b/scripts/networklocation/wps_harvest/Cargo.toml@@ -42,6 +42,10 @@ path = "src/bin/wps_build.rs" name = "wps_extract" path = "src/bin/wps_extract.rs" +[[bin]]+name = "wps_cellcrawl"+path = "src/bin/wps_cellcrawl.rs"+ [dependencies] # HTTPS for the gs-loc client. rustls rather than the platform stack so a build box needs no # system OpenSSL, and webpki-roots so it needs no system trust store either.
scripts/networklocation/wps_harvest/README.md
diff --git a/scripts/networklocation/wps_harvest/README.md b/scripts/networklocation/wps_harvest/README.mdindex 695365d402..a55ea71d9e 100644--- a/scripts/networklocation/wps_harvest/README.md+++ b/scripts/networklocation/wps_harvest/README.md@@ -1,6 +1,6 @@ # wps_harvest — offline beacon store builder -Builds `wifi-v2.wpsdb` and `cells-v2.wpsdb`, the offline WiFi and cell beacon databases that+Builds `wifi-v3.wpsdb` and `cells-v3.wpsdb`, the offline WiFi and cell beacon databases that `:networklocation` resolves scan results against. Replaces `wtfps-experiment/store.py`, which lived outside the repo and no longer exists.@@ -14,8 +14,8 @@ v1 wifi.wpsdb ── wps_extract ──► seed-bssids.txt ─┐ (BSSIDs only) │ ├─ wps_crawl ──► *.shard ─┐ (or a local scan, or random probing) ─────────────┘ (gs-loc snowball) │- ├─ wps_build ──► wifi-v2.wpsdb-OpenCelliD ────── wps_seed ──────────────────────────────► *.shard ─────────┘ cells-v2.wpsdb+ ├─ wps_build ──► wifi-v3.wpsdb+OpenCelliD ────── wps_seed ──────────────────────────────► *.shard ─────────┘ cells-v3.wpsdb ``` Shards are the interchange format: fixed-width, unsorted, append-only. The stages are separate@@ -187,13 +187,29 @@ client with a minimum interval, and failures back off geometrically to a five-mi `analysis/server-inventory-raw.md` already flags the endpoint as a terms-of-service concern; running the crawl faster is both rude and self-defeating. +### Crawl cells from gs-loc++OpenCelliD misses cells, and a phone whose serving cell is missing gets no fix at all: LTE+neighbour reports carry only a PCI, so the serving cell is usually the only one with a full+identity. `wps_cellcrawl` fills the gap from gs-loc. An LTE cell request returns the asked-for+cell plus about 120 surrounding LTE cells, so seeding it with a phone's cells covers the area+around it:++```sh+./target/release/wps_cellcrawl --out cells-gsloc.shard \+ --seed 310,260,14570,21206278 --max-queries 30 --radius-km 15+```++Seeds are `MCC,MNC,TAC,CID`. Cells nearest the first resolved seed are queried first. LTE only:+gs-loc's cell request is the LTE tower field.+ ### 3. Build ```sh-./target/release/wps_build --kind wifi --out wifi-v2.wpsdb \+./target/release/wps_build --kind wifi --out wifi-v3.wpsdb \ --scratch /var/tmp/wps wifi-bdb.shard wifi-gsloc.shard -./target/release/wps_build --kind cell --out cells-v2.wpsdb \+./target/release/wps_build --kind cell --out cells-v3.wpsdb \ --scratch /var/tmp/wps cells-ocid.shard cells-bdb.shard ``` @@ -209,8 +225,8 @@ exists but there is no good reason to use it. ### 4. Publish ```sh-sha256sum wifi-v2.wpsdb cells-v2.wpsdb-../../maps/publish_r2.sh wifi-v2.wpsdb cells-v2.wpsdb+sha256sum wifi-v3.wpsdb cells-v3.wpsdb+../../maps/publish_r2.sh wifi-v3.wpsdb cells-v3.wpsdb ``` Put the hashes into `OfflineDatabases.sha256For` in the app. They are load-bearing: a store is
scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs
diff --git a/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs b/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rsnew file mode 100644index 0000000000..37276d5a54--- /dev/null+++ b/scripts/networklocation/wps_harvest/src/bin/wps_cellcrawl.rs@@ -0,0 +1,205 @@+//! `wps_cellcrawl --out SHARD --seed MCC,MNC,TAC,CID [--seed ...] [options]`+//!+//! Snowball crawl of LTE cells through gs-loc. A cell request is answered with the asked-for+//! tower plus its surrounding LTE cells (around 120 when the request does not cap them), each+//! with its own position, so a few seeds from a phone's cell list cover a whole neighbourhood.+//! Cells nearest the first resolved seed are queried first, so a bounded run fills in the area+//! around the phone rather than wandering off along a motorway.++use std::cmp::Reverse;+use std::collections::{BinaryHeap, HashSet};+use std::fs::OpenOptions;+use std::io::BufWriter;+use std::path::PathBuf;+use std::process::ExitCode;+use std::time::Duration;++use wps_harvest::gsloc::{CellFix, Client};+use wps_harvest::keys::{self, CellId, RadioType};+use wps_harvest::record::{Record, RecordWriter, Source, ACC_UNKNOWN};++const USAGE: &str = "\+Usage: wps_cellcrawl --out SHARD --seed MCC,MNC,TAC,CID [--seed ...] [options]++ --out record shard to append to+ --seed an LTE cell to start from, e.g. from the phone's cell list (repeatable)+ --max-queries N stop after N requests (default 50)+ --radius-km R only expand into cells within R km of the first resolved seed (default 15)+ --interval MS minimum milliseconds between requests (default 1000)";++/// Consecutive failed requests after which the crawl gives up rather than hammering a+/// service that is refusing it.+const MAX_CONSECUTIVE_ERRORS: u32 = 5;++struct Args {+ out: PathBuf,+ seeds: Vec<(u32, u32, u32, u32)>,+ max_queries: u32,+ radius_m: f64,+ interval: Duration,+}++fn parse_seed(s: &str) -> Result<(u32, u32, u32, u32), String> {+ let p: Vec<u32> = s+ .split(',')+ .map(|v| v.trim().parse::<u32>().map_err(|e| format!("bad seed '{s}': {e}")))+ .collect::<Result<_, _>>()?;+ match p[..] {+ [mcc, mnc, tac, cid] => Ok((mcc, mnc, tac, cid)),+ _ => Err(format!("seed '{s}' must be MCC,MNC,TAC,CID")),+ }+}++fn parse_args(argv: &[String]) -> Result<Args, String> {+ let mut out = None;+ let mut seeds = Vec::new();+ let mut max_queries = 50u32;+ let mut radius_km = 15.0f64;+ let mut interval_ms = 1000u64;+ let mut i = 0;+ while i < argv.len() {+ let a = argv[i].as_str();+ let value = || argv.get(i + 1).cloned().ok_or_else(|| format!("{a} needs a value"));+ match a {+ "--out" => out = Some(PathBuf::from(value()?)),+ "--seed" => seeds.push(parse_seed(&value()?)?),+ "--max-queries" => max_queries = value()?.parse().map_err(|e| format!("{e}"))?,+ "--radius-km" => radius_km = value()?.parse().map_err(|e| format!("{e}"))?,+ "--interval" => interval_ms = value()?.parse().map_err(|e| format!("{e}"))?,+ "-h" | "--help" => return Err("help".to_string()),+ other => return Err(format!("unknown argument: {other}")),+ }+ i += 2;+ }+ if seeds.is_empty() {+ return Err("at least one --seed is required".to_string());+ }+ Ok(Args {+ out: out.ok_or("--out is required")?,+ seeds,+ max_queries,+ radius_m: radius_km * 1000.0,+ interval: Duration::from_millis(interval_ms),+ })+}++/// Great-circle distance in metres between two e8 coordinates.+fn distance_m(a: (i64, i64), b: (i64, i64)) -> f64 {+ let (lat1, lon1) = ((a.0 as f64 / 1e8).to_radians(), (a.1 as f64 / 1e8).to_radians());+ let (lat2, lon2) = ((b.0 as f64 / 1e8).to_radians(), (b.1 as f64 / 1e8).to_radians());+ let h = ((lat2 - lat1) / 2.0).sin().powi(2)+ + lat1.cos() * lat2.cos() * ((lon2 - lon1) / 2.0).sin().powi(2);+ 2.0 * 6_371_000.0 * h.sqrt().asin()+}++fn to_record(f: &CellFix) -> Option<Record> {+ let id = CellId {+ mcc: u16::try_from(f.mcc).ok()?,+ mnc: u16::try_from(f.mnc).ok()?,+ radio: RadioType::Lte,+ cell_id: u64::try_from(f.cell_id).ok()?,+ area: u32::try_from(f.area).ok()?,+ };+ if !keys::cell_fits(&id) {+ return None;+ }+ let accuracy_m = if f.accuracy_m < 0 {+ ACC_UNKNOWN+ } else {+ f.accuracy_m.min(i32::from(ACC_UNKNOWN - 1)) as u16+ };+ let r = Record { key: keys::cell_key(&id), lat_e8: f.lat_e8, lon_e8: f.lon_e8, accuracy_m, source: Source::Gsloc };+ r.in_range().then_some(r)+}++fn run(args: &Args) -> std::io::Result<()> {+ let file = OpenOptions::new().create(true).append(true).open(&args.out)?;+ let mut writer = RecordWriter::new(BufWriter::new(file));+ let mut client = Client::new(args.interval);++ // Seeds go first regardless of distance, in the order given; everything learned later is+ // ordered by distance from the first seed that resolved.+ let mut seeds: Vec<(u32, u32, u32, u32)> = args.seeds.clone();+ seeds.reverse();+ let mut frontier: BinaryHeap<(Reverse<u64>, (u32, u32, u32, u32))> = BinaryHeap::new();+ let mut queried: HashSet<(u32, u32, u32, u32)> = HashSet::new();+ let mut written: HashSet<u128> = HashSet::new();+ let mut center: Option<(i64, i64)> = None;+ let (mut queries, mut errors) = (0u32, 0u32);++ while queries < args.max_queries {+ let Some(next) = seeds.pop().or_else(|| frontier.pop().map(|(_, c)| c)) else { break };+ if !queried.insert(next) {+ continue;+ }+ let (mcc, mnc, tac, cid) = next;+ queries += 1;+ let fixes = match client.query_cell(mcc, mnc, cid, tac) {+ Ok(f) => {+ errors = 0;+ f+ }+ Err(e) => {+ errors += 1;+ eprintln!("query {queries}: {mcc}/{mnc} tac {tac} cid {cid}: {e}");+ if errors >= MAX_CONSECUTIVE_ERRORS {+ eprintln!("giving up after {errors} consecutive errors");+ break;+ }+ continue;+ }+ };++ let asked = fixes.iter().find(|f| f.cell_id == i64::from(cid) && f.area == tac as i32);+ if center.is_none() {+ center = asked.map(|f| (f.lat_e8, f.lon_e8));+ }+ let mut fresh = 0;+ for f in &fixes {+ let Some(r) = to_record(f) else { continue };+ if written.insert(r.key) {+ writer.push(&r)?;+ fresh += 1;+ }+ if let (Some(c), Ok(m), Ok(n), Ok(t), Ok(i)) =+ (center, u32::try_from(f.mcc), u32::try_from(f.mnc), u32::try_from(f.area), u32::try_from(f.cell_id))+ {+ let d = distance_m(c, (f.lat_e8, f.lon_e8));+ if d <= args.radius_m && !queried.contains(&(m, n, t, i)) {+ frontier.push((Reverse(d as u64), (m, n, t, i)));+ }+ }+ }+ println!(+ "query {queries}: {mcc}/{mnc} tac {tac} cid {cid} -> {} cells ({fresh} new){}; {} total, {} queued",+ fixes.len(),+ if asked.is_some() { "" } else { ", asked-for cell unknown" },+ written.len(),+ frontier.len(),+ );+ }+ let n = writer.finish()?;+ println!("Done: {queries} request(s), {n} cell record(s) appended to {}", args.out.display());+ Ok(())+}++fn main() -> ExitCode {+ let argv: Vec<String> = std::env::args().skip(1).collect();+ let args = match parse_args(&argv) {+ Ok(a) => a,+ Err(e) => {+ if e != "help" {+ eprintln!("error: {e}");+ }+ eprintln!("{USAGE}");+ return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE };+ }+ };+ match run(&args) {+ Ok(()) => ExitCode::SUCCESS,+ Err(e) => {+ eprintln!("error: {e}");+ ExitCode::FAILURE+ }+ }+}