npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@windfish-studio/region-labels

v1.1.4

Published

This package is aimed to algorithmically generate labels for two-dimensional geographic regions which will best adapt to the shape and size of the region.

Downloads

7

Readme

region-labels

This package is aimed to algorithmically generate labels for two-dimensional geographic regions which will best adapt to the shape and size of the region.

Italy labelled using region-labels algorithm, with debug lines

JSFiddle Demo

Here is a JSFiddle demo: https://jsfiddle.net/sikanrong/8304Lpvr/

Change log

June 14, 2019
  • Adding support for DBSCAN clustering. The algorithm can now automatically detect clusters of shapes within a feature and label each cluster separately.
  • Massive cleaning of the code; using proper ES6 class structure and import/export syntax. Breaking up this giant monolith into smaller pieces and spreading it out across more source-files.
  • Proper scoping of member variables within the two main classes of the library.
  • Migrate the project to WebPack and babel instead of using browserify.
  • Use AVA test framework instead of the tape package.
  • Adding more tests for the DBSCAN functionality, as well as cleaning up the tests a lot by externalizing the expected test results into their own file.

Documentation

constructor

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. By default, if a FeatureCollection is given, each feature will be labelled individually.

Only geometry of type Polygon and MultiPolygon are supported.

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. label can either be a string, or a callback function which takes the properties object of each feature and returns the desired label.
{label: (_props) => {return _props.NAME}}
  • 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 entirely within some other single region.
{excludeFeatures: (_props) => {
    return (_props.LAND_AREA > 3.5);
}}
  • forceSingleLabel - if set to true, it will treat all features in the FeatureCollection as a single shape and generate a single label for all features. Defaults to false.

  • dbscanClustering - if set to true, the DBSCAN algorithm is used to automatically create groupings of shapes within a feature, and label them separately. Defaults to false.

  • dbscanPointDensity - The algorithm will overlay the geographies with a point grid to perform DBSCAN clustering. This parameter controls the density of the points of this grid, which will hold (dbscanPointDensity)² points. Defaults to 10.

  • dbscanMinPoints - The minimum number of points for the DBSCAN algorithm to consider that cluster as part of a labelling-group. Defaults to 2.

  • dbscanRadiusScale - The clustering radius for the DBSCAN algorithm is by default set as the greatest distance between points on the grid, multiplied by dbscanRadiusScale. Defaults to 2.

drawLabels

The drawLabels function will draw all of the generated labels to a canvas, and return that canvas.

var labelCanvas = rl.drawLabels();
//Draws the generated labels to some other canvas
ctx.drawImage(labelCanvas, 0, 0); 

drawGeometries

The drawGeometries function will draw all of the passed GeoJSON to a canvas and return that canvas.

var geometryCanvas = rl.drawGeometries();
ctx.drawImage(geometryCanvas, 0, 0);

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.debugDraw();
ctx.drawImage(debugCanvas, 0, 0);

Tests

Run tests just by executing

npm run test
TAP version 13
# should use DBSCAN to correctly separate USA into logical labeling groups
ok 1 - should use DBSCAN to correctly separate USA into logical labeling groups
# should generate labels for all test geographies without issue
ok 2 - should generate labels for all test geographies without issue
# should generate correct label data for Texas
ok 3 - should generate correct label data for Texas
# should generate correct label data for New Jersey
ok 4 - should generate correct label data for New Jersey
# should generate correct label data for Hawaii
ok 5 - should generate correct label data for Hawaii
# should generate correct label data for Russia. Should wrap Iultinsky District
ok 6 - should generate correct label data for Russia. Should wrap Iultinsky District

1..6
# tests 6
# pass 6
# fail 0

Authors

Written in Barcelona and Santa Cruz de Tenerife, Spain. 2017-2019

Alexander Pilafian <[email protected]>
Aleksandra Gabka <[email protected]>