pub fn simplify_dp_into(
pts: &[LngLat],
tolerance_m: f64,
method: SimplifyMethod,
out: &mut Vec<LngLat>,
) -> usizeExpand 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 ordertolerance_m- Maximum perpendicular distance threshold in meters (or coordinate units for EuclidRaw)method- Distance calculation method to useout- 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:
- Draw line between first and last points
- Find point with maximum perpendicular distance to this line
- If distance > tolerance, keep the point and recurse on both segments
- Otherwise, drop all intermediate points