diff --git a/src/data.rs b/src/data.rs index 2a0be4d..7f941e3 100644 --- a/src/data.rs +++ b/src/data.rs @@ -17,7 +17,7 @@ pub enum DataError { } #[derive(Clone, Copy)] -pub enum CoorType { +pub enum CoordType { Polar, LatLon, } @@ -33,7 +33,7 @@ where pub dim1: Array1, pub dim2: Array1, pub data: ArrayBase, - pub coord_type: CoorType, + pub coord_type: CoordType, } struct RadarData3d @@ -74,8 +74,8 @@ where let width_rate = width_rate.min(1.0); let height_rate = height_rate.min(1.0); match self.coord_type { - CoorType::Polar => Err(DataError::FormatError), - CoorType::LatLon => { + CoordType::Polar => Err(DataError::FormatError), + CoordType::LatLon => { let width_filtered: ArrayBase, Ix2> = Self::_resample(&self.data, width_rate, filter_len); @@ -319,7 +319,7 @@ where dim1: dim1, dim2: dim2, data: value, - coord_type: CoorType::LatLon, + coord_type: CoordType::LatLon, }) } } @@ -330,7 +330,7 @@ impl RadarData2d> { } } -pub struct Radar2d(RadarData2d>); +pub struct Radar2d(pub RadarData2d>); impl Radar2d { pub fn load( diff --git a/src/main.rs b/src/main.rs index 281fc71..e98f4ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,21 @@ use geo_types::{MultiPolygon, Polygon}; +use gtk::builders::DrawingAreaBuilder; use gtk::gdk::Display; use gtk::{ gio, glib, style_context_add_provider_for_display, Application, ApplicationWindow, CssProvider, - StyleContext, }; use gtk::{prelude::*, DrawingArea}; -use npyz::npz::{self, NpzArchive}; +use painter::wgs84::*; +use painter::Painter; // use plotters::prelude::DrawingArea; use window::Window; // mod backend; mod data; -// mod painter; +mod painter; // mod trees; mod window; -use data::{Npz,Radar2d, levels}; +use data::{CoordType, Npz, Radar2d}; const APP_ID: &str = "org.gtk_rs.HelloWorld2"; @@ -28,16 +29,6 @@ fn main() -> glib::ExitCode { // Connect to "activate" signal of `app` app.connect_activate(build_ui); - let path = "/home/ruomu/Desktop/test.npz"; - - let data = Radar2d::::load(path, Npz).unwrap(); - - let result = levels(data, 3); - - for i in result.iter(){ - println!("{:?}", i.0.depth()); - } - // Run the application app.run() } @@ -49,88 +40,17 @@ fn build_ui(app: &Application) { let drawing_area = DrawingArea::new(); - // drawing_area.set_draw_func(|a, b, c, d| { - // let root = CairoBackend::new(b, (500, 500)) - // .expect("fuck") - // .into_drawing_area(); + let path = "/home/ruomu/Desktop/test.npz"; - // root.fill(&WHITE); + let data = Radar2d::::load(path, Npz).unwrap(); - // use trees::get; + drawing_area.set_draw_func(move |a, b, c, d| { + let projection = Mercator::new(); + let coord = LatLonCoord::new(None, None, ((0, c), (0, d)), projection); + let painter: Painter<'_, f64, LatLonCoord> = Painter::new(b, coord, c as u32, c as u32); + painter.draw_radar_2d(&data.0); - // let mut npz_file = NpzArchive::open("/Users/ruomu/test.npz").unwrap(); - - // // let value = npz_file.by_name("value").unwrap().unwrap(); - // let lon = npz_file.by_name("lon").unwrap().unwrap(); - // // let lat = npz_file.by_name("lat").unwrap().unwrap(); - - // let mut polygons: Vec<(shapefile::Polygon, _)> = - // shapefile::read_as::<_, shapefile::Polygon, shapefile::dbase::Record>( - // "/Users/ruomu/china/省界_region.shp", - // ) - // .unwrap(); - - // let (polygon, polygon_record) = polygons.first_mut().unwrap(); - - // let proj = Mercator::new().change_min_latitude(-80f64); - - // let mut chart = ChartBuilder::on(&root) - // .build_geo_coord( - // Some(std::ops::Range { - // start: 119.0, - // end: 121.0, - // }), - // Some(std::ops::Range { - // start: 29.0, - // end: 30.0, - // }), - // proj, - // ) - // .unwrap(); - - // // chart.draw_mesh_lines( - // // (10, 5), - // // (true, true), - // // &ShapeStyle { - // // color: RGBAColor(0, 0, 0, 1f64), - // // filled: false, - // // stroke_width: 1, - // // }, - // // ); - - // let ring = polygon.ring(0).unwrap(); - - // chart.draw_series( - // lon.into_vec::().unwrap().into_iter().map(|lon| { - // Rectangle::new([(lon, 29.1), (lon + 0.001, 29.1 + 0.001)], BLACK.filled()) - // }), - // ); - - // // chart.draw_series(lon.into_vec::().unwrap().into_iter().map(|lon| { - // // lat.into_vec::().unwrap().into_iter().map(|lat| { - // // Rectangle::new( - // // [ - // // (lon as f64, lat as f64), - // // (lon as f64 + 0.001, lat as f64 + 0.001), - // // ], - // // BLACK.filled(), - // // ) - // // }) - // // })); - - // for (polygon, _) in polygons.into_iter() { - // chart - // .draw_series( - // AreaSeries::new( - // polygon.ring(0).unwrap().points().iter().map(|x| (x.x, x.y)), - // 0.0, - // &RED.mix(0.2), - // ) - // .border_style(&RED), - // ) - // .unwrap(); - // } - // }); + }); // window.set_child(Some(&drawing_area)); window.set_default_width(1000); diff --git a/src/painter/coords/mod.rs b/src/painter/coords/mod.rs index 9b55082..6c6f50a 100644 --- a/src/painter/coords/mod.rs +++ b/src/painter/coords/mod.rs @@ -1,8 +1,8 @@ use num_traits::Num; -mod wgs84; +pub mod wgs84; pub type ScreenCoord = (i32, i32); -pub(super) trait Coord { +pub trait Coord { fn map(&self, axis_1: T, axis_2: T) -> ScreenCoord; } diff --git a/src/painter/coords/wgs84.rs b/src/painter/coords/wgs84.rs index e50b2fb..d49b30a 100644 --- a/src/painter/coords/wgs84.rs +++ b/src/painter/coords/wgs84.rs @@ -197,8 +197,8 @@ impl ProjectionS for Mercator { }); let lat_range = Range { - 0: lat_range.start.max(self.min_latitude), - 1: lat_range.end.min(self.max_latitude), + 0: lat_range.0.max(self.min_latitude), + 1: lat_range.0.min(self.max_latitude), }; (lon_range, lat_range) } diff --git a/src/painter/mod.rs b/src/painter/mod.rs index 3d27cd1..afd1733 100644 --- a/src/painter/mod.rs +++ b/src/painter/mod.rs @@ -1,2 +1,5 @@ mod coords; mod painter; + +pub use painter::Painter; +pub use coords::wgs84; \ No newline at end of file diff --git a/src/painter/painter.rs b/src/painter/painter.rs index 1732908..eb6947e 100644 --- a/src/painter/painter.rs +++ b/src/painter/painter.rs @@ -1,25 +1,36 @@ +use std::marker::PhantomData; + +use crate::data::RadarData2d; + use super::coords::{Coord, ScreenCoord}; use gtk::{cairo::*, gdk::RGBA}; -pub struct Painter { +use num_traits::Num; +pub struct Painter<'a, T: Num, C: Coord> { width: u32, height: u32, - context: Context, - coord: T, + context: &'a Context, + coord: C, + marker: PhantomData, } -impl Painter { - pub fn new(context: Context, coord: T, width: u32, height: u32) -> Self { +impl<'a, T: Num + Clone, C: Coord> Painter<'a, T, C> { + pub fn new(context: &'a Context, coord: C, width: u32, height: u32) -> Self { Self { context, coord, width, height, + marker: PhantomData, } } fn set_color(&self, color: &RGBA) { - self.context - .set_source_rgba(color.red(), color.green(), color.blue(), color.alpha()); + self.context.set_source_rgba( + color.red() as f64, + color.green() as f64, + color.blue() as f64, + color.alpha() as f64, + ); } fn draw_pixel(&self, point: ScreenCoord) {} @@ -27,11 +38,13 @@ impl Painter { fn draw_rect(&self, point: ScreenCoord, width: f64, height: f64) {} fn draw_line(&self, start: ScreenCoord, end: ScreenCoord) {} +} + +impl<'a, C: Coord> Painter<'a, f64, C> { + pub fn draw_radar_2d(&self, data: &RadarData2d) + where + D: ndarray::Data + Clone + ndarray::RawDataClone, + { - pub fn draw_2d_data(&self, data: &RadarArray2) { - data.dim1.iter().zip(data.dim2.iter()).for_each(|(x, y)| { - let (x, y) = self.coord.map(*x, *y); - self.draw_pixel((x, y)); - }); } }