diff options
author | Michael Straßburger <codepoet@cpan.org> | 2016-11-06 15:34:36 +0100 |
---|---|---|
committer | Michael Straßburger <codepoet@cpan.org> | 2016-11-06 15:34:36 +0100 |
commit | 8aa29a9e28bc1038b1e8df864ba21fffef2b5343 (patch) | |
tree | 758e2ed118987c42e56ef7d8c3206c687a42782f | |
parent | 59b64e8dd2ccb4da2c3926891f8824dd43168d42 (diff) |
:runner: applying styles on tile load, filtering unstyled features, caching style
-rw-r--r-- | src/Renderer.coffee | 24 | ||||
-rw-r--r-- | src/Styler.coffee | 3 | ||||
-rw-r--r-- | src/Termap.coffee | 3 | ||||
-rw-r--r-- | src/Tile.coffee | 57 | ||||
-rw-r--r-- | src/TileSource.coffee | 6 |
5 files changed, 51 insertions, 42 deletions
diff --git a/src/Renderer.coffee b/src/Renderer.coffee index 5dda246..099d0eb 100644 --- a/src/Renderer.coffee +++ b/src/Renderer.coffee @@ -87,6 +87,7 @@ module.exports = class Renderer loadStyleFile: (file) -> @styler = new Styler file + @tileSource.useStyler @styler setSize: (@width, @height) -> @canvas = new Canvas @width, @height @@ -173,7 +174,7 @@ module.exports = class Renderer features = {} for layer in @config.drawOrder continue unless tile.data.layers?[layer] - features[layer] = tile.data.layers[layer].tree.search box + features[layer] = tile.data.layers[layer].search box tile.features = features tile @@ -182,8 +183,6 @@ module.exports = class Renderer drawn = {} for layer in @config.drawOrder - short = layer.split(":")[0] - for tile in tiles continue unless tile.features[layer]?.length @@ -191,10 +190,10 @@ module.exports = class Renderer @canvas.translate tile.position.x, tile.position.y for feature in tile.features[layer] - continue if feature.data.id and drawn[feature.data.id] - drawn[feature.data.id] = true + continue if feature.id and drawn[feature.id] + drawn[feature.id] = true - @_drawFeature short, feature, tile.scale, tile.zoom + @_drawFeature layer, feature, tile.scale, tile.zoom @canvas.restore() @@ -210,17 +209,12 @@ module.exports = class Renderer _scaleAtZoom: (zoom) -> baseZoom = Math.min @config.maxZoom, Math.floor Math.max 0, zoom - (@config.tileSize/@config.projectSize)/Math.pow(2, zoom-baseZoom) - - _drawFeature: (layer, data, scale, zoom) -> - feature = data.data + @config.tileSize / @config.projectSize / Math.pow(2, zoom-baseZoom) - # TODO: this is ugly :) need to be fixed @style - #return false if feature.properties.class is "ferry" - feature.type = "LineString" if layer is "building" or layer is "road" + _drawFeature: (layer, feature, scale, zoom) -> + style = feature.style - unless style = @styler.getStyleFor layer, feature, zoom - return false + return false if style.minzoom and zoom < style.minzoom toDraw = (@_scaleAndReduce points, scale for points in feature.points) diff --git a/src/Styler.coffee b/src/Styler.coffee index 83ae897..4b84434 100644 --- a/src/Styler.coffee +++ b/src/Styler.coffee @@ -32,11 +32,10 @@ module.exports = class Styler @styleById[style.id] = style getStyleFor: (layer, feature, zoom) -> - # Skip all layers that don't have any styles set return false unless @styleByLayer[layer] for style in @styleByLayer[layer] - if style.appliesTo(feature) and ((not style.minzoom) or style.minzoom <= zoom) + if style.appliesTo feature return style return false diff --git a/src/Termap.coffee b/src/Termap.coffee index 8a5ff07..6465f75 100644 --- a/src/Termap.coffee +++ b/src/Termap.coffee @@ -172,6 +172,7 @@ module.exports = class Termap # display debug info for unhandled keys @notify JSON.stringify key + drawn: 0 _draw: -> @renderer .draw @center, @zoom @@ -180,6 +181,8 @@ module.exports = class Termap @notify @_getFooter() .catch => @notify "renderer is busy" + # .then => + # @_draw() if @drawn++ < 20 _getFooter: -> # features = @renderer.featuresAt @mousePosition.x-1-(@view[0]>>1), @mousePosition.y-1-(@view[1]>>2) diff --git a/src/Tile.coffee b/src/Tile.coffee index 9ec6894..5824b70 100644 --- a/src/Tile.coffee +++ b/src/Tile.coffee @@ -14,9 +14,11 @@ rbush = require 'rbush' class Tile layers: {} + constructor: (@styler) -> + load: (buffer) -> @_unzipIfNeeded buffer - .then (data) => @_loadTile data + .then (buffer) => @_loadTile buffer .then (tile) => @_loadLayers tile .then => this @@ -45,26 +47,19 @@ class Tile feature = layer.feature i - type = feature.properties.$type = + feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type] + if @styler + style = @styler.getStyleFor name, feature + continue unless style + # TODO: monkey patching test case for tiles with a reduced extent - geo = for sub, i in feature.loadGeometry() - points = [] - last = null - for point, j in sub - p = - x: Math.floor point.x/8 - y: Math.floor point.y/8 - - if last and last.x is p.x and last.y is p.y - continue - points.push p - last = p - points + points = @_reduceGeometry feature, 8 data = - points: geo + style: style + points: points properties: feature.properties id: feature.id type: type @@ -72,7 +67,7 @@ class Tile @_addToTree tree, data data - layers[name] = tree: tree, features: features + layers[name] = tree @layers = layers @@ -85,11 +80,27 @@ class Tile minY = p.y if p.y < minY maxY = p.y if p.y > maxY - tree.insert - minX: minX - maxX: maxX - minY: minY - maxY: maxY - data: data + data.minX = minX + data.maxX = maxX + data.minY = minY + data.maxY = maxY + + tree.insert data + + _reduceGeometry: (feature, factor) -> + for points, i in feature.loadGeometry() + reduced = [] + last = null + for point, j 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 diff --git a/src/TileSource.coffee b/src/TileSource.coffee index 336f533..2d06df4 100644 --- a/src/TileSource.coffee +++ b/src/TileSource.coffee @@ -34,8 +34,8 @@ module.exports = class TileSource HTTP: 3 mode: null - mbtiles: null + styler: null init: (@source) -> if @source.startsWith "http" @@ -59,6 +59,8 @@ module.exports = class TileSource if err then reject err else resolve() + useStyler: (@styler) -> + getTile: (z, x, y) -> unless @mode throw new Error "no TileSource defined" @@ -95,7 +97,7 @@ module.exports = class TileSource 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 @styler tile.load buffer _initPersistence: -> |