smore shit

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-04-16 00:22:06 +02:00
parent e4657c87e6
commit 1c448a240d
2 changed files with 40 additions and 26 deletions

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use gdal::errors::GdalError;
use gdal::Dataset;
use tracing::{debug, debug_span, info};
use tracing::{debug, debug_span, error, info};
use moka::future::Cache;
@ -30,24 +30,26 @@ impl DatasetRepository {
DatasetRepository { cache: c, basedir }
}
async fn get(&self, filename: String) -> Arc<MyDataset> {
async fn get(&self, filename: String) -> Option<Arc<MyDataset>> {
let full_filename = format!("{}/{filename}", self.basedir);
if !self.cache.contains_key(&full_filename) {
info!("Will open {full_filename} because not in cache!");
let ds = Arc::new(MyDataset {
ds: Dataset::open(full_filename.clone()).unwrap(),
});
self.cache.insert(full_filename.clone(), ds).await;
let ds = Dataset::open(full_filename.clone());
match ds {
Err(x) => {
error!("File not present");
return None;
}
Ok(ds) => {
let mds = Arc::new(MyDataset { ds: ds });
self.cache.insert(full_filename.clone(), mds).await;
}
}
}
match self.cache.get(&full_filename).await {
Some(dataset_arc) => dataset_arc,
None => panic!("foo")
self.cache.get(&full_filename).await
}
}
}
impl Clone for DatasetRepository {
@ -59,15 +61,18 @@ impl Clone for DatasetRepository {
}
}
pub async fn elevation_from_coordinates(dr: DatasetRepository, lat: f64, lon: f64) -> f64 {
pub async fn elevation_from_coordinates(dr: DatasetRepository, lat: f64, lon: f64) -> Option<f64> {
let span = debug_span!("req", lat=%lat, lon=%lon);
let _guard = span.enter();
let filename = get_filename_from_latlon(lat, lon);
debug!(filename, "filename");
let ds = &dr.get(filename).await.ds;
let ds = &match dr.get(filename).await {
Some(x) => x,
None => return None,
}.ds;
let (px, py) = geo_to_pixel(ds, lat, lon).unwrap();
@ -75,7 +80,7 @@ pub async fn elevation_from_coordinates(dr: DatasetRepository, lat: f64, lon: f6
let raster_value = raster_band
.read_as::<f64>((px, py), (1, 1), (1, 1), None)
.unwrap();
raster_value.data[0]
Some(raster_value.data[0])
}
fn get_filename_from_latlon(lat: f64, lon: f64) -> String {

View file

@ -1,18 +1,24 @@
mod dem;
use axum::{
extract::{Path, State}, http::Request, routing::get, Router
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
Router,
};
use axum_macros::debug_handler;
use std::env;
use tower_http::{services::ServeDir, trace::{self, DefaultMakeSpan}};
use tower_http::{
services::ServeDir,
trace::{self, DefaultMakeSpan},
};
use dem::DatasetRepository;
use tracing::{info, Level, Span};
use tower_http::trace::TraceLayer;
use tracing::{info, Level, Span};
const DEFAULT_DATA_DIR: &str = "/data";
const DEFAULT_PORT: &str = "3000";
@ -22,7 +28,6 @@ async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
let config = load_config().unwrap();
let cache = DatasetRepository::new(config.basedir);
@ -46,13 +51,17 @@ async fn main() {
}
#[debug_handler]
async fn get_elevation(State(dsr): State<DatasetRepository>, Path((lat, lon)): Path<(f64, f64)>) -> String {
async fn get_elevation(
State(dsr): State<DatasetRepository>,
Path((lat, lon)): Path<(f64, f64)>,
) -> impl IntoResponse {
let ele = dem::elevation_from_coordinates(dsr, lat, lon);
let myele = ele.await;
format!("{myele}")
match ele.await {
Some(ele) => (StatusCode::OK, format!("{ele}")),
None => (StatusCode::NOT_IMPLEMENTED, "".to_string()),
}
}
fn load_config() -> Result<Config, env::VarError> {
Ok(Config {
basedir: env::var("DEM_LOCATION").unwrap_or_else(|_| DEFAULT_DATA_DIR.to_string()),