Skip to content

Commit

Permalink
rename StringNumberMapping -> DictionaryCoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Mar 22, 2016
1 parent bea4d61 commit 8b52b55
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
10 changes: 5 additions & 5 deletions js/data/feature_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var EXTENT = require('./bucket').EXTENT;
var featureFilter = require('feature-filter');
var StructArrayType = require('../util/struct_array');
var Grid = require('../util/grid');
var StringNumberMapping = require('../util/string_number_mapping');
var DictionaryCoder = require('../util/dictionary_coder');
var vt = require('vector-tile');
var Protobuf = require('pbf');
var GeoJSONFeature = require('../util/vectortile_to_geojson');
Expand Down Expand Up @@ -107,7 +107,7 @@ function translateDistance(translate) {
FeatureTree.prototype.query = function(args, styleLayers, returnGeoJSON) {
if (!this.vtLayers) {
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers;
this.sourceLayerNumberMapping = new StringNumberMapping(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']);
this.sourceLayerCoder = new DictionaryCoder(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']);
}

var result = returnGeoJSON ?
Expand Down Expand Up @@ -190,7 +190,7 @@ FeatureTree.prototype.filterMatching = function(result, matching, array, queryGe
var layerIDs = this.bucketLayerIDs[match.bucketIndex];
if (filterLayerIDs && !arraysIntersect(filterLayerIDs, layerIDs)) continue;

var sourceLayerName = this.sourceLayerNumberMapping.numberToString[match.sourceLayerIndex];
var sourceLayerName = this.sourceLayerCoder.decode(match.sourceLayerIndex);
var sourceLayer = this.vtLayers[sourceLayerName];
var feature = sourceLayer.feature(match.featureIndex);

Expand Down Expand Up @@ -262,7 +262,7 @@ FeatureTree.prototype.filterMatching = function(result, matching, array, queryGe
FeatureTree.prototype.makeGeoJSON = function(featureIndexArray, styleLayers) {
if (!this.vtLayers) {
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers;
this.sourceLayerNumberMapping = new StringNumberMapping(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']);
this.sourceLayerCoder = new DictionaryCoder(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']);
}

var result = {};
Expand All @@ -272,7 +272,7 @@ FeatureTree.prototype.makeGeoJSON = function(featureIndexArray, styleLayers) {
var cachedLayerFeatures = {};
for (var i = 0; i < featureIndexArray.length; i++) {
var indexes = featureIndexArray.get(i);
var sourceLayerName = this.sourceLayerNumberMapping.numberToString[indexes.sourceLayerIndex];
var sourceLayerName = this.sourceLayerCoder.decode(indexes.sourceLayerIndex);
var sourceLayer = this.vtLayers[sourceLayerName];
var featureIndex = indexes.featureIndex;

Expand Down
6 changes: 3 additions & 3 deletions js/source/worker_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var FeatureTree = require('../data/feature_tree');
var CollisionTile = require('../symbol/collision_tile');
var Bucket = require('../data/bucket');
var CollisionBoxArray = require('../symbol/collision_box');
var StringNumberMapping = require('../util/string_number_mapping');
var DictionaryCoder = require('../util/dictionary_coder');

module.exports = WorkerTile;

Expand All @@ -28,7 +28,7 @@ WorkerTile.prototype.parse = function(data, layers, actor, rawTileData, callback
this.collisionBoxArray = new CollisionBoxArray();
var collisionTile = new CollisionTile(this.angle, this.pitch, this.collisionBoxArray);
var featureTree = new FeatureTree(this.coord, this.overscaling, collisionTile, data.layers);
var sourceLayerNumberMapping = new StringNumberMapping(data.layers ? Object.keys(data.layers).sort() : ['_geojsonTileLayer']);
var sourceLayerCoder = new DictionaryCoder(data.layers ? Object.keys(data.layers).sort() : ['_geojsonTileLayer']);

var stats = { _total: 0 };

Expand Down Expand Up @@ -57,7 +57,7 @@ WorkerTile.prototype.parse = function(data, layers, actor, rawTileData, callback
overscaling: this.overscaling,
collisionDebug: this.collisionDebug,
collisionBoxArray: this.collisionBoxArray,
sourceLayerIndex: sourceLayerNumberMapping.stringToNumber[layer['source-layer'] || '_geojsonTileLayer']
sourceLayerIndex: sourceLayerCoder.encode(layer['source-layer'] || '_geojsonTileLayer')
});
bucket.createFilter();

Expand Down
21 changes: 21 additions & 0 deletions js/util/dictionary_coder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

module.exports = DictionaryCoder;

function DictionaryCoder(strings) {
this._stringToNumber = {};
this._numberToString = [];
for (var i = 0; i < strings.length; i++) {
var string = strings[i];
this._stringToNumber[string] = i;
this._numberToString[i] = string;
}
}

DictionaryCoder.prototype.encode = function(string) {
return this._stringToNumber[string];
};

DictionaryCoder.prototype.decode = function(n) {
return this._numberToString[n];
};
13 changes: 0 additions & 13 deletions js/util/string_number_mapping.js

This file was deleted.

0 comments on commit 8b52b55

Please sign in to comment.