Decaffeinate remaining methods of the Tile class

This commit is contained in:
Christian Paul 2017-12-23 01:24:23 -08:00
parent 0353effa67
commit bfd5c54fa4
2 changed files with 17 additions and 174 deletions

View File

@ -1,153 +0,0 @@
###
termap - Terminal Map Viewer
by Michael Strassburger <codepoet@cpan.org>
Handling of and access to single VectorTiles
###
VectorTile = require('@mapbox/vector-tile').VectorTile
Protobuf = require 'pbf'
Promise = require 'bluebird'
zlib = require 'zlib'
rbush = require 'rbush'
x256 = require 'x256'
earcut = require 'earcut'
config = require "./config"
utils = require "./utils"
class Tile
layers: {}
constructor: (@styler) ->
load: (buffer) ->
@_unzipIfNeeded buffer
.then (buffer) => @_loadTile buffer
.then => @_loadLayers()
.then => this
_loadTile: (buffer) ->
@tile = new VectorTile new Protobuf buffer
_unzipIfNeeded: (buffer) ->
new Promise (resolve, reject) =>
if @_isGzipped buffer
zlib.gunzip buffer, (err, data) ->
return reject err if err
resolve data
else
resolve buffer
_isGzipped: (buffer) ->
buffer.slice(0,2).indexOf(Buffer.from([0x1f, 0x8b])) is 0
_loadLayers: () ->
layers = {}
colorCache = {}
for name, layer of @tile.layers
nodes = []
#continue if name is "water"
for i in [0...layer.length]
# TODO: caching of similar attributes to avoid looking up the style each time
#continue if @styler and not @styler.getStyleFor layer, feature
feature = layer.feature i
feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type]
if @styler
style = @styler.getStyleFor name, feature
continue unless style
color =
style.paint['line-color'] or
style.paint['fill-color'] or
style.paint['text-color']
# TODO: style zoom stops handling
if color instanceof Object
color = color.stops[0][1]
colorCode = colorCache[color] or colorCache[color] = x256 utils.hex2rgb color
# TODO: monkey patching test case for tiles with a reduced extent 4096 / 8 -> 512
# use feature.loadGeometry() again as soon as we got a 512 extent tileset
geometries = feature.loadGeometry() #@_reduceGeometry feature, 8
sort = feature.properties.localrank or feature.properties.scalerank
label = if style.type is "symbol"
feature.properties["name_"+config.language] or
feature.properties.name_en or
feature.properties.name or
feature.properties.house_num
else
undefined
if style.type is "fill"
nodes.push @_addBoundaries true,
# id: feature.id
layer: name
style: style
label: label
sort: sort
points: geometries
color: colorCode
else
for points in geometries
nodes.push @_addBoundaries false,
# id: feature.id
layer: name
style: style
label: label
sort: sort
points: points
color: colorCode
tree = rbush 18
tree.load nodes
layers[name] =
extent: layer.extent
tree: tree
@layers = layers
_addBoundaries: (deep, data) ->
minX = Infinity
maxX = -Infinity
minY = Infinity
maxY = -Infinity
for p in (if deep then data.points[0] else data.points)
minX = p.x if p.x < minX
maxX = p.x if p.x > maxX
minY = p.y if p.y < minY
maxY = p.y if p.y > maxY
data.minX = minX
data.maxX = maxX
data.minY = minY
data.maxY = maxY
data
_reduceGeometry: (feature, factor) ->
for points, i in feature.loadGeometry()
reduced = []
last = null
for point in points
p =
x: Math.floor point.x/factor
y: Math.floor point.y/factor
if last and last.x is p.x and last.y is p.y
continue
reduced.push last = p
reduced
module.exports = Tile

View File

@ -53,12 +53,11 @@ class Tile {
} }
_loadLayers() { _loadLayers() {
var color, colorCache, colorCode, k, layers, len, nodes, points, style, tree; const layers = {};
layers = {}; const colorCache = {};
colorCache = {};
for (const name in this.tile.layers) { for (const name in this.tile.layers) {
const layer = this.tile.layers[name]; const layer = this.tile.layers[name];
nodes = []; const nodes = [];
//continue if name is 'water' //continue if name is 'water'
for (let i = 0; i < layer.length; i++) { for (let i = 0; i < layer.length; i++) {
// TODO: caching of similar attributes to avoid looking up the style each time // TODO: caching of similar attributes to avoid looking up the style each time
@ -66,13 +65,14 @@ class Tile {
const feature = layer.feature(i); const feature = layer.feature(i);
feature.properties.$type = [undefined, 'Point', 'LineString', 'Polygon'][feature.type]; feature.properties.$type = [undefined, 'Point', 'LineString', 'Polygon'][feature.type];
let style;
if (this.styler) { if (this.styler) {
style = this.styler.getStyleFor(name, feature); style = this.styler.getStyleFor(name, feature);
if (!style) { if (!style) {
continue; continue;
} }
} }
color = ( let color = (
style.paint['line-color'] || style.paint['line-color'] ||
style.paint['fill-color'] || style.paint['fill-color'] ||
style.paint['text-color'] style.paint['text-color']
@ -81,7 +81,7 @@ class Tile {
if (color instanceof Object) { if (color instanceof Object) {
color = color.stops[0][1]; color = color.stops[0][1];
} }
colorCode = colorCache[color] || (colorCache[color] = x256(utils.hex2rgb(color))); const colorCode = colorCache[color] || (colorCache[color] = x256(utils.hex2rgb(color)));
// TODO: monkey patching test case for tiles with a reduced extent 4096 / 8 -> 512 // TODO: monkey patching test case for tiles with a reduced extent 4096 / 8 -> 512
// use feature.loadGeometry() again as soon as we got a 512 extent tileset // use feature.loadGeometry() again as soon as we got a 512 extent tileset
const geometries = feature.loadGeometry(); //@_reduceGeometry feature, 8 const geometries = feature.loadGeometry(); //@_reduceGeometry feature, 8
@ -98,10 +98,9 @@ class Tile {
color: colorCode, color: colorCode,
})); }));
} else { } else {
for (k = 0, len = geometries.length; k < len; k++) { for (const points of geometries) {
points = geometries[k];
nodes.push(this._addBoundaries(false, { nodes.push(this._addBoundaries(false, {
// id: feature.id //id: feature.id,
layer: name, layer: name,
style, style,
label, label,
@ -112,11 +111,11 @@ class Tile {
} }
} }
} }
tree = rbush(18); const tree = rbush(18);
tree.load(nodes); tree.load(nodes);
layers[name] = { layers[name] = {
extent: layer.extent, extent: layer.extent,
tree: tree, tree,
}; };
} }
return this.layers = layers; return this.layers = layers;
@ -142,16 +141,13 @@ class Tile {
} }
_reduceGeometry(feature, factor) { _reduceGeometry(feature, factor) {
var i, j, k, last, len, len1, p, point, points, reduced, ref, results; const results = [];
ref = feature.loadGeometry(); const geometries = feature.loadGeometry();
results = []; for (const points of geometries) {
for (i = j = 0, len = ref.length; j < len; i = ++j) { const reduced = [];
points = ref[i]; let last;
reduced = []; for (const point of points) {
last = null; const p = {
for (k = 0, len1 = points.length; k < len1; k++) {
point = points[k];
p = {
x: Math.floor(point.x / factor), x: Math.floor(point.x / factor),
y: Math.floor(point.y / factor) y: Math.floor(point.y / factor)
}; };