GithubHelp home page GithubHelp logo

avitase / rdppy Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 11 KB

Python implementation of the Ramer-Douglas-Peucker algorithm

License: MIT License

Python 100.00%
ramer-douglas-peucker ramer-douglas-peucker-algorithm python python3

rdppy's Introduction

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)

Installation

RDPpy releases are available as wheel packages for macOS, Windows and Linux on PyPI. Install it using pip:

python -m pip install -U pip
python -m pip install -U rdppy

rdppy's People

Contributors

avitase avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.