📡 implementing HTTP source handling for TileSource

This commit is contained in:
Michael Straßburger 2016-11-03 02:14:47 +01:00
parent 3895fd1fd3
commit d460421eea
5 changed files with 53 additions and 28 deletions

View File

@ -1,12 +1,16 @@
{ {
"name": "termap", "name": "mapscii",
"version": "0.1.0", "version": "0.1.0",
"description": "Terminal Map Viewer", "description": "Terminal Map Viewer",
"main": "termap.coffee", "main": "main.js",
"scripts": { "scripts": {
"start": "node main", "start": "node main",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": {
"type": "git",
"url": "https://github.com/rastapasta/mapscii.git"
},
"keywords": [ "keywords": [
"map", "map",
"console", "console",
@ -14,7 +18,6 @@
"ascii", "ascii",
"osm", "osm",
"vectortile", "vectortile",
"render",
"mbtiles" "mbtiles"
], ],
"author": "Michael Straßburger <codepoet@cpan.org>", "author": "Michael Straßburger <codepoet@cpan.org>",
@ -29,6 +32,8 @@
"mbtiles": "^0.9.0", "mbtiles": "^0.9.0",
"pbf": "^3.0.0", "pbf": "^3.0.0",
"rbush": "^2.0.1", "rbush": "^2.0.1",
"request": "^2.76.0",
"request-promise": "^4.1.1",
"sphericalmercator": "^1.0.5", "sphericalmercator": "^1.0.5",
"term-mouse": "^0.1.1", "term-mouse": "^0.1.1",
"vector-tile": "^1.3.0", "vector-tile": "^1.3.0",

View File

@ -178,22 +178,6 @@ module.exports = class Renderer
featuresAt: (x, y) -> featuresAt: (x, y) ->
@labelBuffer.featuresAt x, y @labelBuffer.featuresAt x, y
_getBBox: (center, zoom) ->
[x, y] = utils.ll2xy center.lon, center.lat
meterPerPixel = utils.metersPerPixel zoom, center.lat
width = @width * meterPerPixel
height = @height * meterPerPixel
west = x - width*.5
east = x + width*.5
south = y + height*.5
north = y - height*.5
box = mercator
.inverse([west+1, south])
.concat mercator.inverse([east-1, north])
_tilesInBBox: (bbox, zoom) -> _tilesInBBox: (bbox, zoom) ->
tiles = {} tiles = {}
[tiles.minX, tiles.minY] = utils.ll2tile bbox[0], bbox[1], Math.floor zoom [tiles.minX, tiles.minY] = utils.ll2tile bbox[0], bbox[1], Math.floor zoom

View File

@ -8,7 +8,6 @@
keypress = require 'keypress' keypress = require 'keypress'
TermMouse = require 'term-mouse' TermMouse = require 'term-mouse'
Promise = require 'bluebird' Promise = require 'bluebird'
mercator = new (require('sphericalmercator'))()
Renderer = require './Renderer' Renderer = require './Renderer'
TileSource = require './TileSource' TileSource = require './TileSource'
@ -19,7 +18,8 @@ module.exports = class Termap
input: process.stdin input: process.stdin
output: process.stdout output: process.stdout
source: __dirname+"/../mbtiles/regensburg.mbtiles" source: "http://nachbar.io/data/osm2vectortiles/"
#source: __dirname+"/../mbtiles/regensburg.mbtiles"
styleFile: __dirname+"/../styles/bright.json" styleFile: __dirname+"/../styles/bright.json"
zoomStep: 0.2 zoomStep: 0.2

View File

@ -10,6 +10,9 @@
Promise = require 'bluebird' Promise = require 'bluebird'
MBTiles = require 'mbtiles' MBTiles = require 'mbtiles'
request = require 'request'
rp = require 'request-promise'
Tile = require './Tile' Tile = require './Tile'
module.exports = class TileSource module.exports = class TileSource
@ -17,15 +20,20 @@ module.exports = class TileSource
modes: modes:
MBTiles: 1 MBTiles: 1
VectorTile: 2 VectorTile: 2
HTTP: 3
mode: null mode: null
mbtiles: null mbtiles: null
init: (source) -> init: (@source) ->
if source.endsWith ".mbtiles" if @source.startsWith "http"
@mode = @modes.HTTP
else if @source.endsWith ".mbtiles"
@mode = @modes.MBTiles @mode = @modes.MBTiles
@loadMBtils source @loadMBtils source
else else
throw new Error "source type isn't supported yet" throw new Error "source type isn't supported yet"
@ -44,13 +52,23 @@ module.exports = class TileSource
if cached = @cache[[z,x,y].join("-")] if cached = @cache[[z,x,y].join("-")]
return Promise.resolve cached return Promise.resolve cached
if @mode is @modes.MBTiles switch @mode
@_getMBTile z, x, y when @modes.MBTiles then @_getMBTile z, x, y
when @modes.HTTP then @_getHTTP z, x, y
_getHTTP: (z, x, y) ->
rp
uri: @source+[z,x,y].join("/")+".pbf"
encoding: null
.then (buffer) =>
@_createTile z, x, y, buffer
_getMBTile: (z, x, y) -> _getMBTile: (z, x, y) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
@mbtiles.getTile z, x, y, (err, tileData) => @mbtiles.getTile z, x, y, (err, buffer) =>
return reject err if err return reject err if err
resolve @_createTile z, x, y, buffer
_createTile: (z, x, y, buffer) ->
tile = @cache[[z,x,y].join("-")] = new Tile() tile = @cache[[z,x,y].join("-")] = new Tile()
resolve tile.load tileData tile.load buffer

View File

@ -4,6 +4,7 @@
methods used all around methods used all around
### ###
mercator = new (require('sphericalmercator'))()
constants = constants =
RADIUS: 6378137 RADIUS: 6378137
@ -42,6 +43,22 @@ utils =
lon: x/Math.pow(2, zoom)*360-180 lon: x/Math.pow(2, zoom)*360-180
lat: 180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))) lat: 180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n)))
geoBBox: (center, zoom, width, height) ->
[x, y] = utils.ll2xy center.lon, center.lat
meterPerPixel = utils.metersPerPixel zoom, center.lat
width *= meterPerPixel
height *= meterPerPixel
west = x - width*.5
east = x + width*.5
south = y + height*.5
north = y - height*.5
box = mercator
.inverse([west+1, south])
.concat mercator.inverse([east-1, north])
metersPerPixel: (zoom, lat = 0) -> metersPerPixel: (zoom, lat = 0) ->
(Math.cos(lat * Math.PI/180) * 2 * Math.PI * constants.RADIUS) / (256 * Math.pow(2, zoom)) (Math.cos(lat * Math.PI/180) * 2 * Math.PI * constants.RADIUS) / (256 * Math.pow(2, zoom))
@ -70,4 +87,5 @@ utils =
digits: (number, digits) -> digits: (number, digits) ->
Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits) Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)
module.exports = utils module.exports = utils