🔍 improving label collision detection

This commit is contained in:
Michael Straßburger 2016-09-20 22:36:22 +02:00
parent c717b7504f
commit 8ccca7f3e0
3 changed files with 22 additions and 17 deletions

View File

@ -21,18 +21,22 @@ Discover the world in your console!
` termap` ` termap`
## Behind the scenes ## Behind the scenes
### Libraries ### Libraries
#### Mastering the console
* [`drawille-canvas-blessed-contrib`](https://github.com/yaronn/drawille-canvas-blessed-contrib/) for braille rendering
* [`term-mouse`](https://github.com/CoderPuppy/term-mouse) for mouse handling
* [`keypress`](https://github.com/TooTallNate/keypress) for input handling
* [`drawille-canvas-blessed-contrib`](https://github.com/yaronn/drawille-canvas-blessed-contrib/) for braille rendering #### Discovering the map data
* [`term-mouse`](https://github.com/CoderPuppy/term-mouse) for mouse handling * [`node-mbtiles`](https://github.com/mapbox/node-mbtiles) for [MBTiles](https://github.com/mapbox/mbtiles-spec/blob/master/1.2/spec.md) parsing
* [`keypress`](https://github.com/TooTallNate/keypress) for input handling * [`pbf`](https://github.com/mapbox/pbf) for [Protobuf](https://developers.google.com/protocol-buffers/) decoding
* [`node-mbtiles`](https://github.com/mapbox/node-mbtiles) for MBTiles parsing
* [`pbf`](https://github.com/mapbox/pbf) for Protobuf decoding
* [`vector-tile-js`](https://github.com/mapbox/vector-tile-js) for [VectorTile](https://github.com/mapbox/vector-tile-spec/tree/master/2.1) parsing * [`vector-tile-js`](https://github.com/mapbox/vector-tile-js) for [VectorTile](https://github.com/mapbox/vector-tile-spec/tree/master/2.1) parsing
* [`rbush`](https://github.com/mourner/rbush) for 2D spatial indexing
#### Juggling the vectors and numbers
* [`rbush`](https://github.com/mourner/rbush) for 2D spatial indexing based label and mouse collision detection
* [`sphericalmercator`](https://github.com/mapbox/node-sphericalmercator) for EPSG:3857 <> WGS84 conversions * [`sphericalmercator`](https://github.com/mapbox/node-sphericalmercator) for EPSG:3857 <> WGS84 conversions
## Wishlist ## Wishlist
* node port of [libdrawille](https://github.com/Huulivoide/libdrawille) - well optimized library, supporting filled polygons * node port of [libdrawille](https://github.com/Huulivoide/libdrawille) - well optimized library, supporting filled polygons

View File

@ -13,15 +13,16 @@ module.exports = class LabelBuffer
writeIfPossible: (text, x, y) -> writeIfPossible: (text, x, y) ->
point = @project x, y point = @project x, y
return false unless @_hasSpace text, point[0], point[1] if @_hasSpace text, point[0], point[1]
@tree.insert @_calculateArea text, point[0], point[1] @tree.insert @_calculateArea text, point[0], point[1]
true else
false
_hasSpace: (text, x, y) -> _hasSpace: (text, x, y) ->
not @tree.collides @_calculateArea text, x, y not @tree.collides @_calculateArea text, x, y, 0
_calculateArea: (text, x, y) -> _calculateArea: (text, x, y, margin = @margin) ->
minX: x-@margin minX: x-margin
minY: y-@margin minY: y-margin
maxX: x+@margin+text.length maxX: x+margin+text.length
maxY: y+@margin maxY: y+margin

View File

@ -285,6 +285,6 @@ class Termap
termap = new Termap() termap = new Termap()
# TODO: abstracing this class, create loader class # TODO: abstracing this class, create loader class
data = fs.readFileSync __dirname+"/tiles/world.pbf.gz" data = fs.readFileSync __dirname+"/tiles/regensburg.pbf.gz"
termap.features = termap._getFeatures termap._parseTile data termap.features = termap._getFeatures termap._parseTile data
termap._draw() termap._draw()