Struct cheap_ruler::CheapRuler

source ·
pub struct CheapRuler<T>where
    T: Float + Debug,{ /* private fields */ }
Expand description

A collection of very fast approximations to common geodesic measurements. Useful for performance-sensitive code that measures things on a city scale. Point coordinates are in the [x = longitude, y = latitude] form.

Implementations§

source§

impl<T> CheapRuler<T>where T: Float + Debug,

source

pub fn new(latitude: T, distance_unit: DistanceUnit) -> Self

source

pub fn from_tile(y: u32, z: u32, distance_unit: DistanceUnit) -> Self

Creates a ruler object from tile coordinates (y and z). Convenient in tile-reduce scripts

Arguments
  • y - y
  • z - z
  • distance_unit - Unit to express distances in
Examples
use cheap_ruler::{CheapRuler, DistanceUnit};
let cr = CheapRuler::<f64>::from_tile(1567, 12, DistanceUnit::Meters);
source

pub fn square_distance(&self, a: &Point<T>, b: &Point<T>) -> T

Calculates the square of the approximate distance between two geographical points

Arguments
  • a - First point
  • b - Second point
source

pub fn distance(&self, a: &Point<T>, b: &Point<T>) -> T

Calculates the approximate distance between two geographical points

Arguments
  • a - First point
  • b - Second point
Examples
use cheap_ruler::{CheapRuler, DistanceUnit};
let cr = CheapRuler::new(44.7192003, DistanceUnit::Meters);
let dist = cr.distance(
  &(14.8901816, 44.7209699).into(),
  &(14.8905188, 44.7209699).into()
);
assert!(dist < 38.0);
source

pub fn bearing(&self, a: &Point<T>, b: &Point<T>) -> T

Returns the bearing between two points in angles

Arguments
  • a - First point
  • b - Second point
Examples
use cheap_ruler::{CheapRuler, DistanceUnit};
let cr = CheapRuler::new(44.7192003, DistanceUnit::Meters);
let bearing = cr.bearing(
  &(14.8901816, 44.7209699).into(),
  &(14.8905188, 44.7209699).into()
);
assert_eq!(bearing, 90.0);
source

pub fn destination(&self, origin: &Point<T>, dist: T, bearing: T) -> Point<T>

Returns a new point given distance and bearing from the starting point

Arguments
  • origin - origin point
  • dist - distance
  • bearing - bearing
Examples
use cheap_ruler::{CheapRuler, DistanceUnit};
let cr = CheapRuler::new(44.7192003, DistanceUnit::Meters);
let p1 = (14.8901816, 44.7209699).into();
let p2 = (14.8905188, 44.7209699).into();
let dist = cr.distance(&p1, &p2);
let bearing = cr.bearing(&p1, &p2);
let destination = cr.destination(&p1, dist, bearing);

assert_eq!(destination.x(), p2.x());
assert_eq!(destination.y(), p2.y());
source

pub fn offset(&self, origin: &Point<T>, dx: T, dy: T) -> Point<T>

Returns a new point given easting and northing offsets (in ruler units) from the starting point

Arguments
  • origin - point
  • dx - easting
  • dy - northing
source

pub fn line_distance(&self, points: &LineString<T>) -> T

Given a line (an array of points), returns the total line distance.

Arguments
  • points - line of points
Example
use cheap_ruler::{CheapRuler, DistanceUnit};
use geo_types::LineString;
let cr = CheapRuler::new(50.458, DistanceUnit::Meters);
let line_string: LineString<f64> = vec![
    (-67.031, 50.458),
    (-67.031, 50.534),
    (-66.929, 50.534),
    (-66.929, 50.458)
].into();
let length = cr.line_distance(&line_string);
source

pub fn area(&self, polygon: &Polygon<T>) -> T

Given a polygon returns the area

  • polygon - Polygon
source

pub fn along(&self, line: &LineString<T>, dist: T) -> Option<Point<T>>

Returns the point at a specified distance along the line

Arguments
  • line - Line
  • dist - Distance along the line
source

pub fn point_to_segment_distance( &self, p: &Point<T>, start: &Point<T>, end: &Point<T> ) -> T

Returns the shortest distance between a point and a line segment given with two points.

Arguments
  • p - Point to calculate the distance from
  • start - Start point of line segment
  • end - End point of line segment
source

pub fn point_on_line( &self, line: &LineString<T>, point: &Point<T> ) -> Option<PointOnLine<T>>

Returns a tuple of the form (point, index, t) where point is closest point on the line from the given point, index is the start index of the segment with the closest point, and t is a parameter from 0 to 1 that indicates where the closest point is on that segment

Arguments
  • line - Line to compare with point
  • point - Point to calculate the closest point on the line
source

pub fn line_slice( &self, start: &Point<T>, stop: &Point<T>, line: &LineString<T> ) -> LineString<T>

Returns a part of the given line between the start and the stop points (or their closest points on the line)

Arguments
  • start - Start point
  • stop - Stop point
  • line - Line string
source

pub fn line_slice_along( &self, start: T, stop: T, line: &LineString<T> ) -> LineString<T>

Returns a part of the given line between the start and the stop points indicated by distance along the line

  • start - Start distance
  • stop - Stop distance
  • line - Line string
source

pub fn buffer_point(&self, p: &Point<T>, buffer: T) -> Rect<T>

Given a point, returns a bounding rectangle created from the given point buffered by a given distance

Arguments
  • p - Point
  • buffer - Buffer distance
source

pub fn buffer_bbox(&self, bbox: &Rect<T>, buffer: T) -> Rect<T>

Given a bounding box, returns the box buffered by a given distance

Arguments
  • bbox - Bounding box
  • buffer - Buffer distance
source

pub fn inside_bbox(&self, p: &Point<T>, bbox: &Rect<T>) -> bool

Returns true if the given point is inside in the given bounding box, otherwise false.

Arguments
  • p - Point
  • bbox - Bounding box

Trait Implementations§

source§

impl<T> Clone for CheapRuler<T>where T: Float + Debug + Clone,

source§

fn clone(&self) -> CheapRuler<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for CheapRuler<T>where T: Float + Debug + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> PartialEq for CheapRuler<T>where T: Float + Debug + PartialEq,

source§

fn eq(&self, other: &CheapRuler<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> StructuralPartialEq for CheapRuler<T>where T: Float + Debug,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for CheapRuler<T>where T: RefUnwindSafe,

§

impl<T> Send for CheapRuler<T>where T: Send,

§

impl<T> Sync for CheapRuler<T>where T: Sync,

§

impl<T> Unpin for CheapRuler<T>where T: Unpin,

§

impl<T> UnwindSafe for CheapRuler<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.