rapidgeo-simplify
Polyline simplification using the Douglas-Peucker algorithm with pluggable distance backends.
Installation
[]
= "0.1"
# For parallel processing support
= { = "0.1", = ["batch"] }
Quick Start
use LngLat;
use ;
let points = vec!;
let mut simplified = Vecnew;
let count = simplify_dp_into;
println!;
Why This Exists
The Douglas-Peucker algorithm simplifies polylines by removing points that don't significantly change the shape. Points are kept only if they deviate more than the specified tolerance from the simplified line. This implementation provides multiple distance calculation backends:
- GreatCircleMeters: Spherical distance calculations for global accuracy
- PlanarMeters: ENU projection for regional work with better performance
- EuclidRaw: Raw coordinate differences for non-geographic data
Examples
Basic Simplification
use LngLat;
use ;
let points = vec!;
let mut mask = Vecnew;
simplify_dp_mask;
// mask[i] is true if points[i] should be kept
for in mask.iter.enumerate
Batch Processing
use simplify_batch;
let polylines = vec!;
let simplified = simplify_batch;
Parallel Processing (requires batch feature)
use simplify_batch_par;
let simplified = simplify_batch_par;
Distance Methods
GreatCircleMeters
Uses spherical distance calculations. Best for:
- Global datasets
- High accuracy requirements
- Cross-country or intercontinental paths
PlanarMeters
Projects to East-North-Up coordinates around the polyline's midpoint. Best for:
- Regional datasets (city/state level)
- Better performance than great circle
- Reasonable accuracy for smaller areas
EuclidRaw
Direct coordinate subtraction. Best for:
- Non-geographic coordinate systems
- Screen coordinates
- Already-projected data
API
Core Functions
simplify_dp_into(points, tolerance, method, output)- Simplify into output vectorsimplify_dp_mask(points, tolerance, method, mask)- Generate keep/drop mask
Batch Functions (feature = "batch")
simplify_batch(polylines, tolerance, method)- Process multiple polylinessimplify_batch_par(polylines, tolerance, method)- Parallel batch processingsimplify_dp_into_par(points, tolerance, method, output)- Parallel single polyline
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
The algorithm guarantees that:
- First and last points are always preserved
- No point deviates more than tolerance from the simplified line
- Shape characteristics are maintained
Performance
- Single-threaded: ~1M points/second for typical GPS traces
- Memory: O(n) for input, O(log n) stack depth
- Parallel processing available for large datasets with
batchfeature
Limitations
- Requires at least 2 points (endpoints always preserved)
- GreatCircleMeters doesn't handle antimeridian crossing optimally
- PlanarMeters accuracy decreases for very large regions
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.