fucking works
Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
parent
2353753f43
commit
0507a4d7d7
2 changed files with 28 additions and 12 deletions
22
src/dem.rs
22
src/dem.rs
|
@ -14,15 +14,16 @@ use gdal::Dataset;
|
||||||
use gdal;
|
use gdal;
|
||||||
use tracing_subscriber::registry::Data;
|
use tracing_subscriber::registry::Data;
|
||||||
|
|
||||||
use crate::{SharedState};
|
use crate::{DSC};
|
||||||
|
|
||||||
struct Pos {
|
struct Pos {
|
||||||
lat: f64,
|
lat: f64,
|
||||||
lon: f64,
|
lon: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyDataset(OnceCell<Dataset>);
|
pub struct MyDataset{pub ds: Dataset}
|
||||||
pub unsafe impl Send for MyDataset{}
|
unsafe impl Send for MyDataset{}
|
||||||
|
unsafe impl Sync for MyDataset{}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DatasetCache {
|
pub struct DatasetCache {
|
||||||
|
@ -42,7 +43,7 @@ impl DatasetCache {
|
||||||
|
|
||||||
if !self.hm.contains_key(&filename){
|
if !self.hm.contains_key(&filename){
|
||||||
let ds = Dataset::open(filename.clone()).unwrap();
|
let ds = Dataset::open(filename.clone()).unwrap();
|
||||||
self.hm.insert(filename.clone(), MyDataset(OnceCell::new(ds)));
|
self.hm.insert(filename.clone(), MyDataset{ds: ds});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,7 +88,7 @@ fn geo_to_pixel(dataset: &Dataset, lat: f64, lon: f64) -> Result<(isize, isize),
|
||||||
Ok((x_pixel, y_pixel))
|
Ok((x_pixel, y_pixel))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn elevation_from_coordinates(dsc: SharedState, lat: f64, lon: f64) -> f64 {
|
pub async fn elevation_from_coordinates(dsc: DSC, lat: f64, lon: f64) -> f64 {
|
||||||
let file = get_filename_from_latlon(lat, lon);
|
let file = get_filename_from_latlon(lat, lon);
|
||||||
let full_filename = format!("data/{file}");
|
let full_filename = format!("data/{file}");
|
||||||
|
|
||||||
|
@ -102,8 +103,15 @@ pub async fn elevation_from_coordinates(dsc: SharedState, lat: f64, lon: f64) ->
|
||||||
// ds = dd;
|
// ds = dd;
|
||||||
// //ds = cache.await.get_dataset_for_filename(full_filename);
|
// //ds = cache.await.get_dataset_for_filename(full_filename);
|
||||||
//}
|
//}
|
||||||
let x = dsc.get_dataset_for_filename(full_filename);
|
//let x = dsc.get_dataset_for_filename(full_filename);
|
||||||
let ds = x.0.read().unwrap();
|
|
||||||
|
if !dsc.contains_key(&full_filename) {
|
||||||
|
println!(">>> WILL GET {full_filename} because not in cache!");
|
||||||
|
let ds = Arc::new(MyDataset{ds:Dataset::open(full_filename.clone()).unwrap()});
|
||||||
|
dsc.insert(full_filename.clone(), ds).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ds = &dsc.get(&full_filename).await.unwrap().ds;
|
||||||
|
|
||||||
|
|
||||||
//let ds = dsc.get_dataset_for_filename(full_filename);
|
//let ds = dsc.get_dataset_for_filename(full_filename);
|
||||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -9,31 +9,39 @@ use axum_macros::debug_handler;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Bytes, extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router
|
body::Bytes, extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router
|
||||||
};
|
};
|
||||||
use dem::DatasetCache;
|
use dem::{DatasetCache, MyDataset};
|
||||||
|
|
||||||
use gdal::Dataset;
|
use gdal::Dataset;
|
||||||
|
use moka::future::Cache;
|
||||||
|
|
||||||
pub type SharedState = Arc<DatasetCache>;
|
|
||||||
|
|
||||||
//#[derive(Default)]
|
//#[derive(Default)]
|
||||||
//struct AppState {
|
//struct AppState {
|
||||||
// db: RwLock<HashMap<String, Dataset>>,
|
// db: RwLock<HashMap<String, Dataset>>,
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
type DSC = Cache<String, Arc<MyDataset>>;
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// initialize tracing
|
// initialize tracing
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
|
||||||
let data_set_cache = SharedState::default(); //Arc::new(RwLock::new(dem::DatasetCache::new()));
|
// Evict based on the number of entries in the cache.
|
||||||
|
let cache = Cache::builder()
|
||||||
|
// Up to 10,000 entries.
|
||||||
|
.max_capacity(10_000)
|
||||||
|
// Create the cache.
|
||||||
|
.build();
|
||||||
|
//cache.insert("hello".to_string(), Arc::new(dem::MyDataset{ds: Dataset::open("oueou").unwrap()})).await;
|
||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
// `GET /` goes to `root`
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
.route("/elevation/:lat/:lon", get(get_elevation))
|
.route("/elevation/:lat/:lon", get(get_elevation))
|
||||||
.with_state(Arc::clone(&data_set_cache));
|
.with_state(cache);
|
||||||
|
|
||||||
// run our app with hyper, listening globally on port 3000
|
// run our app with hyper, listening globally on port 3000
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
|
@ -48,7 +56,7 @@ async fn root() -> &'static str {
|
||||||
|
|
||||||
//async fn get_elevation(State(data_set_cache): State<Arc<RwLock<DatasetCache>>>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
//async fn get_elevation(State(data_set_cache): State<Arc<RwLock<DatasetCache>>>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
async fn get_elevation(State(shared): State<SharedState>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
async fn get_elevation(State(shared): State<DSC>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
||||||
|
|
||||||
// let x = AppState::default();
|
// let x = AppState::default();
|
||||||
// x.db.get("hello");
|
// x.db.get("hello");
|
||||||
|
|
Loading…
Reference in a new issue