geo_plot/
plot.rs

1use geo_types::*;
2use gnuplot::{Axes2D, PlotOption};
3
4pub trait Plot {
5    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]);
6}
7
8impl Plot for Line<f64> {
9    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
10        let (sx, sy) = self.start.x_y();
11        let (ex, ey) = self.end.x_y();
12        let x = vec![sx, ex];
13        let y = vec![sy, ey];
14        fg.lines(&x, &y, opt);
15    }
16}
17
18impl Plot for LineString<f64> {
19    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
20        let (x, y): (Vec<f64>, Vec<f64>) = self.points_iter().map(|p| p.x_y()).unzip();
21        fg.lines(&x, &y, opt);
22    }
23}
24
25impl Plot for Coordinate<f64> {
26    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
27        let (x, y) = self.x_y();
28        fg.points(vec![x], vec![y], opt);
29    }
30}
31
32impl Plot for Point<f64> {
33    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
34        let (x, y) = self.x_y();
35        fg.points(vec![x], vec![y], opt);
36    }
37}
38
39impl Plot for Polygon<f64> {
40    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
41        let (x, y): (Vec<f64>, Vec<f64>) = self.exterior().points_iter().map(|p| p.x_y()).unzip();
42        fg.lines(&x, &y, opt);
43    }
44}
45
46impl Plot for MultiLineString<f64> {
47    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
48        self.clone().into_iter().for_each(|line| line.plot(fg, opt))
49    }
50}
51
52impl Plot for MultiPolygon<f64> {
53    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
54        self.clone().into_iter().for_each(|poly| poly.plot(fg, opt))
55    }
56}
57
58impl Plot for MultiPoint<f64> {
59    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
60        self.clone()
61            .into_iter()
62            .for_each(|point| point.plot(fg, opt))
63    }
64}
65
66impl Plot for Geometry<f64> {
67    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
68        match self {
69            Geometry::Point(x) => x.plot(fg, opt),
70            Geometry::Line(x) => x.plot(fg, opt),
71            Geometry::LineString(x) => x.plot(fg, opt),
72            Geometry::Polygon(x) => x.plot(fg, opt),
73            Geometry::MultiPoint(x) => x.plot(fg, opt),
74            Geometry::MultiLineString(x) => x.plot(fg, opt),
75            Geometry::MultiPolygon(x) => x.plot(fg, opt),
76            Geometry::GeometryCollection(x) => x.plot(fg, opt),
77        }
78    }
79}
80
81impl Plot for GeometryCollection<f64> {
82    fn plot(&self, fg: &mut Axes2D, opt: &[PlotOption<&str>]) {
83        self.clone().into_iter().for_each(|g| g.plot(fg, opt))
84    }
85}