region-labels
The region-labels package is aimed to algorithmically generate labels for two-dimensional geographic regions which will best adapt to the shape and size of the region to attempt to maximally fill the space.
JSFiddle Demo
Here is a JSFiddle demo: https://jsfiddle.net/sikanrong/8304Lpvr/
Documentation
constructor
Generally the RegionLabel class constructor takes a GeoJSON object as the fist parameter, and an options object as the second. The GeoJSON object can either be a single Feature or a FeatureCollection. Geometry of Polygon and MultiPolygon are accepted.
The following options are accepted:
- margin - accepts an integer of how many pixels to leave as margin between the label and the edge of the canvas
- canvasDims - the dimensions of the desired label must be passed in as options so that the given geography and label can be centered within those dimensions
- canvas - optionally an existing canvas of desired size can be passed instead of canvasDims
- label - the desired label for the region. If label is a string, the passed GeoJSON FeatureCollection will be labeled as a group. If label is a function, the function will be called to determine the label to draw for each Feature within the FeatureCollection.
- excludeFeatures - A function to which the properties of each Feature is passed. If the function returns true, that feature will be excluded from the labeling; e.g. the label will actively avoid that feature and label the rest as a group. Useful for countries or administrative areas landlocked entrely within some other single region.
rl = geojson margin: 20 canvasDims: 256 256 label: 'United States';
drawLabels
The drawLabels function will draw all of the generated labels to a canvas, and return that canvas.
var labelCanvas = rl;//Draws the generated labels to some other canvasctx;
drawGeometries
The drawGeometries function will draw all of the passed GeoJSON to a canvas and return that canvas.
var geometryCanvas = rl;ctx;
debugDraw
This will return a canvas with some colored lines and points which are mathematically meaninful in terms of how final the label spline is calculated. Primarily used for debugging; useful as an overlay atop the label/geometry data.
var debugCanvas = rl;ctx;
Tests
Run tests just by executing node ./test.js
Authors
Written in Barcelona and Santa Cruz de Tenerife, Spain. 2017-2018
Alexander Pilafian <sikanrong@gmail.com>
Aleksandra Gabka <rivvel@gmail.com>