🐁 adding Renderer.featuresAt to return features for mouse positions

This commit is contained in:
Michael Straßburger 2016-09-29 20:03:53 +02:00
parent 80edcc3f04
commit 0e481bf5c9
5 changed files with 34 additions and 14 deletions

View File

@ -48,6 +48,8 @@ No web browser around? No worries - discover the planet in your console!
* [`sphericalmercator`](https://github.com/mapbox/node-sphericalmercator) for [EPSG:3857](http://spatialreference.org/ref/sr-org/6864/) <> [WGS84](http://spatialreference.org/ref/epsg/wgs-84/) conversions
### TODOs
* [ ] mouse hover of POIs (maybe even polygons?)
* [ ] termap-server - telnet and ssh access
* [ ] cli linking
* [ ] mapping of view to tiles to show
* [x] abstracted MapBox style JSON support

View File

@ -9,7 +9,9 @@
rbush = require 'rbush'
module.exports = class LabelBuffer
treeWithMargin: null
tree: null
margin: 5
constructor: (@width, @height) ->
@ -18,22 +20,28 @@ module.exports = class LabelBuffer
clear: ->
@tree.clear()
project: (x, y) ->
[Math.floor(x/2), Math.floor(y/4)]
writeIfPossible: (text, x, y, margin = @margin) ->
writeIfPossible: (text, x, y, feature, margin = @margin) ->
point = @project x, y
if @_hasSpace text, point[0], point[1]
@tree.insert @_calculateArea text, point[0], point[1], margin
data = @_calculateArea text, point[0], point[1], margin
data.feature = feature
@tree.insert data
else
false
_hasSpace: (text, x, y) ->
not @tree.collides @_calculateArea text, x, y, 0
featuresAt: (x, y) ->
@tree.search minX: x, maxX: x, minY: y, maxY: y
_calculateArea: (text, x, y, margin) ->
_hasSpace: (text, x, y) ->
not @tree.collides @_calculateArea text, x, y
_calculateArea: (text, x, y, margin = 0) ->
minX: x-margin
minY: y-margin
minY: y-margin/4
maxX: x+margin+text.length
maxY: y+margin
maxY: y+margin/4

View File

@ -100,6 +100,10 @@ module.exports = class Renderer
@isDrawing = false
featuresAt: (x, y) ->
@labelBuffer.featuresAt x, y
_write: (output) ->
process.stdout.write output
@ -165,9 +169,9 @@ module.exports = class Renderer
x = point[0] - text.length
margin = @config.layers[layer]?.margin or @config.labelMargin
if @labelBuffer.writeIfPossible text, x, point[1], margin
if @labelBuffer.writeIfPossible text, x, point[1], feature, margin
@canvas.text text, x, point[1], colorCode
else if @config.layers[layer]?.cluster and @labelBuffer.writeIfPossible "X", point[0], point[1], 3
else if @config.layers[layer]?.cluster and @labelBuffer.writeIfPossible "X", point[0], point[1], feature, 3
@canvas.text "", point[0], point[1], colorCode
_scaleAndReduce: (points, scale) ->

View File

@ -97,8 +97,10 @@ module.exports = class Termap
else
@mouseDragging = x: event.x, y: event.y
# update internal mouse tracker
@mousePosition = x: event.x, y: event.y
# update internal mouse tracker
@mousePosition = x: event.x, y: event.y
@renderer.notify @_getFooter()
_onKey: (key) ->
# check if the pressed key is configured
@ -138,9 +140,13 @@ module.exports = class Termap
#mercator.inverse([x - width/2, y + width/2]).concat mercator.inverse([x + width/2, y - width/2])
_getFooter: ->
#{}"center: [#{utils.digits @center.lat, 2}, #{utils.digits @center.lng, 2}] zoom: #{utils.digits @zoom, 2}"
"bbox: [#{@_getBBox().map((z) -> utils.digits(z, 2)).join(', ')}]"
#{}"#{@mouseDragging.x} #{@mouseDragging.y} #{@mousePosition.x} #{@mousePosition.y}"
features = @renderer.featuresAt @mousePosition.x-1-(@view[0]>>1), @mousePosition.y-1-(@view[1]>>2)
"features: ["+features.map((f) -> f.feature.id).join(", ")+"] "+
"#{@mousePosition.x} #{@mousePosition.y}"
#"center: [#{utils.digits @center.lat, 2}, #{utils.digits @center.lng, 2}] zoom: #{utils.digits @zoom, 2}"
#"bbox: [#{@_getBBox().map((z) -> utils.digits(z, 2)).join(', ')}]"
#features.map((f) -> JSON.stringify f.feature.properties).join(" - ")
zoomBy: (step) ->
return @zoom = 0 if @zoom+step < 0

BIN
tiles/berlin.z12.pbf.gz Normal file

Binary file not shown.