Skip to content

Commit

Permalink
Add index check command
Browse files Browse the repository at this point in the history
  • Loading branch information
madninja committed Apr 3, 2023
1 parent edba792 commit 3e8cb23
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_json = "1"
flate2 = "1"
h3ron = {version = "0.16"}
geo-types = "*"
hextree = "0"
byteorder = "1.4"
rayon = "1.6"
log = "0"
Expand Down
37 changes: 37 additions & 0 deletions generator/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ impl Cmd {
pub enum IndexCmd {
Generate(Generate),
Export(Export),
Check(Check),
}

impl IndexCmd {
pub fn run(&self) -> Result<()> {
match self {
Self::Generate(cmd) => cmd.run(),
Self::Export(cmd) => cmd.run(),
Self::Check(cmd) => cmd.run(),
}
}
}
Expand Down Expand Up @@ -101,6 +103,18 @@ fn read_cells<P: AsRef<path::Path>>(file: P) -> Result<Vec<H3Cell>> {
Ok(vec)
}

fn read_hexset<P: AsRef<path::Path>>(file: P) -> Result<hextree::HexTreeSet> {
let file = fs::File::open(file.as_ref())?;
let mut reader = GzDecoder::new(file);
let mut vec = Vec::new();

while let Ok(entry) = reader.read_u64::<byteorder::LittleEndian>() {
vec.push(hextree::Cell::from_raw(entry)?);
}

Ok(vec.iter().collect())
}

fn write_cells<P: AsRef<path::Path>>(cells: Vec<H3Cell>, output: P) -> Result<()> {
let file = fs::File::create(output.as_ref())?;
let mut writer = GzEncoder::new(file, Compression::default());
Expand Down Expand Up @@ -148,3 +162,26 @@ impl Export {
Ok(())
}
}

/// Check membership of a given h3 index in the given binary file
#[derive(Debug, clap::Args)]
pub struct Check {
input: path::PathBuf,
cell: h3ron::H3Cell,
}

impl Check {
pub fn run(&self) -> Result<()> {
let hex_set = read_hexset(&self.input)?;
if hex_set.contains(hextree::Cell::from_raw(*self.cell)?) {
println!("Cell {} in {}", self.cell.to_string(), self.input.display())
} else {
anyhow::bail!(
"Cell {} not in {}",
self.cell.to_string(),
self.input.display()
)
}
Ok(())
}
}

0 comments on commit 3e8cb23

Please sign in to comment.