2016-09-18 02:44:31 +02:00
|
|
|
Canvas = require 'drawille-canvas-blessed-contrib'
|
|
|
|
VectorTile = require('vector-tile').VectorTile
|
|
|
|
Protobuf = require 'pbf'
|
|
|
|
keypress = require 'keypress'
|
|
|
|
fs = require 'fs'
|
|
|
|
zlib = require 'zlib'
|
2016-09-20 15:21:24 +02:00
|
|
|
TermMouse = require 'term-mouse'
|
|
|
|
mercator = new (require('sphericalmercator'))()
|
2016-09-20 20:52:49 +02:00
|
|
|
LabelBuffer = require __dirname+'/src/LabelBuffer'
|
2016-09-20 15:21:24 +02:00
|
|
|
|
|
|
|
utils =
|
|
|
|
deg2rad: (angle) ->
|
|
|
|
# (angle / 180) * Math.PI
|
|
|
|
angle * 0.017453292519943295
|
|
|
|
rad2deg: (angle) ->
|
|
|
|
angle / Math.PI * 180
|
|
|
|
|
|
|
|
digits: (number, digits) ->
|
|
|
|
Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)
|
2016-09-20 20:52:49 +02:00
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
metersPerPixel: (zoom, lat = 0) ->
|
|
|
|
utils.rad2deg(40075017*Math.cos(utils.deg2rad(lat))/Math.pow(2, zoom+8))
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
|
|
|
|
class Termap
|
|
|
|
config:
|
2016-09-20 15:21:24 +02:00
|
|
|
zoomStep: 0.5
|
2016-09-18 21:50:09 +02:00
|
|
|
drawOrder: ["admin", "water", "landuse", "building", "road", "poi_label", "housenum_label"]
|
2016-09-18 17:46:14 +02:00
|
|
|
|
|
|
|
icons:
|
|
|
|
car: "🚗"
|
|
|
|
school: "🏫"
|
|
|
|
marker: "⭐"
|
|
|
|
'art-gallery': "🎨"
|
|
|
|
attraction: "❕"
|
|
|
|
stadium: "🏈"
|
|
|
|
toilet: "🚽"
|
|
|
|
cafe: "☕"
|
|
|
|
laundry: "👚"
|
|
|
|
bus: "🚌"
|
|
|
|
restaurant: "🍛"
|
2016-09-18 21:15:21 +02:00
|
|
|
lodging: "🛏."
|
2016-09-18 17:46:14 +02:00
|
|
|
'fire-station': "🚒"
|
|
|
|
shop: "🛍"
|
|
|
|
pharmacy: "💊"
|
|
|
|
beer: "🍺"
|
|
|
|
cinema: "🎦"
|
|
|
|
|
|
|
|
layers:
|
2016-09-18 21:15:21 +02:00
|
|
|
housenum_label:
|
2016-09-20 21:46:31 +02:00
|
|
|
minZoom: 2
|
2016-09-18 21:15:21 +02:00
|
|
|
color: 8
|
|
|
|
building:
|
2016-09-20 21:46:31 +02:00
|
|
|
minZoom: 3.5
|
2016-09-18 21:15:21 +02:00
|
|
|
color: 8
|
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
poi_label:
|
|
|
|
minZoom: 3
|
|
|
|
color: "yellow"
|
2016-09-18 21:50:09 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
road:
|
2016-09-20 15:21:24 +02:00
|
|
|
color: 15
|
2016-09-18 21:15:21 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
landuse:
|
|
|
|
color: "green"
|
|
|
|
water:
|
|
|
|
color: "blue"
|
|
|
|
admin:
|
|
|
|
color: "red"
|
|
|
|
|
|
|
|
mouse: null
|
|
|
|
width: null
|
|
|
|
height: null
|
|
|
|
canvas: null
|
|
|
|
|
|
|
|
isDrawing: false
|
|
|
|
lastDrawAt: 0
|
|
|
|
|
|
|
|
mousePosition: [0, 0]
|
|
|
|
mouseDragging: false
|
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
center:
|
|
|
|
lat: 49.019855
|
|
|
|
lng: 12.096956
|
|
|
|
|
2016-09-20 20:52:49 +02:00
|
|
|
zoom: 2
|
2016-09-20 21:46:31 +02:00
|
|
|
view: [0, 0]
|
2016-09-20 15:21:24 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
scale: 4
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
@_initControls()
|
|
|
|
@_initCanvas()
|
|
|
|
|
|
|
|
@_onResize =>
|
|
|
|
@_initCanvas()
|
|
|
|
@_draw()
|
|
|
|
|
|
|
|
_initControls: ->
|
|
|
|
keypress process.stdin
|
|
|
|
process.stdin.setRawMode true
|
|
|
|
process.stdin.resume()
|
|
|
|
|
|
|
|
process.stdin.on 'keypress', (ch, key) => @_onKey key
|
|
|
|
|
|
|
|
@mouse = TermMouse()
|
|
|
|
@mouse.start()
|
|
|
|
|
|
|
|
@mouse.on 'click', (event) => @_onClick event
|
|
|
|
@mouse.on 'scroll', (event) => @_onMouseScroll event
|
|
|
|
@mouse.on 'move', (event) => @_onMouseMove event
|
|
|
|
|
|
|
|
_initCanvas: ->
|
|
|
|
@width = Math.floor((process.stdout.columns-1)/2)*2*2
|
|
|
|
@height = Math.ceil(process.stdout.rows/4)*4*4
|
|
|
|
@canvas = new Canvas @width, @height
|
|
|
|
|
2016-09-20 21:46:31 +02:00
|
|
|
@zoom = Math.log(4096/@width)/Math.LN2
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
_onResize: (cb) ->
|
|
|
|
process.stdout.on 'resize', cb
|
|
|
|
|
|
|
|
_onClick: (event) ->
|
|
|
|
if @mouseDragging and event.button is "left"
|
|
|
|
@view[0] -= (@mouseDragging.x-@mousePosition.x)*2
|
|
|
|
@view[1] -= (@mouseDragging.y-@mousePosition.y)*4
|
|
|
|
@_draw()
|
|
|
|
|
|
|
|
@mouseDragging = false
|
|
|
|
|
|
|
|
_onMouseScroll: (event) ->
|
|
|
|
# TODO: handle .x/y for directed zoom
|
2016-09-18 21:50:09 +02:00
|
|
|
@zoomBy @config.zoomStep * if event.button is "up" then 1 else -1
|
2016-09-18 17:46:14 +02:00
|
|
|
@_draw()
|
|
|
|
|
|
|
|
_onMouseMove: (event) ->
|
|
|
|
# only continue if x/y are valid
|
|
|
|
return unless event.x <= process.stdout.columns and event.y <= process.stdout.rows
|
|
|
|
|
|
|
|
# start dragging
|
|
|
|
if not @mouseDragging and event.button is "left"
|
|
|
|
@mouseDragging = x: event.x, y: event.y
|
|
|
|
|
|
|
|
# update internal mouse tracker
|
|
|
|
@mousePosition = x: event.x, y: event.y
|
|
|
|
|
|
|
|
_onKey: (key) ->
|
|
|
|
# check if the pressed key is configured
|
|
|
|
draw = switch key?.name
|
|
|
|
when "q"
|
|
|
|
process.exit 0
|
|
|
|
|
2016-09-18 21:50:09 +02:00
|
|
|
when "z" then @zoomBy @config.zoomStep
|
|
|
|
when "a" then @zoomBy -@config.zoomStep
|
2016-09-18 17:46:14 +02:00
|
|
|
when "left" then @view[0] += 5
|
|
|
|
when "right" then @view[0] -= 5
|
|
|
|
when "up" then @view[1]+= 5
|
|
|
|
when "down" then @view[1]-= 5
|
|
|
|
|
|
|
|
else
|
|
|
|
false
|
|
|
|
|
|
|
|
if draw
|
|
|
|
@_draw()
|
|
|
|
else
|
|
|
|
# display debug info for unhandled keys
|
|
|
|
@notify JSON.stringify key
|
2016-09-18 02:44:31 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
_parseTile: (buffer) ->
|
|
|
|
# extract, decode and parse a given tile buffer
|
|
|
|
new VectorTile new Protobuf zlib.gunzipSync data
|
2016-09-18 15:26:09 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
_getFeatures: (tile) ->
|
|
|
|
features = {}
|
|
|
|
for name,layer of tile.layers
|
|
|
|
continue unless @config.layers[name]
|
2016-09-18 15:26:09 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
features[name] = for i in [0...layer.length]
|
2016-09-18 15:26:09 +02:00
|
|
|
feature = layer.feature i
|
2016-09-18 02:44:31 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
type: [undefined, "point", "line", "polygon"][feature.type]
|
|
|
|
id: feature.id
|
|
|
|
properties: feature.properties
|
|
|
|
points: feature.loadGeometry()
|
|
|
|
|
|
|
|
features
|
|
|
|
|
|
|
|
_draw: ->
|
|
|
|
return if @isDrawing
|
|
|
|
@isDrawing = true
|
|
|
|
@lastDrawAt = Date.now()
|
|
|
|
|
|
|
|
@canvas.clearRect 0, 0, @width, @height
|
|
|
|
#@_write @canvas._canvas.frame()
|
|
|
|
|
|
|
|
@canvas.save()
|
|
|
|
|
|
|
|
@canvas.translate @view[0], @view[1]
|
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
scale = Math.pow 2, @zoom
|
|
|
|
|
2016-09-20 20:52:49 +02:00
|
|
|
drawn = []
|
|
|
|
labelBuffer = new LabelBuffer()
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
for layer in @config.drawOrder
|
|
|
|
continue unless @features?[layer]
|
2016-09-18 02:44:31 +02:00
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
if @config.layers[layer].minZoom and @zoom > @config.layers[layer].minZoom
|
2016-09-18 21:15:21 +02:00
|
|
|
continue
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
@canvas.strokeStyle = @canvas.fillStyle = @config.layers[layer].color
|
2016-09-18 02:44:31 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
for feature in @features[layer]
|
|
|
|
for points in feature.points
|
2016-09-18 04:20:35 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
visible = false
|
|
|
|
points = for point in points
|
2016-09-20 15:21:24 +02:00
|
|
|
p = [point.x/scale, point.y/scale]
|
2016-09-20 20:52:49 +02:00
|
|
|
if not visible and @_isOnScreen p
|
|
|
|
visible = true
|
2016-09-18 17:46:14 +02:00
|
|
|
p
|
|
|
|
continue unless visible
|
2016-09-18 06:32:26 +02:00
|
|
|
|
2016-09-20 20:52:49 +02:00
|
|
|
wasDrawn = false
|
2016-09-18 17:46:14 +02:00
|
|
|
switch feature.type
|
|
|
|
when "polygon", "line"
|
|
|
|
@canvas.beginPath()
|
|
|
|
@canvas.moveTo points.shift()...
|
|
|
|
@canvas.lineTo point... for point in points
|
|
|
|
@canvas.stroke()
|
2016-09-20 20:52:49 +02:00
|
|
|
wasDrawn = true
|
2016-09-18 06:32:26 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
when "point"
|
2016-09-18 21:15:21 +02:00
|
|
|
text = feature.properties.house_num or @config.icons[feature.properties.maki] or "◉"
|
2016-09-20 20:52:49 +02:00
|
|
|
|
|
|
|
for point in points
|
|
|
|
if labelBuffer.writeIfPossible text, point...
|
|
|
|
@canvas.fillText text, point...
|
|
|
|
wasDrawn = true
|
|
|
|
|
|
|
|
if wasDrawn
|
|
|
|
drawn.push feature
|
2016-09-18 06:32:26 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
@canvas.restore()
|
2016-09-18 06:32:26 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
@_write @canvas._canvas.frame()
|
|
|
|
@_write @_getFooter()
|
2016-09-18 06:32:26 +02:00
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
@isDrawing = false
|
|
|
|
|
2016-09-20 20:52:49 +02:00
|
|
|
_isOnScreen: (point) ->
|
|
|
|
point[0]+@view[0]>=4 and
|
|
|
|
point[0]+@view[0]<@width-4 and
|
|
|
|
point[1]+@view[1]>=0 and
|
|
|
|
point[1]+@view[1]<@height
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
_write: (text) ->
|
|
|
|
process.stdout.write text
|
|
|
|
|
2016-09-20 15:21:24 +02:00
|
|
|
_getBBox: ->
|
|
|
|
[x, y] = mercator.forward [@center.lng, @center.lat]
|
|
|
|
width = @width * Math.pow(2, @zoom)
|
|
|
|
height = @height * Math.pow(2, @zoom)
|
|
|
|
|
|
|
|
mercator.inverse([x - width/2, y + width/2]).concat mercator.inverse([x + width/2, y - width/2])
|
|
|
|
|
2016-09-18 17:46:14 +02:00
|
|
|
_getFooter: ->
|
2016-09-20 21:46:31 +02:00
|
|
|
"center: [#{utils.digits @center.lat, 2}, #{utils.digits @center.lng, 2}] zoom: #{@zoom}"
|
|
|
|
# bbox: [#{@_getBBox().map((z) -> utils.digits(z, 2)).join(',')}]"
|
2016-09-18 17:46:14 +02:00
|
|
|
|
2016-09-18 21:50:09 +02:00
|
|
|
notify: (text) ->
|
2016-09-18 17:46:14 +02:00
|
|
|
return if @isDrawing
|
|
|
|
@_write "\r\x1B[K#{@_getFooter()} #{text}"
|
|
|
|
|
|
|
|
zoomBy: (step) ->
|
|
|
|
return unless @scale+step > 0
|
|
|
|
|
|
|
|
before = @scale
|
|
|
|
@scale += step
|
2016-09-20 15:21:24 +02:00
|
|
|
@zoom += step
|
2016-09-18 17:46:14 +02:00
|
|
|
|
|
|
|
@view[0] = @view[0]*before/@scale + if step > 0 then 8 else -8
|
|
|
|
@view[1] = @view[1]*before/@scale + if step > 0 then 8 else -8
|
|
|
|
|
|
|
|
termap = new Termap()
|
|
|
|
|
|
|
|
# TODO: abstracing this class, create loader class
|
2016-09-20 22:36:22 +02:00
|
|
|
data = fs.readFileSync __dirname+"/tiles/regensburg.pbf.gz"
|
2016-09-18 17:46:14 +02:00
|
|
|
termap.features = termap._getFeatures termap._parseTile data
|
|
|
|
termap._draw()
|