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,
impl<T> CheapRuler<T>where T: Float + Debug,
pub fn new(latitude: T, distance_unit: DistanceUnit) -> Self
sourcepub fn from_tile(y: u32, z: u32, distance_unit: DistanceUnit) -> Self
pub fn from_tile(y: u32, z: u32, distance_unit: DistanceUnit) -> Self
sourcepub fn square_distance(&self, a: &Point<T>, b: &Point<T>) -> T
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 pointb
- Second point
sourcepub fn distance(&self, a: &Point<T>, b: &Point<T>) -> T
pub fn distance(&self, a: &Point<T>, b: &Point<T>) -> T
Calculates the approximate distance between two geographical points
Arguments
a
- First pointb
- 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);
sourcepub fn bearing(&self, a: &Point<T>, b: &Point<T>) -> T
pub fn bearing(&self, a: &Point<T>, b: &Point<T>) -> T
Returns the bearing between two points in angles
Arguments
a
- First pointb
- 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);
sourcepub fn destination(&self, origin: &Point<T>, dist: T, bearing: T) -> Point<T>
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 pointdist
- distancebearing
- 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());
sourcepub fn offset(&self, origin: &Point<T>, dx: T, dy: T) -> Point<T>
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
- pointdx
- eastingdy
- northing
sourcepub fn line_distance(&self, points: &LineString<T>) -> T
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);
sourcepub fn area(&self, polygon: &Polygon<T>) -> T
pub fn area(&self, polygon: &Polygon<T>) -> T
Given a polygon returns the area
polygon
- Polygon
sourcepub fn along(&self, line: &LineString<T>, dist: T) -> Option<Point<T>>
pub fn along(&self, line: &LineString<T>, dist: T) -> Option<Point<T>>
Returns the point at a specified distance along the line
Arguments
line
- Linedist
- Distance along the line
sourcepub fn point_to_segment_distance(
&self,
p: &Point<T>,
start: &Point<T>,
end: &Point<T>
) -> T
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 fromstart
- Start point of line segmentend
- End point of line segment
sourcepub fn point_on_line(
&self,
line: &LineString<T>,
point: &Point<T>
) -> Option<PointOnLine<T>>
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 pointpoint
- Point to calculate the closest point on the line
sourcepub fn line_slice(
&self,
start: &Point<T>,
stop: &Point<T>,
line: &LineString<T>
) -> LineString<T>
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 pointstop
- Stop pointline
- Line string
sourcepub fn line_slice_along(
&self,
start: T,
stop: T,
line: &LineString<T>
) -> LineString<T>
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 distancestop
- Stop distanceline
- Line string
sourcepub fn buffer_point(&self, p: &Point<T>, buffer: T) -> Rect<T>
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
- Pointbuffer
- Buffer distance
sourcepub fn buffer_bbox(&self, bbox: &Rect<T>, buffer: T) -> Rect<T>
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 boxbuffer
- Buffer distance
Trait Implementations§
source§impl<T> Clone for CheapRuler<T>where
T: Float + Debug + Clone,
impl<T> Clone for CheapRuler<T>where T: Float + Debug + Clone,
source§fn clone(&self) -> CheapRuler<T>
fn clone(&self) -> CheapRuler<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T> PartialEq for CheapRuler<T>where
T: Float + Debug + PartialEq,
impl<T> PartialEq for CheapRuler<T>where T: Float + Debug + PartialEq,
source§fn eq(&self, other: &CheapRuler<T>) -> bool
fn eq(&self, other: &CheapRuler<T>) -> bool
self
and other
values to be equal, and is used
by ==
.