Skip to main content

Implementation of the Ramer–Douglas–Peucker algorithm

Project description

RDPpy

Python 3.8 PyPI Code style: 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):

>>> 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.,

>>> 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:

>>> 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='<U21')

The default metric only works for 2D points but users may define custom metrics to measure the distance between a list of points, points, of any dimension and a segment parametrized via its start, seg_start, and end seg_end. For instance my_dist2 measures the distances of 2D points to the (infinite) line rather than the finite segment:

>>> 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:

rdppy.filter(points, threshold, dist2_fun=rdppy.rdp.dist2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdppy-0.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rdppy-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file rdppy-0.0.2.tar.gz.

File metadata

  • Download URL: rdppy-0.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for rdppy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d84fd29e60522136e37e297cd43d8a6882449b9501f7585d98e9087120e84981
MD5 244e895103951c20e40fe9b9db5d702f
BLAKE2b-256 294f3ba4d7765ced021831aa2e5d9880a04b87a0d8a3875ede000d09b86ac60e

See more details on using hashes here.

File details

Details for the file rdppy-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: rdppy-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for rdppy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 291ffdda0df135e3e14c849883ae26f34f1b44f5443845e932d013b6b34c9385
MD5 9c172230b23d4d6864bcd10a9975e6f4
BLAKE2b-256 48e380054a887807604e5434ad1e4ce9ae69b6eec8bb0021764ce4c95ce36100

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page