It ain't fugly if it works, right?

This commit is contained in:
Admar Schoonen 2022-02-18 19:51:26 +01:00
parent f1a303edc0
commit 5cd2b56176
1 changed files with 87 additions and 10 deletions

View File

@ -21,6 +21,8 @@ use rocket::tokio::time::Instant;
use rocket::tokio::{self, select};
use rocket::{get, routes, State};
use std::f64::consts::PI;
pub(crate) use self::forecast::Metric;
use self::forecast::{forecast, Forecast};
pub(crate) use self::maps::{Maps, MapsHandle};
@ -58,6 +60,17 @@ async fn forecast_geo(
Json(forecast)
}
fn deg2rad(x: f64)->f64 {
let y: f64 = x * PI / 180.0;
return y;
}
/// Mercator projection from https://stackoverflow.com/questions/18838915/convert-lat-lon-to-pixel-coordinate
fn mercator_y(lat: f64)->f64 {
return f64::ln(f64::tan(lat / 2.0 + PI / 4.0));
}
/// Handler for showing the current map with the geocoded position for a specific metric.
///
/// Note: This handler is mosly used for debugging purposes!
@ -83,16 +96,80 @@ async fn show_map(
_ => return None, // Unsupported metric
};
// Paint the provided position on the map using a 11×11 pixel square.
// FIXME: Use an actual correct calculation, instead of this very rough estimation!
let rel_x = (lon - 3.3) / 4.0; // 3.3..7.3, 4.0
let rel_y = 1.0 - ((lat - 50.7) / 3.0); // 50.7..53.7, 3.0
let x = (rel_x * image.height() as f64) as u32;
let y = (rel_y * image.width() as f64) as u32;
for px in x - 5..=x + 5 {
for py in y - 5..=y + 5 {
image.put_pixel(px, py, Rgba::from([0xff, 0x0, 0x0, 0x70]));
}
// GPS coordinates from Google Maps
let vlissingen_lat: f64 = deg2rad(51.44);
let vlissingen_lon: f64 = deg2rad(3.57);
let vlissingen_x: u32 = 84;
let vlissingen_y: u32 = 745;
let winschoten_lat: f64 = deg2rad(53.14);
let winschoten_lon: f64 = deg2rad(7.04);
let winschoten_x: u32 = 729;
let winschoten_y: u32 = 185;
let lauwersoog_lat: f64 = deg2rad(53.40);
let lauwersoog_lon: f64 = deg2rad(6.22);
let lauwersoog_x: u32 = 566;
let lauwersoog_y: u32 = 111;
let enschede_lat: f64 = deg2rad(52.22);
let enschede_lon: f64 = deg2rad(6.90);
let enschede_x: u32 = 694;
let enschede_y: u32 = 494;
let ref_x1 = vlissingen_x;
let ref_lon1 = vlissingen_lon;
let ref_y1 = vlissingen_y;
let ref_lat1 = vlissingen_lat;
let ref_x2 = enschede_x;
let ref_lon2 = enschede_lon;
let ref_y2 = lauwersoog_y;
let ref_lat2 = lauwersoog_lat;
let y_min = mercator_y(ref_lat1);
let y_max = mercator_y(ref_lat2);
let x_factor = ((ref_x2 - ref_x1) as f64) / (ref_lon2 - ref_lon1);
let y_factor = ((ref_y1 - ref_y2) as f64) / (y_max - y_min);
println!("x_factor: {}, y_factor: {}", x_factor, y_factor);
println!("y_min: {}, y_max: {}, lat: {}", y_min * y_factor, y_max * y_factor, mercator_y(deg2rad(lat)) * y_factor);
let mut x_f64 = (deg2rad(lon) - ref_lon1) * x_factor + ref_x1 as f64;
let mut y_f64 = (y_max - mercator_y(deg2rad(lat))) * y_factor + ref_y2 as f64;
println!("x: {}, y: {}", x_f64, y_f64);
if x_f64 < 0.0 {
x_f64 = 0.0;
}
if x_f64 > (image.width() - 1) as f64 {
x_f64 = (image.width() - 1) as f64;
}
if y_f64 < 0.0 {
y_f64 = 0.0;
}
if y_f64 > (image.height() - 1) as f64 {
y_f64 = (image.height() - 1) as f64;
}
println!("bounded to x: {}, y: {}", x_f64, y_f64);
for px in ref_x1 - 5..=ref_x1 + 5 {
for py in ref_y1 - 5..=ref_y1 + 5 {
image.put_pixel(px, py, Rgba::from([0x00, 0xff, 0xff, 0x70]));
}
}
for px in ref_x2 - 5..=ref_x2 + 5 {
for py in ref_y2 - 5..=ref_y2 + 5 {
image.put_pixel(px, py, Rgba::from([0xff, 0x00, 0xff, 0x70]));
}
}
for py in 0..(image.height() - 1) {
image.put_pixel(x_f64 as u32, py, Rgba::from([0x00, 0x00, 0x00, 0x70]));
}
for px in 0..(image.width() - 1) {
image.put_pixel(px, y_f64 as u32, Rgba::from([0x00, 0x00, 0x00, 0x70]));
}
// Encode the image as PNG image data.