From 6033f0be3c814d1e2fdd636127e0f5067fe464af Mon Sep 17 00:00:00 2001 From: Christian Paul Date: Sat, 4 Nov 2017 17:11:53 -0700 Subject: [PATCH] Decaffeinate utils --- src/utils.coffee | 74 ---------------------------------- src/utils.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 74 deletions(-) delete mode 100644 src/utils.coffee create mode 100644 src/utils.js diff --git a/src/utils.coffee b/src/utils.coffee deleted file mode 100644 index 9e05419..0000000 --- a/src/utils.coffee +++ /dev/null @@ -1,74 +0,0 @@ -### - termap - Terminal Map Viewer - by Michael Strassburger - - methods used all around -### -config = require './config' - -constants = - RADIUS: 6378137 - -utils = - clamp: (num, min, max) -> - if num <= min then min else if num >= max then max else num - - baseZoom: (zoom) -> - Math.min config.tileRange, Math.max 0, Math.floor zoom - - tilesizeAtZoom: (zoom) -> - config.projectSize * Math.pow(2, zoom-utils.baseZoom(zoom)) - - deg2rad: (angle) -> - # (angle / 180) * Math.PI - angle * 0.017453292519943295 - - ll2tile: (lon, lat, zoom) -> - x: (lon+180)/360*Math.pow(2, zoom) - y: (1-Math.log(Math.tan(lat*Math.PI/180)+1/Math.cos(lat*Math.PI/180))/Math.PI)/2*Math.pow(2, zoom) - z: zoom - - tile2ll: (x, y, zoom) -> - n = Math.PI - 2*Math.PI*y/Math.pow(2, zoom) - - lon: x/Math.pow(2, zoom)*360-180 - lat: 180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))) - - metersPerPixel: (zoom, lat = 0) -> - (Math.cos(lat * Math.PI/180) * 2 * Math.PI * constants.RADIUS) / (256 * Math.pow(2, zoom)) - - hex2rgb: (color) -> - 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) - - normalize: (ll) -> - ll.lon += 360 if ll.lon < -180 - ll.lon -= 360 if ll.lon > 180 - - ll.lat = 85.0511 if ll.lat > 85.0511 - ll.lat = -85.0511 if ll.lat < -85.0511 - - ll - - population: (val) -> - bits = 0 - while val>0 - bits += val & 1 - val >>= 1 - bits - -module.exports = utils diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..914f504 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,103 @@ +/* + termap - Terminal Map Viewer + by Michael Strassburger + + methods used all around +*/ +'use strict'; +const config = require('./config'); + +const constants = { + RADIUS: 6378137, +}; + +const utils = { + clamp: (num, min, max) => { + if (num <= min) { + return min + } else if (num >= max) { + return max; + } else { + return num; + } + }, + + baseZoom: (zoom) => { + return Math.min(config.tileRange, Math.max(0, Math.floor(zoom))); + }, + + tilesizeAtZoom: (zoom) => { + return config.projectSize * Math.pow(2, zoom-utils.baseZoom(zoom)); + }, + + deg2rad: (angle) => { + // (angle / 180) * Math.PI + return angle * 0.017453292519943295; + }, + + ll2tile: (lon, lat, zoom) => { + return { + x: (lon+180)/360*Math.pow(2, zoom), + y: (1-Math.log(Math.tan(lat*Math.PI/180)+1/Math.cos(lat*Math.PI/180))/Math.PI)/2*Math.pow(2, zoom), + z: zoom, + }; + }, + + tile2ll: (x, y, zoom) => { + const n = Math.PI - 2*Math.PI*y/Math.pow(2, zoom); + + return { + lon: x/Math.pow(2, zoom)*360-180, + lat: 180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))), + }; + }, + + metersPerPixel: (zoom, lat = 0) => { + return (Math.cos(lat * Math.PI/180) * 2 * Math.PI * constants.RADIUS) / (256 * Math.pow(2, zoom)); + }, + + hex2rgb: (color) => { + if (typeof color !== 'string') return [255, 0, 0]; + + if (/^#[a-fA-F0-9]{3,6}$/.test()) { + throw new Error('#{color} isn\'t a supported hex color'); + } + + color = color.substr(1); + const decimal = parseInt(color, 16); + + if (color.length === 3) { + const rgb = [decimal>>8, (decimal>>4)&15, decimal&15]; + return rgb.map((c) => { + return c + (c<<4); + }); + } else { + return [(decimal>>16)&255, (decimal>>8)&255, decimal&255]; + } + }, + + digits: (number, digits) => { + return Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits); + }, + + normalize: (ll) => { + if (ll.lon < -180) ll.lon += 360; + if (ll.lon > 180) ll.lon -= 360; + + if (ll.lat > 85.0511) ll.lat = 85.0511; + if (ll.lat < -85.0511) ll.lat = -85.0511; + + return ll; + }, + + population: (val) => { + let bits = 0 + while (val > 0) { + bits += val & 1; + val >>= 1; + } + return bits; + }, +}; + +module.exports = utils;