Skip to content

Commit

Permalink
Add API for downloading metrics with SQL and geoms
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Oct 2, 2024
1 parent 1cff0b5 commit 726d96f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion popgetter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::collections::HashSet;
#[cfg(feature = "cache")]
use std::path::Path;

use anyhow::Result;
#[cfg(feature = "cache")]
use anyhow::{anyhow, Context};
use anyhow::{bail, Result};
use data_request_spec::DataRequestSpec;
use geo::get_geometries;
use itertools::Itertools;
use log::debug;
#[cfg(feature = "cache")]
use log::error;
use metadata::Metadata;
use parquet::get_metrics_sql;
use polars::frame::DataFrame;
use search::{Params, SearchParams, SearchResults};

Expand Down Expand Up @@ -122,6 +126,32 @@ impl Popgetter {
.download(&self.config, &params.download)
.await
}

pub async fn download_metrics_sql(&self, params: &Params) -> Result<String> {
let metric_requests = self.search(&params.search).to_metric_requests(&self.config);
get_metrics_sql(&metric_requests, None)
}

pub async fn download_geoms(&self, params: &Params) -> Result<DataFrame> {
let metric_requests = self.search(&params.search).to_metric_requests(&self.config);
let all_geom_files: HashSet<String> = metric_requests
.iter()
.map(|m| m.geom_file.clone())
.collect();
let bbox = params
.download
.region_spec
.first()
.and_then(|region_spec| region_spec.bbox().clone());
if all_geom_files.len().ne(&1) {
bail!(
"Exactly 1 geom file is currently supported, {} included in metric requests: {:?}",
all_geom_files.len(),
all_geom_files.into_iter().collect_vec().join(", ")
);
}
get_geometries(all_geom_files.iter().next().unwrap(), bbox).await
}
}

#[cfg(test)]
Expand Down

0 comments on commit 726d96f

Please sign in to comment.