Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace geo with geo-types #20

Merged
merged 3 commits into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ documentation = "https://docs.rs/gpx"
repository = "https://github.com/georust/gpx"

[dependencies]
geo = "0.9"
geo-types = "0.2"
xml-rs = "0.8"
chrono = "0.4"
error-chain = "0.11.0"
assert_approx_eq = "1.0.0"

[dev-dependencies]
geo = "0.10"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Crates.io](https://img.shields.io/crates/v/gpx.svg)](https://crates.io/crates/gpx) [![Build Status](https://travis-ci.org/georust/gpx.svg?branch=master)](https://travis-ci.org/georust/gpx) [![docs.rs](https://docs.rs/gpx/badge.svg)](https://docs.rs/gpx)

gpx is a library for reading and writing GPX (GPS Exchange Format) files. It uses the
primitives provided by [rust-geo](https://github.com/georust/rust-geo) to allow for storage
primitives provided by [geo-types](https://github.com/georust/geo) to allow for storage
of GPS data.

## Example
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! gpx is a library for reading and writing GPX (GPS Exchange Format) files.
//! It uses the primitives provided by [rust-geo](https://github.com/georust/rust-geo)
//! It uses the primitives provided by [geo-types](https://github.com/georust/geo)
//! to allow for storage of GPS data.
//!
//! # Examples
Expand Down Expand Up @@ -39,9 +39,12 @@ extern crate error_chain;
extern crate assert_approx_eq;

extern crate chrono;
extern crate geo;
extern crate geo_types;
extern crate xml;

#[cfg(test)]
extern crate geo;

// Export our type structs in the root, along with the read function.
pub use reader::read;
pub use types::*;
Expand Down
26 changes: 15 additions & 11 deletions src/parser/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use errors::*;

use geo::Bbox;
use geo_types::{Coordinate, Rect};
use std::io::Read;
use xml::reader::XmlEvent;

use parser::verify_starting_tag;
use parser::Context;

/// consume consumes a bounds element until it ends.
pub fn consume<R: Read>(context: &mut Context<R>) -> Result<Bbox<f64>> {
pub fn consume<R: Read>(context: &mut Context<R>) -> Result<Rect<f64>> {
let attributes = verify_starting_tag(context, "bounds")?;
// get required bounds
let minlat = attributes
Expand Down Expand Up @@ -51,11 +51,15 @@ pub fn consume<R: Read>(context: &mut Context<R>) -> Result<Bbox<f64>> {
.parse()
.chain_err(|| "error while casting max longitude to f64")?;

let bounds: Bbox<f64> = Bbox {
xmin: minlon,
xmax: maxlon,
ymin: minlat,
ymax: maxlat,
let bounds: Rect<f64> = Rect {
min: Coordinate {
x: minlon,
y: minlat,
},
max: Coordinate {
x: maxlon,
y: maxlat,
},
};

for event in context.reader() {
Expand Down Expand Up @@ -98,10 +102,10 @@ mod tests {
assert!(bounds.is_ok());

let bounds = bounds.unwrap();
assert_eq!(bounds.xmin, -74.031837463);
assert_eq!(bounds.ymin, 45.487064362);
assert_eq!(bounds.xmax, -73.586273193);
assert_eq!(bounds.ymax, 45.701225281);
assert_eq!(bounds.min.x, -74.031837463);
assert_eq!(bounds.min.y, 45.487064362);
assert_eq!(bounds.max.x, -73.586273193);
assert_eq!(bounds.max.y, 45.701225281);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/parser/gpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use chrono::{DateTime, Utc};
use errors::*;
use geo::Bbox;
use geo_types::Rect;
use std::io::Read;
use xml::reader::XmlEvent;

Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn consume<R: Read>(context: &mut Context<R>) -> Result<Gpx> {
let mut urlname: Option<String> = None;
let mut email: Option<String> = None;
let mut time: Option<DateTime<Utc>> = None;
let mut bounds: Option<Bbox<f64>> = None;
let mut bounds: Option<Rect<f64>> = None;
let mut gpx_name: Option<String> = None;
let mut description: Option<String> = None;
let mut keywords: Option<String> = None;
Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn consume<R: Read>(context: &mut Context<R>) -> Result<Gpx> {

#[cfg(test)]
mod tests {
use geo::Point;
use geo_types::Point;
use std::io::BufReader;

use super::consume;
Expand Down
34 changes: 18 additions & 16 deletions src/parser/waypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use errors::*;
use std::io::Read;
use xml::reader::XmlEvent;

use geo::Point;
use geo_types::Point;
use parser::extensions;
use parser::fix;
use parser::link;
Expand Down Expand Up @@ -99,26 +99,28 @@ pub fn consume<R: Read>(context: &mut Context<R>, tagname: &'static str) -> Resu
)
}
"sat" => {
waypoint.sat = Some(
string::consume(context, "sat")?
.parse()
.chain_err(|| "error while casting number of satellites (sat) to u64")?,
)
waypoint.sat =
Some(string::consume(context, "sat")?.parse().chain_err(|| {
"error while casting number of satellites (sat) to u64"
})?)
}
"hdop" => {
waypoint.hdop = Some(string::consume(context, "hdop")?.parse().chain_err(
|| "error while casting horizontal dilution of precision (hdop) to f64",
)?)
waypoint.hdop =
Some(string::consume(context, "hdop")?.parse().chain_err(|| {
"error while casting horizontal dilution of precision (hdop) to f64"
})?)
}
"vdop" => {
waypoint.vdop = Some(string::consume(context, "vdop")?.parse().chain_err(
|| "error while casting vertical dilution of precision (vdop) to f64",
)?)
waypoint.vdop =
Some(string::consume(context, "vdop")?.parse().chain_err(|| {
"error while casting vertical dilution of precision (vdop) to f64"
})?)
}
"pdop" => {
waypoint.pdop = Some(string::consume(context, "pdop")?.parse().chain_err(
|| "error while casting position dilution of precision (pdop) to f64",
)?)
waypoint.pdop =
Some(string::consume(context, "pdop")?.parse().chain_err(|| {
"error while casting position dilution of precision (pdop) to f64"
})?)
}
"ageofgpsdata" => {
waypoint.age = Some(
Expand Down Expand Up @@ -163,7 +165,7 @@ pub fn consume<R: Read>(context: &mut Context<R>, tagname: &'static str) -> Resu

#[cfg(test)]
mod tests {
use geo::Point;
use geo_types::Point;
use std::io::BufReader;

use super::consume;
Expand Down
39 changes: 19 additions & 20 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! generic types for GPX

use geo::{Bbox, LineString, MultiLineString, Point};
use geo::{Geometry, ToGeo};
use geo_types::{Geometry, LineString, MultiLineString, Point, Rect};

use chrono::prelude::Utc;
use chrono::DateTime;
Expand Down Expand Up @@ -61,7 +60,7 @@ pub struct Metadata {
pub keywords: Option<String>,
/*copyright: GpxCopyrightType,*/
/// Bounds for the tracks in the GPX.
pub bounds: Option<Bbox<f64>>,
pub bounds: Option<Rect<f64>>,
/*extensions: GpxExtensionsType,*/
}

Expand Down Expand Up @@ -118,9 +117,9 @@ impl Track {
}
}

impl ToGeo<f64> for Track {
fn to_geo(&self) -> Geometry<f64> {
Geometry::MultiLineString(self.multilinestring())
impl From<Track> for Geometry<f64> {
fn from(track: Track) -> Geometry<f64> {
Geometry::MultiLineString(track.multilinestring())
}
}

Expand Down Expand Up @@ -149,10 +148,10 @@ impl TrackSegment {
///
/// ```
/// extern crate gpx;
/// extern crate geo;
/// extern crate geo_types;
///
/// use gpx::{TrackSegment, Waypoint};
/// use geo::Point;
/// use geo_types::Point;
///
/// fn main() {
/// let mut trkseg: TrackSegment = TrackSegment::new();
Expand All @@ -165,13 +164,13 @@ impl TrackSegment {
}
}

impl ToGeo<f64> for TrackSegment {
fn to_geo(&self) -> Geometry<f64> {
Geometry::LineString(self.linestring())
impl From<TrackSegment> for Geometry<f64> {
fn from(track_segment: TrackSegment) -> Geometry<f64> {
Geometry::LineString(track_segment.linestring())
}
}

// A Version of geo::Point that has the Default trait implemented, which
// A Version of geo_types::Point that has the Default trait implemented, which
// allows us to initialise the GpxPoint with default values compactly
// in the Waypoint::new function below
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -264,11 +263,11 @@ impl Waypoint {
/// Gives the geographical point of the waypoint.
///
/// ```
/// extern crate geo;
/// extern crate geo_types;
/// extern crate gpx;
///
/// use gpx::Waypoint;
/// use geo::Point;
/// use geo_types::Point;
///
/// fn main() {
/// // Kind of useless, but it shows the point.
Expand All @@ -279,17 +278,17 @@ impl Waypoint {
/// }
/// ```
pub fn point(&self) -> Point<f64> {
self.point.0 //.0 to extract the geo::Point from the tuple struct GpxPoint
self.point.0 //.0 to extract the geo_types::Point from the tuple struct GpxPoint
}

/// Creates a new Waypoint from a given geographical point.
///
/// ```
/// extern crate geo;
/// extern crate geo_types;
/// extern crate gpx;
///
/// use gpx::Waypoint;
/// use geo::Point;
/// use geo_types::Point;
///
/// fn main() {
/// let point = Point::new(-121.97, 37.24);
Expand All @@ -306,9 +305,9 @@ impl Waypoint {
}
}

impl ToGeo<f64> for Waypoint {
fn to_geo(&self) -> Geometry<f64> {
Geometry::Point(self.point())
impl From<Waypoint> for Geometry<f64> {
fn from(waypoint: Waypoint) -> Geometry<f64> {
Geometry::Point(waypoint.point())
}
}

Expand Down
7 changes: 4 additions & 3 deletions tests/gpx_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ extern crate assert_approx_eq;

extern crate chrono;
extern crate geo;
extern crate geo_types;
extern crate gpx;

#[cfg(test)]
mod tests {
use chrono::prelude::*;
use geo::algorithm::haversine_distance::HaversineDistance;
use geo::euclidean_length::EuclideanLength;
use geo::{Geometry, Point, ToGeo};
use geo_types::{Geometry, Point};
use std::fs::File;
use std::io::BufReader;

Expand Down Expand Up @@ -124,10 +125,10 @@ mod tests {
assert!(time < Utc.ymd(2017, 7, 30).and_hms_micro(0, 0, 0, 000_000));

// Should coerce to Point.
let geo: Geometry<f64> = point.to_geo();
let geo: Geometry<f64> = point.clone().into();
match geo {
Geometry::Point(_) => {} // ok
_ => panic!("point.to_geo() gave bad geometry"),
_ => panic!("point.into() gave bad geometry"),
}

// It's missing almost all fields, actually.
Expand Down