diff --git a/server/src/trees.rs b/server/src/trees.rs index 58d1bae..d2e34fe 100644 --- a/server/src/trees.rs +++ b/server/src/trees.rs @@ -9,7 +9,7 @@ use geo::Point; use serde::Serialize; use mime::{APPLICATION_JSON, IMAGE_PNG}; use http::StatusCode; -use chrono::NaiveDate; +use chrono::{Local, NaiveDate}; use cairo::{ImageSurface, Context}; use crate::{AppState, AreaExtractor, IdExtractor}; @@ -121,19 +121,13 @@ pub fn get_heatmap(state: State) -> (State, Response) { let app_state = AppState::borrow_from(&state); let result = { let mut db = app_state.db.lock().unwrap(); - db.query("SELECT coord, score FROM trees WHERE coord <@ $1::box AND SCORE IS NOT NULL", &[ + db.query("SELECT coord, score, planted FROM trees WHERE coord <@ $1::box AND SCORE IS NOT NULL", &[ &pe.grow(0.2).to_rect() ]).unwrap() }; const TILE_SIZE: i32 = 256; - // let (w, h) = if pe.w() / 2. > pe.h() { - // (TILE_MAX / 2, (TILE_MAX as f64 * pe.h() / pe.w()) as i32) - // } else { - // (((TILE_MAX as f64 / 2. * pe.w() / pe.h()) as i32), TILE_MAX) - // }; let (w, h) = (TILE_SIZE, TILE_SIZE); - // println!("{}x{} ({}x{})", w, h, pe.w(), pe.h()); let s = ImageSurface::create(cairo::Format::ARgb32, w, h).unwrap(); let ctx = Context::new(&s).unwrap(); ctx.set_antialias(cairo::Antialias::Fast); @@ -141,11 +135,12 @@ pub fn get_heatmap(state: State) -> (State, Response) { for row in result { let point: Point = row.get(0); let score: f64 = row.get(1); + let planted: Option = row.get(2); + let age = planted.map(|planted| ((Local::today().naive_local() - planted).num_days()) / 365); let (r, g, b) = temperature(1. - score); ctx.set_source_rgba(r, g, b, 0.1); - let radius = 1.5 + 0.03 / pe.w(); - // println!("radius: {}", radius); + let radius = 1.5 + (0.4 + age.unwrap_or(50) as f64 / 80.).min(1.4) * 0.02 / pe.w(); ctx.arc((point.x() - pe.x1) * (w as f64) / pe.w(), (pe.y2 - point.y()) * (h as f64) / pe.h(), radius, 0., 2. * core::f64::consts::PI); ctx.fill().unwrap();