Skip to main content

CheapRuler

Struct 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 duplicate of the value. Read more
1.0.0 (const: unstable) · 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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

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> Freeze for CheapRuler<T>
where T: Freeze,

§

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> UnsafeUnpin for CheapRuler<T>
where T: UnsafeUnpin,

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.