mirror of
https://github.com/rastapasta/mapscii.git
synced 2024-11-21 15:43:08 +01:00
✨ refactoring into separate modules, extending Canvas
This commit is contained in:
parent
51f3078fac
commit
241f9f7ef6
46
scripts/cleanStyle.coffee
Normal file
46
scripts/cleanStyle.coffee
Normal file
@ -0,0 +1,46 @@
|
||||
###
|
||||
termap - Terminal Map Viewer
|
||||
by Michael Strassburger <codepoet@cpan.org>
|
||||
|
||||
just a tool to make development easier
|
||||
|
||||
Input: Mapbox Studio map style file
|
||||
Output: Reduced map style file (only supported attributes are kept)
|
||||
###
|
||||
fs = require 'fs'
|
||||
|
||||
cleanStyle = (file) ->
|
||||
json = JSON.parse fs.readFileSync(file).toString()
|
||||
|
||||
cleanedStyle =
|
||||
name: json.name
|
||||
layers: []
|
||||
|
||||
for layer in json.layers
|
||||
continue if layer.ref
|
||||
|
||||
cleanLayer =
|
||||
type: layer.type
|
||||
id: layer.id
|
||||
paint: {}
|
||||
'source-layer': layer['source-layer']
|
||||
|
||||
|
||||
for key in ['filter', 'minzoom']
|
||||
cleanLayer[key] = layer[key] if layer[key]
|
||||
|
||||
if layer.layout?['text-size']
|
||||
cleanLayer.layout = 'text-size': layer.layout?['text-size']
|
||||
|
||||
for key in ['fill-color', 'line-color', 'text-color', 'background-color']
|
||||
cleanLayer.paint[key] = layer.paint[key] if layer.paint?[key]
|
||||
|
||||
if Object.keys(cleanLayer.paint).length
|
||||
cleanedStyle.layers.push cleanLayer
|
||||
|
||||
JSON.stringify cleanedStyle, null, ' '
|
||||
|
||||
console.log unless process.argv[2]
|
||||
"usage: coffee cleanStyle.coffee <inputJSON>"
|
||||
else
|
||||
cleanStyle process.argv[2]
|
11
src/Canvas.coffee
Normal file
11
src/Canvas.coffee
Normal file
@ -0,0 +1,11 @@
|
||||
###
|
||||
termap - Terminal Map Viewer
|
||||
by Michael Strassburger <codepoet@cpan.org>
|
||||
|
||||
Extends drawille-canvas to add additional drawing functions.
|
||||
To be PRed up the tree at some point.
|
||||
###
|
||||
|
||||
BlessedCanvas = require 'drawille-canvas-blessed-contrib'
|
||||
|
||||
module.exports = class Canvas extends BlessedCanvas
|
@ -1,3 +1,11 @@
|
||||
###
|
||||
termap - Terminal Map Viewer
|
||||
by Michael Strassburger <codepoet@cpan.org>
|
||||
|
||||
Using 2D spatial indexing to avoid overlapping labels and markers
|
||||
Future: to detect collision on mouse interaction
|
||||
###
|
||||
|
||||
rbush = require 'rbush'
|
||||
|
||||
module.exports = class LabelBuffer
|
||||
|
@ -1,8 +1,15 @@
|
||||
fs = require 'fs'
|
||||
# 'text-field'
|
||||
###
|
||||
termap - Terminal Map Viewer
|
||||
by Michael Strassburger <codepoet@cpan.org>
|
||||
|
||||
# Verrrrry MVP implementation
|
||||
# TODO: should be optimized by compiling the json to method&cb based filters
|
||||
Minimalistic parser and compiler for Mapbox (Studio) Map Style files
|
||||
See: https://www.mapbox.com/mapbox-gl-style-spec/
|
||||
|
||||
Verrrrry MVP implementation
|
||||
TODO: should be optimized by compiling the json to method&cb based filters
|
||||
###
|
||||
|
||||
fs = require 'fs'
|
||||
|
||||
module.exports = class Styler
|
||||
styleById: {}
|
||||
@ -56,37 +63,3 @@ module.exports = class Styler
|
||||
for value in filter[2..]
|
||||
return false if feature.properties[field] is value
|
||||
true
|
||||
|
||||
###
|
||||
cleanStyle: (file) ->
|
||||
json = JSON.parse fs.readFileSync(file).toString()
|
||||
|
||||
cleanedStyle =
|
||||
name: json.name
|
||||
layers: []
|
||||
|
||||
for layer in json.layers
|
||||
continue if layer.ref
|
||||
|
||||
cleanLayer =
|
||||
type: layer.type
|
||||
id: layer.id
|
||||
paint: {}
|
||||
'source-layer': layer['source-layer']
|
||||
|
||||
|
||||
for key in ['filter', 'minzoom']
|
||||
cleanLayer[key] = layer[key] if layer[key]
|
||||
|
||||
if layer.layout?['text-size']
|
||||
cleanLayer.layout = 'text-size': layer.layout?['text-size']
|
||||
|
||||
# TODO: opacity
|
||||
for key in ['fill-color', 'line-color', 'text-color', 'background-color']
|
||||
cleanLayer.paint[key] = layer.paint[key] if layer.paint?[key]
|
||||
|
||||
if Object.keys(cleanLayer.paint).length
|
||||
cleanedStyle.layers.push cleanLayer
|
||||
|
||||
console.log JSON.stringify cleanedStyle, null, ' '
|
||||
###
|
||||
|
32
src/utils.coffee
Normal file
32
src/utils.coffee
Normal file
@ -0,0 +1,32 @@
|
||||
utils =
|
||||
deg2rad: (angle) ->
|
||||
# (angle / 180) * Math.PI
|
||||
angle * 0.017453292519943295
|
||||
rad2deg: (angle) ->
|
||||
angle / Math.PI * 180
|
||||
|
||||
hex2rgb: (color) ->
|
||||
if not color.match
|
||||
console.log color
|
||||
process.exit()
|
||||
return [255, 0, 0] unless color?.match
|
||||
|
||||
unless color.match /^#[a-fA-F0-9]{3,6}$/
|
||||
throw new Error "#{color} isn\'t a supported hex color"
|
||||
|
||||
color = color.substr 1
|
||||
decimal = parseInt color, 16
|
||||
|
||||
if color.length is 3
|
||||
rgb = [decimal>>8, (decimal>>4)&15, decimal&15]
|
||||
rgb.map (c) => c + (c<<4)
|
||||
else
|
||||
[(decimal>>16)&255, (decimal>>8)&255, decimal&255]
|
||||
|
||||
digits: (number, digits) ->
|
||||
Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)
|
||||
|
||||
metersPerPixel: (zoom, lat = 0) ->
|
||||
utils.rad2deg(40075017*Math.cos(utils.deg2rad(lat))/Math.pow(2, zoom+8))
|
||||
|
||||
module.exports = utils
|
@ -1,4 +1,8 @@
|
||||
Canvas = require '../drawille-canvas-blessed-contrib'
|
||||
###
|
||||
termap - Terminal Map Viewer
|
||||
by Michael Strassburger <codepoet@cpan.org>
|
||||
###
|
||||
|
||||
keypress = require 'keypress'
|
||||
TermMouse = require 'term-mouse'
|
||||
x256 = require 'x256'
|
||||
@ -10,69 +14,38 @@ zlib = require 'zlib'
|
||||
mercator = new (require('sphericalmercator'))()
|
||||
triangulator = new (require('pnltri')).Triangulator()
|
||||
|
||||
Canvas = require __dirname+'/src/Canvas'
|
||||
LabelBuffer = require __dirname+'/src/LabelBuffer'
|
||||
Styler = require __dirname+'/src/Styler'
|
||||
|
||||
utils =
|
||||
deg2rad: (angle) ->
|
||||
# (angle / 180) * Math.PI
|
||||
angle * 0.017453292519943295
|
||||
rad2deg: (angle) ->
|
||||
angle / Math.PI * 180
|
||||
|
||||
hex2rgb: (color) ->
|
||||
if not color.match
|
||||
console.log color
|
||||
process.exit()
|
||||
return [255, 0, 0] unless color?.match
|
||||
|
||||
unless color.match /^#[a-fA-F0-9]{3,6}$/
|
||||
throw new Error "#{color} isn\'t a supported hex color"
|
||||
|
||||
color = color.substr 1
|
||||
decimal = parseInt color, 16
|
||||
|
||||
if color.length is 3
|
||||
rgb = [decimal>>8, (decimal>>4)&15, decimal&15]
|
||||
rgb.map (c) => c + (c<<4)
|
||||
else
|
||||
[(decimal>>16)&255, (decimal>>8)&255, decimal&255]
|
||||
|
||||
digits: (number, digits) ->
|
||||
Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)
|
||||
|
||||
metersPerPixel: (zoom, lat = 0) ->
|
||||
utils.rad2deg(40075017*Math.cos(utils.deg2rad(lat))/Math.pow(2, zoom+8))
|
||||
|
||||
utils = require __dirname+'/src/utils'
|
||||
|
||||
class Termap
|
||||
config:
|
||||
styleFile: __dirname+"/styles/bright.json"
|
||||
|
||||
fillPolygons: true
|
||||
zoomStep: 0.5
|
||||
zoomStep: 0.4
|
||||
|
||||
# landuse "poi_label"
|
||||
drawOrder: ["admin", "water", "building", "road", "housenum_label"]
|
||||
drawOrder: ["admin", "water", "building", "road", "poi_label", "city_label", "housenum_label"]
|
||||
|
||||
icons:
|
||||
car: "🚗"
|
||||
school: "🏫"
|
||||
school: "S" #{}"🏫"
|
||||
marker: "⭐"
|
||||
'art-gallery': "🎨"
|
||||
'art-gallery': "A" #"🎨"
|
||||
attraction: "❕"
|
||||
stadium: "🏈"
|
||||
toilet: "🚽"
|
||||
cafe: "☕"
|
||||
laundry: "👚"
|
||||
bus: "🚌"
|
||||
restaurant: "🍛"
|
||||
lodging: "🛏."
|
||||
restaurant: "R" #🍛"
|
||||
lodging: "B" #🛏"
|
||||
'fire-station': "🚒"
|
||||
shop: "🛍"
|
||||
pharmacy: "💊"
|
||||
beer: "🍺"
|
||||
cinema: "🎦"
|
||||
beer: "H" #"🍺"
|
||||
cinema: "C" #"🎦"
|
||||
|
||||
layers:
|
||||
housenum_label:
|
||||
@ -195,9 +168,9 @@ class Termap
|
||||
|
||||
if draw
|
||||
@_draw()
|
||||
else
|
||||
# display debug info for unhandled keys
|
||||
@notify JSON.stringify key
|
||||
# else
|
||||
# # display debug info for unhandled keys
|
||||
# @notify JSON.stringify key
|
||||
|
||||
_parseTile: (buffer) ->
|
||||
# extract, decode and parse a given tile buffer
|
||||
@ -364,7 +337,7 @@ 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: #{@zoom}"
|
||||
"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(',')}]"
|
||||
|
||||
notify: (text) ->
|
||||
|
Loading…
Reference in New Issue
Block a user