https://github.com/avitase/rdppy
Python implementation of the Ramer-Douglas-Peucker algorithm
https://github.com/avitase/rdppy
python python3 ramer-douglas-peucker ramer-douglas-peucker-algorithm
Last synced: about 2 months ago
JSON representation
Python implementation of the Ramer-Douglas-Peucker algorithm
- Host: GitHub
- URL: https://github.com/avitase/rdppy
- Owner: avitase
- License: mit
- Created: 2021-10-15T13:30:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-01T14:02:04.000Z (over 3 years ago)
- Last Synced: 2025-03-15T01:16:57.544Z (2 months ago)
- Topics: python, python3, ramer-douglas-peucker, ramer-douglas-peucker-algorithm
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# RDPpy
[](https://www.python.org/downloads/release/python-380/)
[](https://pypi.org/project/rdppy/)
[](https://github.com/psf/black)Implementation of the Ramer-Douglas-Peucker algorithm for polylines using a point-to-line-segment distance measure.
## Usage
The API consists of a single function `rdppy.filter(points, threshold)` which returns a binary mask upon calling with a sequence of points and a threshold (aka the _epsilon_ value):
```python
>>> import rdppy
>>> points = [(0, 0),
... (4, 0),
... (0, 1),
... (1, 1),
... (1, 2),
... (2, 2),
... (2, 3),
... (3, 3),
... (3, 4),
... (5, 4)]
>>> mask = rdppy.filter(points, threshold=.9)
>>> mask
array([True, True, True, False, False, False, False, False, True, True])
```
This mask is a `numpy array` and can be used to filter a given sequence, e.g.,
```python
>>> import numpy as np
>>> np.array(points)[mask]
array([[0, 0],
[4, 0],
[0, 1],
[3, 4],
[5, 4]])
```
Note that this allows the filtering of more complex sequences which carry, for instance, meta information:
```python
>>> points = np.array([(0, 0, 'a'),
... (4, 0, 'b'),
... (0, 1, 'c'),
... (1, 1, 'd'),
... (1, 2, 'e'),
... (2, 2, 'f'),
... (2, 3, 'g'),
... (3, 3, 'h'),
... (3, 4, 'i'),
... (5, 4, 'j')])
>>> mask = rdppy.filter([(float(x), float(y)) for x, y, _ in points], .9)
>>> points[mask, -1]
array(['a', 'b', 'c', 'i', 'j'], dtype='>> def my_dist2(points, seg_start, seg_end):
... d = np.divide(seg_end - seg_start, np.sqrt(np.sum((seg_end - seg_start) ** 2)))
... return np.cross(points - np.expand_dims(seg_start, 0), np.expand_dims(d, 0)) ** 2
>>> rdppy.filter(points, threshold=.9, dist2_fun=my_dist2)
```
The maximum of the returned values is compared with the squared threshold value. By default the function `rdp.dist2` is used:
```python
rdppy.filter(points, threshold, dist2_fun=rdppy.rdp.dist2)
```## Installation
`RDPpy` releases are available as wheel packages for macOS, Windows and Linux on [PyPI](https://pypi.org/project/rdppy/).
Install it using pip:
```
python -m pip install -U pip
python -m pip install -U rdppy
```