Skip to content

Commit

Permalink
Split core geo types into new geo-types crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed May 3, 2018
1 parent 960e85a commit 858fa50
Show file tree
Hide file tree
Showing 43 changed files with 441 additions and 303 deletions.
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

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

0 comments on commit 858fa50

Please sign in to comment.