package
A JS library for finding optimal label position inside a polygon
npm install polylabelGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
A fast algorithm for finding polygon pole of inaccessibility, the most distant internal point from the polygon outline (not to be confused with centroid), implemented as a JavaScript library. Useful for optimal placement of a text label on a polygon.
It's an iterative grid algorithm, inspired by paper by Garcia-Castellanos & Lombardo, 2007. Unlike the one in the paper, this algorithm:

This is an iterative grid-based algorithm, which starts by covering the polygon with big square cells and then iteratively splitting them in the order of the most promising ones, while aggressively pruning uninteresting cells.
cell_size * sqrt(2) / 2).cell_max - best_dist > precision),
split it into 4 children cells and put them in the queue.
Given polygon coordinates in
GeoJSON-like format (an array of arrays of [x, y] points)
and precision (1.0 by default),
Polylabel returns the pole of inaccessibility coordinate in [x, y] format. The distance to the closest polygon point (in input units) is included as a distance property.
const p = polylabel([[[0, 0], [1, 0], ...]], 1.0);
const distance = p.distance;Be careful to pick precision appropriate for the input units. E.g. in case of geographic coordinates (longitude and latitude), 0.000001 is appropriate, while the default (1.0) would be too imprecise.
TypeScript type definitions
are available via npm install --save @types/polylabel.
mapbox-npm-02
accounts+npmjs-02@mapbox.com
mapbox-npm-04
accounts+npmjs-04@mapbox.com
mapbox-npm-09
accounts+npmjs-09@mapbox.com
mapbox-npm-05
accounts+npmjs-05@mapbox.com
mapbox-npm-06
accounts+npmjs-06@mapbox.com
mapbox-npm-08
accounts+npmjs-08@mapbox.com
mapbox-npm-advanced-actions
accounts+npmjs-advanced-actions@mapbox.com
mapbox-npm-ci
accounts+npmjs-npm-ci@mapbox.com
mapbox-npm
accounts+npmjs@mapbox.com
mapbox-admin
accounts@mapbox.com
mapbox-machine-user
accounts+npm-mapbox-machine-user@mapbox.com
mbx-npm-ci-staging
yannick.meeus+npm+staging+ci@mapbox.com
mbx-npm-ci-production
yannick.meeus+npm+production+ci@mapbox.com
mbx-npm-01-production
yannick.meeus+npm+prod+01@mapbox.com
mbx-npm-02-production
yannick.meeus+npm+prod+02@mapbox.com
mbx-npm-03-production
yannick.meeus+npm+prod+03@mapbox.com
mbx-npm-04-production
yannick.meeus+npm+prod+04@mapbox.com
mbx-npm-05-production
yannick.meeus+npm+prod+05@mapbox.com
mbx-npm-06-production
yannick.meeus+npm+prod+06@mapbox.com
mbx-npm-07-production
yannick.meeus+npm+prod+07@mapbox.com
mbx-npm-08-production
yannick.meeus+npm+prod+08@mapbox.com
mbx-npm-09-production
yannick.meeus+npm+prod+09@mapbox.com
mbx-npm-02-staging
yannick.meeus@mapbox.com
mbx-npm-advanced-actions-staging
yannick.meeus+npm+staging+advanced+actions@mapbox.com
mbx-npm-advanced-actions-production
yannick.meeus+npm+prod+advanced+actions@mapbox.com
mourner
agafonkin@gmail.com
Estimated unpacked size across 2 packages.
20.8 kB
Includes dependencies.