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