radar-g/src/main.rs
2023-05-24 08:49:08 +08:00

150 lines
4.3 KiB
Rust

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 plotters::coord::geo::*;
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 window::Window;
mod backend;
mod painter;
mod trees;
mod window;
const APP_ID: &str = "org.gtk_rs.HelloWorld2";
fn main() -> glib::ExitCode {
gio::resources_register_include!("window.gresource").expect("Failed to register resources");
// Create a new application
let app = Application::builder().application_id(APP_ID).build();
app.connect_startup(|_| load_css());
// Connect to "activate" signal of `app`
app.connect_activate(build_ui);
// Run the application
app.run()
}
fn build_ui(app: &Application) {
// Create a window and set the title
let window = Window::new(app);
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();
root.fill(&WHITE);
use trees::get;
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::<f64>().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::<f32>().unwrap().into_iter().map(|lon| {
// lat.into_vec::<f32>().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);
window.set_default_height(800);
window.present();
}
fn load_css() {
// Load the CSS file and add it to the provider
let provider = CssProvider::new();
provider.load_from_data(include_str!("css/style.css"));
// Add the provider to the default screen
style_context_add_provider_for_display(
&Display::default().expect("Could not connect to a display."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}