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

Split core geo types into new geo-types crate. #201

Merged
merged 1 commit into from
May 3, 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
/Cargo.lock
**/target
**/Cargo.lock
36 changes: 2 additions & 34 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,2 @@
[package]
name = "geo"
description = "Geospatial primitives and algorithms"
version = "0.8.2"
authors = ["Corey Farwell <coreyf@rwell.org>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/georust/rust-geo"
documentation = "https://docs.rs/geo/"
readme = "README.md"
keywords = ["gis", "geo", "geography", "geospatial"]

[badges]
travis-ci = { repository = "georust/rust-geo" }

[dependencies]
num-traits = "0.2"
serde = "1.0"
serde_derive = "1.0"
spade = "1.3.0"
failure = "0.1.1"
postgis = { version = "0.5", optional = true }
proj-sys = { version = "0.6.1", optional = true }
libc = { version = "0.2.39", optional = true }

[features]
default = []
postgis-integration = ["postgis"]
proj = ["proj-sys", "libc"]

[dev-dependencies]
approx = "0.1.1"

[package.metadata.docs.rs]
features = ["postgis"]
[workspace]
members = ["geo", "geo-types"]
15 changes: 15 additions & 0 deletions geo-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "geo-types"
version = "0.1.0"
authors = ["Corey Farwell <coreyf@rwell.org>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/georust/rust-geo"
documentation = "https://docs.rs/geo-types/"
readme = "../README.md"
keywords = ["gis", "geo", "geography", "geospatial"]
description = "Geospatial primitive data types"

[dependencies]
num-traits = "0.2"
serde = { version = "1", optional = true, features = ["derive"] }
spade = { version = "1.3", optional = true }
94 changes: 94 additions & 0 deletions geo-types/src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// FIXME: everything in this file is copy/paste from the 'geo' crate. ideally we
// wouldn't have this duplication
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the (non-public) traits below are unfortunately necessary to keep the spade trait impls on the geo types in geo-types/src/lib.rs. for now it's a little messy, but i think we can reevaluate this in the future. i.e. do we really need spade?


use num_traits::{Float, ToPrimitive};
use ::{Point, Line, CoordinateType};

pub trait EuclideanDistance<T, Rhs = Self> {
fn euclidean_distance(&self, rhs: &Rhs) -> T;
}

fn line_segment_distance<T>(point: &Point<T>, start: &Point<T>, end: &Point<T>) -> T
where
T: Float + ToPrimitive,
{
if start == end {
return point.euclidean_distance(start);
}
let dx = end.x() - start.x();
let dy = end.y() - start.y();
let r = ((point.x() - start.x()) * dx + (point.y() - start.y()) * dy) / (dx.powi(2) + dy.powi(2));
if r <= T::zero() {
return point.euclidean_distance(start);
}
if r >= T::one() {
return point.euclidean_distance(end);
}
let s = ((start.y() - point.y()) * dx - (start.x() - point.x()) * dy) / (dx * dx + dy * dy);
s.abs() * (dx * dx + dy * dy).sqrt()
}

impl<T> EuclideanDistance<T, Point<T>> for Point<T>
where
T: Float,
{
fn euclidean_distance(&self, p: &Point<T>) -> T {
let (dx, dy) = (self.x() - p.x(), self.y() - p.y());
dx.hypot(dy)
}
}

impl<T> EuclideanDistance<T, Point<T>> for Line<T>
where
T: Float,
{
fn euclidean_distance(&self, point: &Point<T>) -> T {
line_segment_distance(point, &self.start, &self.end)
}
}


pub trait BoundingBox<T: CoordinateType> {
type Output;

fn bbox(&self) -> Self::Output;
}

impl<T> BoundingBox<T> for Line<T>
where
T: CoordinateType,
{
type Output = Bbox<T>;

fn bbox(&self) -> Self::Output {
let a = self.start;
let b = self.end;
let (xmin, xmax) = if a.x() <= b.x() {
(a.x(), b.x())
} else {
(b.x(), a.x())
};
let (ymin, ymax) = if a.y() <= b.y() {
(a.y(), b.y())
} else {
(b.y(), a.y())
};
Bbox {
xmin: xmin,
xmax: xmax,
ymin: ymin,
ymax: ymax,
}
}
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Bbox<T>
where
T: CoordinateType,
{
pub xmin: T,
pub xmax: T,
pub ymin: T,
pub ymax: T,
}
Loading