simplify_dp_into

Function simplify_dp_into 

Source
pub fn simplify_dp_into(
    pts: &[LngLat],
    tolerance_m: f64,
    method: SimplifyMethod,
    out: &mut Vec<LngLat>,
) -> usize
Expand description

Simplify a polyline using the Douglas-Peucker algorithm.

Returns the simplified points in the output vector and the count of points kept. The output vector is cleared before simplification begins.

§Arguments

  • pts - Input polyline points in longitude, latitude order
  • tolerance_m - Maximum perpendicular distance threshold in meters (or coordinate units for EuclidRaw)
  • method - Distance calculation method to use
  • out - Output vector to store simplified points (will be cleared)

§Returns

Number of points in the simplified polyline (same as out.len() after the call)

§Examples

use rapidgeo_distance::LngLat;
use rapidgeo_simplify::{simplify_dp_into, SimplifyMethod};

let points = vec![
    LngLat::new_deg(-122.0, 37.0),
    LngLat::new_deg(-121.5, 37.5), // This might be removed if tolerance is high
    LngLat::new_deg(-121.0, 37.0),
];

let mut simplified = Vec::new();
let count = simplify_dp_into(
    &points,
    1000.0, // 1km tolerance
    SimplifyMethod::GreatCircleMeters,
    &mut simplified,
);

assert!(count >= 2); // At least endpoints preserved
assert_eq!(simplified[0], points[0]); // First point preserved
assert_eq!(simplified[count - 1], points[points.len() - 1]); // Last point preserved

§Algorithm

Implements the Douglas-Peucker algorithm:

  1. Draw line between first and last points
  2. Find point with maximum perpendicular distance to this line
  3. If distance > tolerance, keep the point and recurse on both segments
  4. Otherwise, drop all intermediate points