mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-03-19 08:18:13 +01:00
📡 implementing HTTP source handling for TileSource
This commit is contained in:
parent
3895fd1fd3
commit
d460421eea
11
package.json
11
package.json
@ -1,12 +1,16 @@
|
||||
{
|
||||
"name": "termap",
|
||||
"name": "mapscii",
|
||||
"version": "0.1.0",
|
||||
"description": "Terminal Map Viewer",
|
||||
"main": "termap.coffee",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"start": "node main",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rastapasta/mapscii.git"
|
||||
},
|
||||
"keywords": [
|
||||
"map",
|
||||
"console",
|
||||
@ -14,7 +18,6 @@
|
||||
"ascii",
|
||||
"osm",
|
||||
"vectortile",
|
||||
"render",
|
||||
"mbtiles"
|
||||
],
|
||||
"author": "Michael Straßburger <codepoet@cpan.org>",
|
||||
@ -29,6 +32,8 @@
|
||||
"mbtiles": "^0.9.0",
|
||||
"pbf": "^3.0.0",
|
||||
"rbush": "^2.0.1",
|
||||
"request": "^2.76.0",
|
||||
"request-promise": "^4.1.1",
|
||||
"sphericalmercator": "^1.0.5",
|
||||
"term-mouse": "^0.1.1",
|
||||
"vector-tile": "^1.3.0",
|
||||
|
@ -178,22 +178,6 @@ module.exports = class Renderer
|
||||
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) ->
|
||||
tiles = {}
|
||||
[tiles.minX, tiles.minY] = utils.ll2tile bbox[0], bbox[1], Math.floor zoom
|
||||
|
@ -8,7 +8,6 @@
|
||||
keypress = require 'keypress'
|
||||
TermMouse = require 'term-mouse'
|
||||
Promise = require 'bluebird'
|
||||
mercator = new (require('sphericalmercator'))()
|
||||
|
||||
Renderer = require './Renderer'
|
||||
TileSource = require './TileSource'
|
||||
@ -19,7 +18,8 @@ module.exports = class Termap
|
||||
input: process.stdin
|
||||
output: process.stdout
|
||||
|
||||
source: __dirname+"/../mbtiles/regensburg.mbtiles"
|
||||
source: "http://nachbar.io/data/osm2vectortiles/"
|
||||
#source: __dirname+"/../mbtiles/regensburg.mbtiles"
|
||||
styleFile: __dirname+"/../styles/bright.json"
|
||||
zoomStep: 0.2
|
||||
|
||||
|
@ -10,6 +10,9 @@
|
||||
Promise = require 'bluebird'
|
||||
MBTiles = require 'mbtiles'
|
||||
|
||||
request = require 'request'
|
||||
rp = require 'request-promise'
|
||||
|
||||
Tile = require './Tile'
|
||||
|
||||
module.exports = class TileSource
|
||||
@ -17,15 +20,20 @@ module.exports = class TileSource
|
||||
modes:
|
||||
MBTiles: 1
|
||||
VectorTile: 2
|
||||
HTTP: 3
|
||||
|
||||
mode: null
|
||||
|
||||
mbtiles: null
|
||||
|
||||
init: (source) ->
|
||||
if source.endsWith ".mbtiles"
|
||||
init: (@source) ->
|
||||
if @source.startsWith "http"
|
||||
@mode = @modes.HTTP
|
||||
|
||||
else if @source.endsWith ".mbtiles"
|
||||
@mode = @modes.MBTiles
|
||||
@loadMBtils source
|
||||
|
||||
else
|
||||
throw new Error "source type isn't supported yet"
|
||||
|
||||
@ -44,13 +52,23 @@ module.exports = class TileSource
|
||||
if cached = @cache[[z,x,y].join("-")]
|
||||
return Promise.resolve cached
|
||||
|
||||
if @mode is @modes.MBTiles
|
||||
@_getMBTile z, x, y
|
||||
switch @mode
|
||||
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) ->
|
||||
new Promise (resolve, reject) =>
|
||||
@mbtiles.getTile z, x, y, (err, tileData) =>
|
||||
@mbtiles.getTile z, x, y, (err, buffer) =>
|
||||
return reject err if err
|
||||
resolve @_createTile z, x, y, buffer
|
||||
|
||||
_createTile: (z, x, y, buffer) ->
|
||||
tile = @cache[[z,x,y].join("-")] = new Tile()
|
||||
resolve tile.load tileData
|
||||
tile.load buffer
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
methods used all around
|
||||
###
|
||||
mercator = new (require('sphericalmercator'))()
|
||||
|
||||
constants =
|
||||
RADIUS: 6378137
|
||||
@ -42,6 +43,22 @@ utils =
|
||||
lon: x/Math.pow(2, zoom)*360-180
|
||||
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) ->
|
||||
(Math.cos(lat * Math.PI/180) * 2 * Math.PI * constants.RADIUS) / (256 * Math.pow(2, zoom))
|
||||
|
||||
@ -70,4 +87,5 @@ utils =
|
||||
digits: (number, digits) ->
|
||||
Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)
|
||||
|
||||
|
||||
module.exports = utils
|
||||
|
Loading…
Reference in New Issue
Block a user