forked from mapbox/mapbox-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script for showing properties in a vector tile
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env node | ||
var Protobuf = require('../js/protobuf.js'); | ||
var VectorTile = require('../js/vectortile.js'); | ||
|
||
|
||
// Script for displaying tags/frequency in a vector tile | ||
|
||
var fs = require("fs"); | ||
var zlib = require("zlib"); | ||
|
||
if (process.argv.length < 3) { | ||
console.warn('Usage: %s %s [file.vector.pbf]', process.argv[0], process.argv[1]); | ||
process.exit(1); | ||
} | ||
|
||
var data = fs.readFileSync(process.argv[2]); | ||
|
||
zlib.inflate(data, function(err, data) { | ||
if (err) throw err; | ||
|
||
var tile = new VectorTile(new Protobuf(new Uint8Array(data))); | ||
|
||
for (var layer_name in tile.layers) { | ||
var layer = tile.layers[layer_name]; | ||
|
||
console.warn("\x1B[32m\x1B[1m%s\x1B[22m\x1B[39m", layer_name); | ||
|
||
var tags = {}; | ||
|
||
for (var i = 0; i < layer.length; i++) { | ||
var feature = layer.feature(i); | ||
|
||
for (var key in feature) { | ||
if (feature.hasOwnProperty(key) && key[0] !== '_') { | ||
if (!(key in tags)) tags[key] = []; | ||
tags[key].push(feature[key]); | ||
} | ||
} | ||
} | ||
|
||
for (var key in tags) { | ||
var counts = {}; | ||
for (var i = 0; i < tags[key].length; i++) { | ||
var val = tags[key][i]; | ||
if (counts[val]) counts[val].count++; | ||
else counts[val] = { val: val, count: 1 }; | ||
} | ||
|
||
counts = Object.keys(counts).map(function(k) { return counts[k]; }); | ||
counts.sort(function(a, b) { | ||
return b.count - a.count; | ||
}); | ||
counts = counts.map(function(c) { | ||
return JSON.stringify(c.val) + ": " + c.count; | ||
}).join(", "); | ||
|
||
var width = process.stdout.columns - 4 - key.length - 2; | ||
if (counts.length > width) { | ||
counts = counts.substr(0, width - 3) + '...'; | ||
} | ||
|
||
|
||
console.warn(' %s: \x1B[90m%s\x1B[39m', key, counts); | ||
} | ||
} | ||
|
||
}); |