Entry Point Logic (index.js)
Relevant source files
The following files were used as context for generating this wiki page:
The index.js file serves as the primary entry point for the geo-path-reducer library. Its main purpose is to export the geoPathReducer function, which implements the Douglas-Peucker algorithm to simplify geographic paths represented by arrays of latitude and longitude coordinates. This process reduces the number of points in a curve while maintaining its overall shape based on a specified tolerance level, known as epsilon.
Sources: src/index.js:1-3, README.md:3-8, package.json:6
Core Algorithm Implementation
The entry point defines the recursive logic of the Douglas-Peucker algorithm. It evaluates a set of points by finding the point furthest from the line segment connecting the first and last points. If this maximum distance exceeds the provided epsilon value, the algorithm splits the path at that point and recurses on both halves.
Initialization and Distance Calculation
The function begins by iterating through the internal points of the provided array to calculate their perpendicular distance from the line segment formed by the start and end points of the current path segment.
Recursive Reduction Logic
The branching logic determines whether a segment needs further simplification or can be reduced to just its endpoints.
| Condition | Action | Result |
|---|---|---|
dmax > epsilon | Split the array at index. Recursively call simplification on the left and right sub-arrays. | A concatenated array of simplified sub-paths. |
dmax <= epsilon | No points are far enough to warrant keeping. | An array containing only the start and end points. |
Sources: src/index.js:17-26
The following sequence diagram illustrates the recursive flow and the internal dependency on the distance calculation modules:
Dependency Integration
The entry point coordinates two specialized utility modules to perform the mathematical heavy lifting required for spatial analysis:
- perpendicular-distance.js: Provides the logic to determine how far a point is from a line segment using Heron's formula to find the area of the triangle formed by the three points.
- distance.js: Implements the Haversine formula (Great-circle distance) to calculate the distance between two coordinates in kilometers, using Earth's radius (6371 km).
Sources: src/index.js:1, src/perpendicular-distance.js:1-11, src/distance.js:3-15
Module Configuration and Usage
The module is configured as an ES module ("type": "module") and exposes the reducer as a default export.
API Parameters
The geoPathReducer function accepts the following arguments:
| Parameter | Type | Description |
|---|---|---|
points | Array<Object> | An array of objects, each containing lat and lng properties. |
epsilon | Number | The maximum allowable distance (tolerance) between the original and simplified curve, measured in degrees. |
Sources: src/index.js:3, README.md:23-28, package.json:10
Implementation Example
import geoPathReducer from 'geo-path-reducer';
const points = [{ lat: 37.7749, lng: -122.4194 }, ...];
const epsilon = 0.0001;
const result = geoPathReducer(points, epsilon);
Sources: README.md:12-21
Conclusion
The index.js file effectively orchestrates the Douglas-Peucker simplification process by managing recursive splits and leveraging external distance calculations. It provides a clean, single-function interface for reducing geographic path complexity, which is essential for optimizing map data rendering and spatial data processing.
Sources: src/index.js:3-27, README.md:3-5