diff --git a/Cargo.lock b/Cargo.lock index b1213d0..6db7ce7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,8 +99,8 @@ dependencies = [ "lazycell", "log", "peeking_take_while", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "regex", "rustc-hash", "shlex", @@ -245,11 +245,13 @@ dependencies = [ "glib", "glib-build-tools", "gtk4", + "ndarray", "npyz", "num-traits", "plotters", "plotters-backend", "proj", + "quadtree_rs", "shapefile", "thiserror", ] @@ -284,7 +286,7 @@ dependencies = [ "ansi_term", "atty", "bitflags", - "strsim", + "strsim 0.8.0", "textwrap", "unicode-width", "vec_map", @@ -407,6 +409,41 @@ dependencies = [ "typenum", ] +[[package]] +name = "darling" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcfbcb0c5961907597a7d1148e3af036268f2b773886b8bb3eeb1e1281d3d3d6" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6afc018370c3bff3eb51f89256a6bdb18b4fdcda72d577982a14954a7a0b402c" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2 0.4.30", + "quote 0.6.13", + "strsim 0.7.0", + "syn 0.15.44", +] + +[[package]] +name = "darling_macro" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6d8dac1c6f1d29a41c4712b4400f878cb4fcc4c7628f298dd75038e024998d1" +dependencies = [ + "darling_core", + "quote 0.6.13", + "syn 0.15.44", +] + [[package]] name = "dbase" version = "0.3.0" @@ -417,6 +454,31 @@ dependencies = [ "time 0.3.21", ] +[[package]] +name = "derive_builder" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ac53fa6a3cda160df823a9346442525dcaf1e171999a1cf23e67067e4fd64d4" +dependencies = [ + "darling", + "derive_builder_core", + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", +] + +[[package]] +name = "derive_builder_core" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0288a23da9333c246bb18c143426074a6ae96747995c5819d2947b64cd942b37" +dependencies = [ + "darling", + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", +] + [[package]] name = "digest" version = "0.10.7" @@ -536,6 +598,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "font-kit" version = "0.11.0" @@ -635,8 +703,8 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", ] @@ -734,8 +802,8 @@ dependencies = [ name = "geo-macros" version = "0.1.0" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", ] @@ -843,8 +911,8 @@ dependencies = [ "heck", "proc-macro-crate", "proc-macro-error", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 1.0.109", ] @@ -962,8 +1030,8 @@ dependencies = [ "anyhow", "proc-macro-crate", "proc-macro-error", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 1.0.109", ] @@ -1045,6 +1113,12 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "image" version = "0.24.6" @@ -1055,7 +1129,7 @@ dependencies = [ "byteorder", "color_quant", "jpeg-decoder", - "num-rational", + "num-rational 0.4.1", "num-traits", "png", ] @@ -1146,6 +1220,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "matrixmultiply" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090126dc04f95dc0d1c1c91f61bdd474b3930ca064c1edc8a849da2c6cbe1e77" +dependencies = [ + "autocfg", + "rawpointer", +] + [[package]] name = "memchr" version = "2.5.0" @@ -1186,6 +1270,19 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex 0.4.3", + "num-integer", + "num-traits", + "rawpointer", +] + [[package]] name = "nom" version = "7.1.3" @@ -1203,11 +1300,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80581878bc671c9c832a199569fdb6696a1948b32346a8095c939d12a1946178" dependencies = [ "byteorder", - "num-bigint", + "num-bigint 0.4.3", "py_literal", "zip", ] +[[package]] +name = "num" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" +dependencies = [ + "num-bigint 0.2.6", + "num-complex 0.2.4", + "num-integer", + "num-iter", + "num-rational 0.2.4", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.3" @@ -1219,6 +1341,16 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits", +] + [[package]] name = "num-complex" version = "0.4.3" @@ -1238,6 +1370,29 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint 0.2.6", + "num-integer", + "num-traits", +] + [[package]] name = "num-rational" version = "0.4.1" @@ -1367,8 +1522,8 @@ checksum = "6c435bf1076437b851ebc8edc3a18442796b30f1728ffea6262d59bbe28b077e" dependencies = [ "pest", "pest_meta", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", ] @@ -1472,8 +1627,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 1.0.109", "version_check", ] @@ -1484,11 +1639,20 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "version_check", ] +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid", +] + [[package]] name = "proc-macro2" version = "1.0.58" @@ -1530,20 +1694,39 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "102df7a3d46db9d3891f178dcc826dc270a6746277a9ae6436f8d29fd490a8e1" dependencies = [ - "num-bigint", - "num-complex", + "num-bigint 0.4.3", + "num-complex 0.4.3", "num-traits", "pest", "pest_derive", ] +[[package]] +name = "quadtree_rs" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ec1506903c0f5cff273bd34df6646c7040f2a7c5b0a92a9179d89551bed9ffd" +dependencies = [ + "derive_builder", + "num", +] + +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + [[package]] name = "quote" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.58", ] [[package]] @@ -1552,6 +1735,12 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "redox_syscall" version = "0.2.16" @@ -1661,8 +1850,8 @@ version = "1.0.160" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", ] @@ -1735,6 +1924,12 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" + [[package]] name = "strsim" version = "0.8.0" @@ -1747,14 +1942,25 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid", +] + [[package]] name = "syn" version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "unicode-ident", ] @@ -1764,8 +1970,8 @@ version = "2.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "unicode-ident", ] @@ -1832,8 +2038,8 @@ version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", ] @@ -1928,6 +2134,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + [[package]] name = "vec_map" version = "0.8.2" @@ -1987,8 +2199,8 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", "wasm-bindgen-shared", ] @@ -1999,7 +2211,7 @@ version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "358a79a0cb89d21db8120cbfb91392335913e4890665b1a7981d9e956903b434" dependencies = [ - "quote", + "quote 1.0.27", "wasm-bindgen-macro-support", ] @@ -2009,8 +2221,8 @@ version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4783ce29f09b9d93134d41297aded3a712b7b979e9c6f28c32cb88c973a94869" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.58", + "quote 1.0.27", "syn 2.0.16", "wasm-bindgen-backend", "wasm-bindgen-shared", diff --git a/Cargo.toml b/Cargo.toml index 7ad7d6e..643a99b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ shapefile = { version = "0.4", features = ["geo-types"] } thiserror = "1.0.40" num-traits = "0.2.15" npyz = { version = "0.8.0", features = ["npz"] } +ndarray = "0.15.6" +quadtree_rs = "0.1.2" [build-dependencies] glib-build-tools = "0.17.0" diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..2c8346b --- /dev/null +++ b/src/data.rs @@ -0,0 +1,144 @@ +use ndarray::{array, s, Array, Array1, Array2, Array3}; +use num_traits::{AsPrimitive, FromPrimitive, Num, ToPrimitive}; +use quadtree_rs::Quadtree; +use std::{self, borrow::Borrow, f64::consts::PI}; +use thiserror::Error; + +type Lon = f64; +type Lat = f64; + +#[derive(Error, Debug)] +pub enum DataError { + #[error("value")] + FormatError, + #[error("")] + IOError { + #[from] + source: std::io::Error, + }, +} + +pub enum CoorType { + Polar, + LatLon, +} + +pub struct RadarData2d +where + T: Num, + X: Num, + Y: Num, +{ + pub dim1: Array1, + pub dim2: Array1, + pub data: Array2, + pub coord_type: CoorType, +} + +pub struct RadarData3d +where + T: Num, + X: Num, + Y: Num, + Z: Num, +{ + pub dim1: Array1, + pub dim2: Array1, + pub dim3: Array1, + pub data: Array3, +} + +trait MultiDimensionData { + fn levels(&self, levels: impl IntoIterator) -> Self; +} + +impl + FromPrimitive> RadarData2d { + fn resample( + &self, + width_rate: f64, + height_rate: f64, + filter_len: f64, + ) -> Result, DataError> { + 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 => { + let width_filtered = Self::_resample(&self.data, width_rate, filter_len, false); + Ok(Self::_resample( + &width_filtered, + height_rate, + filter_len, + true, + )) + } + } + } + + fn split(&self) { + let a = self.data.slice(s![0..10, 1..10]); + } + + fn _resample(data: &Array2, rate: f64, filter_len: f64, reverse: bool) -> Array2 { + let ori_width = if reverse { data.nrows() } else { data.ncols() }; + let ori_height = if reverse { data.ncols() } else { data.nrows() }; + + let new_width = (ori_width as f64 * rate).ceil() as usize; + let mut result: Array2 = Array2::zeros((ori_height, new_width)); + (0..ori_height).into_iter().for_each(|height| { + for width in 0..new_width { + let center_x = (width as f64 + 0.5) / new_width as f64 * ori_width as f64; + let filter_start = center_x - filter_len / 2.0; + let start_idx = (filter_start - 0.5).ceil() as usize; + + let mut value_sum = 0.0; + let mut filter_sum = 0.0; + + for i in 0..filter_len as usize { + let input_x = start_idx + i; + let weight = windowed_sinc( + (input_x as f64 + 0.5 - center_x) * rate, + (input_x as f64 + 0.5 - filter_start) / filter_len, + ); + value_sum += weight * data[[height, input_x.clamp(0, ori_width - 1)]].as_(); + filter_sum += weight; + } + + result[[height, width]] = T::from_f64(value_sum / filter_sum).unwrap(); + } + }); + result + } +} + +#[inline] +fn windowed_sinc(x: f64, y: f64) -> f64 { + let x = x * PI; + let sinc = if x != 0.0 { f64::sin(x) / x } else { 1.0 }; + let window = if 0f64 <= y && y <= 1.0 { + 1.0 - (y - 0.5).abs() * 2.0 + } else { + 0f64 + }; + sinc * window +} + +pub struct LevelData { + levels: Vec>, +} + +impl LevelData { + fn new(levels: Vec>) -> Self { + let mut qt = Quadtree::>::new(levels.len()); + Self { levels } + } +} + +// impl + FromPrimitive> MultiDimensionData for RadarData2d { +// // fn levels(&self, levels: usize) -> Self { +// // let a: Vec<_> = Array1::linspace(0.0, 1.0, levels) +// // .into_iter() +// // .map(|scale| self.resample(scale, scale, 3.0)) +// // .collect(); +// // } +// } diff --git a/src/main.rs b/src/main.rs index 85b525f..85d8182 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,22 @@ use backend::CairoBackend; -use plotters::chart::MeshStyle; -use plotters::prelude::*; - -use geo_types::{MultiPolygon, Polygon}; use gtk::gdk::Display; -use gtk::{ - gio, glib, style_context_add_provider_for_display, Application, ApplicationWindow, CssProvider, - StyleContext, -}; +use gtk::{gio, glib, style_context_add_provider_for_display, Application, CssProvider}; use plotters::coord::geo::*; +use plotters::prelude::*; use shapefile; use gtk::{prelude::*, DrawingArea}; -use proj::Proj; -// use plotters::prelude::{ChartBuilder, IntoDrawingArea, Rectangle}; -use npyz::npz::{self, NpzArchive}; -use plotters::style::{HSLColor, BLACK, WHITE}; -// use plotters::prelude::DrawingArea; +use npyz::npz::NpzArchive; +use plotters::style::{BLACK, WHITE}; use window::Window; mod backend; mod painter; mod trees; mod window; +mod data; + +use ndarray::{self, ArrayD}; +use trees::get; const APP_ID: &str = "org.gtk_rs.HelloWorld2"; @@ -39,6 +34,24 @@ fn main() -> glib::ExitCode { app.run() } +fn load_npz() -> ArrayD { + use ndarray::ShapeBuilder; + + let mut npz_file = NpzArchive::open("/home/ruomu/Desktop/test.npz").unwrap(); + let reader = npz_file.by_name("value").unwrap().unwrap(); + + let shape = reader.shape().to_vec(); + let order = reader.order(); + let data = reader.into_vec::().unwrap(); + + let _shape: Vec<_> = shape.into_iter().map(|x| x as usize).collect(); + + let true_shape = _shape.set_f(order == npyz::Order::Fortran); + + ndarray::ArrayD::from_shape_vec(true_shape, data) + .unwrap_or_else(|e| panic!("shape error: {}", e)) +} + fn build_ui(app: &Application) { // Create a window and set the title @@ -53,21 +66,59 @@ fn build_ui(app: &Application) { root.fill(&WHITE); - use trees::get; + let levels: Vec = vec![0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65]; - 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(); + // ['#00ACA4', '#C0C0FE', '#7A72EE', '#1E26D0', '#A6FCA8', '#00EA00', '#10921A', '#FCF464', '#C8C802', '#8C8C00', '#FEACAC', '#FE645C', '#EE0230', '#D48EFE', '#AA24FA'] + let colors = vec![ + RGBColor(0, 172, 164), + RGBColor(192, 192, 254), + RGBColor(122, 114, 238), + RGBColor(30, 38, 208), + RGBColor(166, 252, 168), + RGBColor(0, 234, 0), + RGBColor(16, 146, 26), + RGBColor(252, 244, 100), + RGBColor(200, 200, 2), + RGBColor(140, 140, 0), + RGBColor(254, 172, 172), + RGBColor(254, 100, 92), + RGBColor(238, 2, 48), + RGBColor(212, 142, 254), + RGBColor(170, 36, 250), + ]; let mut polygons: Vec<(shapefile::Polygon, _)> = shapefile::read_as::<_, shapefile::Polygon, shapefile::dbase::Record>( - "/Users/ruomu/china/省界_region.shp", + "/home/ruomu/Desktop/china/省界_region.shp", ) .unwrap(); - let (polygon, polygon_record) = polygons.first_mut().unwrap(); + // let (polygon, polygon_record) = polygons.first_mut().unwrap(); + + let mut npz_file = NpzArchive::open("/home/ruomu/Desktop/test.npz").unwrap(); + // let value = npz_file.by_name("value").unwrap().unwrap(); + let lon = npz_file + .by_name("lon") + .unwrap() + .unwrap() + .into_vec::() + .unwrap(); + let lat = npz_file + .by_name("lat") + .unwrap() + .unwrap() + .into_vec::() + .unwrap(); + + // let val = npz_file + // .by_name("value") + // .unwrap() + // .unwrap() + // .into_vec::() + // .unwrap(); + + let val = load_npz(); + let proj = Mercator::new().change_min_latitude(-80f64); @@ -78,8 +129,8 @@ fn build_ui(app: &Application) { end: 121.0, }), Some(std::ops::Range { - start: 29.0, - end: 30.0, + start: 30.0, + end: 31.0, }), proj, ) @@ -95,38 +146,43 @@ fn build_ui(app: &Application) { // }, // ); - let ring = polygon.ring(0).unwrap(); + // let ring = polygon.ring(0).unwrap(); + + let lon_dpi = lon[1] - lon[0]; + let lat_dpi = lat[1] - lat[0]; + 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()) - }), + lon.into_iter() + .map(|inner_lon| { + return lat.to_owned().into_iter().map(move |inner_lat| { + return (inner_lon, inner_lat); + }); + }) + .flatten() + .zip(val.t().into_iter()) + .filter(|((_,_),val)| **val >-125) + .map(|((lon, lat), val)| { + Rectangle::new( + [(lon, lat), (lon + lon_dpi, lat + lat_dpi)], + get(&levels, &colors, val.clone()).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(); - } + // 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)); diff --git a/src/tree.rs b/src/tree.rs new file mode 100644 index 0000000..2344a3c --- /dev/null +++ b/src/tree.rs @@ -0,0 +1,22 @@ +use num_traits::Num; +use plotters::style::RGBAColor; +pub fn get(levels: &Vec, colors: &Vec, v: T) -> RGBAColor +where + T: Num + PartialOrd, +{ + let len = levels.len(); + + let mut left = 0; + let mut right = len - 1; + + while left < right - 1 { + let middle = (right + left) / 2; + if v > levels[middle] { + left = middle; + } else { + right = middle; + } + } + + colors[left] +} diff --git a/src/trees.rs b/src/trees.rs index 2344a3c..1d03bd8 100644 --- a/src/trees.rs +++ b/src/trees.rs @@ -1,6 +1,6 @@ use num_traits::Num; -use plotters::style::RGBAColor; -pub fn get(levels: &Vec, colors: &Vec, v: T) -> RGBAColor +use plotters::style::RGBColor; +pub fn get(levels: &Vec, colors: &Vec, v: T) -> RGBColor where T: Num + PartialOrd, { @@ -18,5 +18,5 @@ where } } - colors[left] + colors[left].clone() }