JSTS (JavaScript Topology Suite) is a powerful JavaScript library for geometric operations. In this tutorial, we’ll explore how to use JSTS’s simplification algorithms with interactive code examples.
🚀 Interactive Demo
Click the button below to open a live, executable notebook where each code cell can be run independently:
Or view it embedded below:
What is JSTS?
JSTS is a JavaScript port of the Java Topology Suite (JTS), providing:
- Geometric operations (union, intersection, buffer, etc.)
- Topology-preserving simplification algorithms
- Spatial predicates and analysis
- GeoJSON support
Simplification Algorithms
JSTS provides several simplification algorithms:
1. TopologyPreservingSimplifier
Based on Douglas-Peucker but prevents self-intersections and maintains valid geometry. Similar to PostGIS’s ST_SimplifyPreserveTopology.
2. DouglasPeuckerSimplifier
Standard Douglas-Peucker algorithm - fast but can create invalid geometries.
3. VWSimplifier (Visvalingam-Whyatt)
Area-based simplification that often produces visually pleasing results.
Code Walkthrough
Below are snippets from the interactive notebook demonstrating the simplification process.
1. Import Libraries
Using Deno, we import JSTS directly from a CDN.
1 | // Load JSTS library |
2. Simplification Logic
We define a helper function to switch between different simplification strategies.
1 | function simplifyGeometry(geojson, tolerance, method = 'topology') { |
3. Visualization with SVG (Auto-Scaling)
The visualization function has been updated to automatically calculate bounding boxes, ensuring that any polygon—regardless of size or position—fits perfectly within the SVG view panel.
1 | // Helper to get bounding box |
Comparison Results
The notebook compares the algorithm on a procedurally generated “Noisy Circle” (200+ vertices) to truly test the simplification capabilities.
| Algorithm | Speed | Validity | Aggressiveness | Best For |
|---|---|---|---|---|
| TopologyPreserving | Medium | ✅ Always Valid | Conservative | GIS, CAD |
| DouglasPeucker | Fast | ⚠️ May Break | Aggressive | Visualization |
| Visvalingam-Whyatt | Medium | ✅ Usually Valid | Balanced | Cartography |
Conclusion
JSTS provides powerful geometry simplification algorithms for JavaScript. TopologyPreservingSimplifier is the safest choice for most applications, ensuring your simplified geometries remain valid. Use DouglasPeucker only when performance is critical and geometry validity can be compromised.
Open the interactive notebook above to try it yourself!