#geo #polylabel #gis

polylabel

A Rust implementation of the Polylabel algorithm for finding optimum polygon label positions

30 releases (stable)

✓ Uses Rust 2018 edition

new 2.3.1 Jun 29, 2020
2.2.0 Apr 11, 2020
2.0.0 Nov 8, 2019
1.3.2 Oct 28, 2019
0.1.6 Oct 12, 2016

#21 in Science

Download history 33/week @ 2020-03-12 82/week @ 2020-03-19 34/week @ 2020-03-26 14/week @ 2020-04-02 151/week @ 2020-04-09 96/week @ 2020-04-16 12/week @ 2020-04-23 21/week @ 2020-04-30 9/week @ 2020-05-07 95/week @ 2020-05-14 24/week @ 2020-05-21 96/week @ 2020-05-28 148/week @ 2020-06-04 49/week @ 2020-06-11 190/week @ 2020-06-18 121/week @ 2020-06-25

334 downloads per month
Used in 2 crates

MIT and maybe MPL-2.0

400KB
801 lines

Build Status Build status Coverage Status

Polylabel-rs

A Rust implementation of the Polylabel algorithm

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

You can generate this visualisation yourself by cloning this repo, switching to the visualise branch, and opening the visualise.ipynb Jupyter notebook, then stepping through the cells. You can also easily visualise a Polygon of your own using the notebook.

How to Use

extern crate polylabel;
use polylabel::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 = polylabel(&poly, &0.10);
// Point(0.5625, 0.5625)

Command-Line Tool

A command-line tool is available: cargo install polylabel_cmd. This enables the polylabel command, which takes a GeoJSON file as input, as well as an optional (-t / --tolerance) tolerance value. See more at crates.io.

Documentation

https://docs.rs/polylabel

FFI

Call polylabel_ffi with the following three mandatory arguments:

  • Array (a struct with two fields):
    • data: a void pointer to an array of two-element c_double arrays, each of which represents a point on the exterior Polygon shell)
    • len: the length of the data array, a size_t
  • WrapperArray (a struct with two fields):
    • data: a void pointer to an array of Arrays, each entry representing an interior Polygon ring. Empty if there are no rings.
    • len: the length of the data array, a size_t. 0 if it's empty.
  • tolerance, a c_double

The function returns a struct with two c_double fields:

  • x_pos
  • y_pos

A Python example is available in ffi.py

An auto-generated header file is available at include/header.h

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.

Binaries

Binary libs for:

  • x86_64 *nix (built using manylinux1, thus easy to include in Python 2.7 / 3.5 / 3.6 wheels) and OS X
  • i686 and x86_64 Windows

are available in releases.

License

MIT

Dependencies

~3MB
~63K SLoC