Skip to content

A Rust implementation of the Polylabel algorithm, with FFI.

License

Notifications You must be signed in to change notification settings

ComPathIC/pylylabel

 
 

Repository files navigation

Pylylabel

A Rust implementation of the Polylabel algorithm.

This is a fork of polylabel-rs. Pylylabel's polylabel function also returns the distance from the label position to the polygon. Python binding is provided using PyO3.

The orange dot is the polygon centroid. The teal dot is the ideal label position. Red boxes show the search space. GIF

How to Use

extern crate pylylabel;
use pylylabel::polylabel;

extern crate geo;
use geo::{Point, Polygon};

let coords = vec![
    (0.0, 0.0),
    (4.0, 0.0),
    (4.0, 1.0),
    (1.0, 1.0),
    (1.0, 4.0),
    (0.0, 4.0),
    (0.0, 0.0)
];
let poly = Polygon::new(coords.into(), vec![]);
let label_pos, distance = polylabel(&poly, &0.10);
// Point(0.5625, 0.5625), 0.5625
from pylylabel import polylabel
from shapely.geometry import Polygon

exterior = [
    [0.0, 0.0],
    [4.0, 0.0],
    [4.0, 1.0],
    [1.0, 1.0],
    [1.0, 4.0],
    [0.0, 4.0],
    [0.0, 0.0]
]
pol = Polygon(exterior)
x, y, distance = polylabel(pol, tolerance=0.1)
# 0.5625, 0.5625, 0.5625

Documentation

https://docs.rs/polylabel

Performance vs Accuracy

Using a 4-core 2.3 GHz Intel Core i5, finding a label position on a ~9k-vertex polygon (representing the Norwegian mainland) using a tolerance of 1.0 takes around 9 ms. Depending upon the dimensions of your polygon(s), you may require a higher tolerance (i.e. a smaller number). See here for some guidance on the accuracy provided by each decimal place.

Build

Requirements:

maturin build --release

License

MIT

About

A Rust implementation of the Polylabel algorithm, with FFI.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 98.9%
  • Python 1.1%