Git Product home page Git Product logo

labelgun's Introduction

labelgun


What is Labelgun?

Labelgun is a mapping library agnostic labelling engine. It allows you to avoid cluttering in mapping popups and labels, providing precedence to labels of your choice.

The library makes three assumptions:

  • Each label has a bounding rectangle (Min X, Min Y, Max X, Max Y)
  • Each label has a weight
  • You can provide a function that will hide and show a label (e.g. changing a CSS class or calling a mapping library method)

Labelgun example in leaflet


Using Labelgun

Use

You can use labelgun in your project via npm such as:

npm install labelgun --save

Or if you're using yarn:

yarn add labelgun

You can also use a auto-generated CDN thanks to unpkg :

https://unpkg.com/[email protected]/lib/labelgun.js

Docs and Demos

Check out the docs and demos live here

A nice interactive way to play with the demos locally is to use a hot reloading web server such as live-server:

npm install -g live-server

live-server

Developing

For instructions please see the DEVELOPING document.

Users

  • Vroom - Vehicle routing optimisation software
  • OL Mapbox Style - Use Mapbox Style objects with OpenLayers
  • qgis2web - A QGIS plugin to export a map to an OpenLayers/Leaflet webmap

Using Labelgun? Open a pull request and let us know!

Acknowledgements

Labelgun as an open source project was made possible thanks to Podaris.

License

MIT

labelgun's People

Contributors

davidpiesse avatar jameslmilner avatar tomchadwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

labelgun's Issues

Example fails locally

example.js:20 Fetch API cannot load file:///C:/Users/tchadwin/Desktop/labelgun-master/examples/geojson/cupcakes.geojson. URL scheme must be "http" or "https" for CORS request.
(anonymous) @ example.js:20

Compatibility

When used in a QtWebkit environment, this fails because of no support for Set, I believe. What's the best approach - a polyfill? Or is there some option in your build process to compile a version compatible with older JS engines?

Other errors might subsequently transpire, but let's fix them as they arise.

OpenLayers3 example has collisions at certain zooms

It seems very difficult to get to a point where there are no label collisions on the OpenLayers3 example. I think this is more a OpenLayers3 issue than a Labelgun issue. There doesn't appear to be a strict 'this layer has loaded and rendered' event in OL3.

screenshot_20170109_152331

Differentiate initial hide() from subsequent hide()

I've added an opacity transition to the style, and this works well. However, this means all overlapping labels initially display on map load, immediately transitioning to fade out.

What would be the easiest way to differentiate calls to hideLabel() made initially from subsequent ones? The transition could then be removed for the subsequent calls.

Multiple layers

I think there's a simple enhancement to make this work across multiple layers. PR coming to see whether you think it works.

Leaflet example doesn't use L.Tooltip?

Warning: slightly rambling ignorant thought follows.

Obviously, Labelgun is mapping-library-agnostic, which is great. It also aims to be extremely efficient, using rbush under the hood. This is also splendid.

However, the Leaflet example doesn't (I think) use Leaflet 1's new L.Tooltip. Is it possible to use it? There would be obvious advantages to using it. It could be that failing to use it compromises performance (a guess).

Leaflet.VectorGrid implementation

An example in use with Leaflet.VectorGrid would be incredibly useful. As it stands, L.VectorGrid doesn't handle labelling. There are a couple of open PRs which could enable it, but they all produce one label per feature per tile. This means blurry text if Tooltips with no background are used.

If Labelgun could be implemented, it would automatically get rid of the duplicate labels (this was @perliedman's idea). However, I'm not having any joy implementing it with L.VectorGrid. The Leaflet example uses L.LayerGroup.eachFeature(), but this is not available in L.VectorGrid.

Any help would be very welcome indeed.

OL4 example: does the label text really have to be built into an SVG?

Without Labelgun, I label OL features as follows:

new ol.style.Style({
    text: new ol.style.Text({
    font: '10.725px \'MS Shell Dlg 2\', sans-serif',
    text: labelText,
    textBaseline: 'middle',
    textAlign: textAlign,
    offsetX: offsetX,
    offsetY: offsetY,
    fill: new ol.style.Fill({
        color: 'rgba(0, 0, 0, 1)'
    })
});});

The Labelgun OL4 examples seems to write the text into an SVG:

var iconSVG = "<svg " +
                    "version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" " +
                    "x=\"0px\" y=\"0px\" width=\"" + labelWidth + "px\" height=\""+labelHeight+"px\" " +
                    "viewBox=\"0 0 " + labelWidth + " "+labelHeight+"\" " +
                    "enable-background=\"new 0 0 " + labelWidth + " "+labelHeight+"\" >"+
                    "<g>" +
                    "<rect x=\"0\" y=\"0\" width=\"" + labelWidth + "\" height=\""+labelHeight+"\" stroke=\"#000000\" fill=\"" + fillColor + "\" stroke-width=\"2\"></rect>" +
                    "<text x=\"5\" y=\"14\" fill=\"#000000\" font-family=\"Arial\" font-size=\""+labelFontSize+"\" font-weight=\"normal\">" +
                        _.escape(text) +  // We need to escape all the special characters like & etc
                    "</text>" +
                    "</g>" +
                "</svg>";

  var svgURI = encodeURIComponent(iconSVG);
  var src = "data:image/svg+xml;charset=utf-8," + svgURI;

Is this a prerequisite for Labelgun in OL4, or could it be applied to my existing labelling method?

Show an example of draggin a marker/label

Labelgun has the ability to set a label to be 'dragged' which in turn makes it's weight infinite and always shown in the collision calculations. An example in Leaflet or OpenLayers would be super!

Here's an example of how you might do it:

   // The last argument is dragged which we set to true
    labelEngine.ingestLabel(
      boundingBox,
      id,
      parseInt(Math.random() * (5 - 1) + 1), // Weight
      label,
      "Test " + id,
      true 
    ); 

TypeError: layer.getTooltip(...) is undefined

I'm getting this error:

TypeError: layer.getTooltip(...) is undefined
    addLabel file:///C:/Users/tchadwin/Desktop/é/qgis2web_2019_06_17-15_11_29_065534/js/labels.js:29
    resetLabels file:///C:/Users/tchadwin/Desktop/é/qgis2web_2019_06_17-15_11_29_065534/js/labels.js:20
    eachLayer file:///C:/Users/tchadwin/Desktop/é/qgis2web_2019_06_17-15_11_29_065534/js/leaflet.js:5```

Relevant line is:

```javascript
  // This is ugly but there is no getContainer method on the tooltip :(
  var label = layer.getTooltip()._source._tooltip._container;

This implementation was taken from the labelgun Leaflet examples.

I'm assuming this must have occurred with a Leaflet version upgrade, but other than that, I'm in the dark at the moment.

Confusing Documentation

I feel like this may be under utilized because of how confusing setting it up is.

Also when using browser only getting errors that prevent running.

_hideShownCollisions should not be required in update()

For one reason or another we have to call this._hideShownCollisions() in order to hide labels that shouldn't be shown (I'd say it effects about 5% of labels). Not sure what the root cause of this is, but obviously a hack and decreases performance so would be A+++ to get rid of.

Issues with new algorithm

I'm not sure the new algorithm is as successful as the old. I'm getting frequent overlaps:

2017-09-06 13_10_12-index html

I would also say that some plausible candidates for labelling have no label:

2017-09-06 13_12_28-index html

Performance enhancements

It would be great to improve the performance of the library. You can use :

npm run benchmark

to check the general speed of the update function (probably the most crucial for most people).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.