svg-mapper
Version:
Tool to slice big, annotated SVG into WebMercator tiles
155 lines (154 loc) • 4.81 kB
JavaScript
// Generated by LiveScript 1.2.0
/*
This module provides method for generating the UTFGrid JSON. First, all
exportable fields (paths) should be loaded by getExportableIndex and colored
with getColorByIndex. Then, canvas with a such colored tile should be passed
to generateJson, which outputs the resulting UTFGrid JSON.
*/
(function(){
var TileJsonGenerator;
module.exports = TileJsonGenerator = (function(){
TileJsonGenerator.displayName = 'TileJsonGenerator';
var prototype = TileJsonGenerator.prototype, constructor = TileJsonGenerator;
prototype.exportables = null;
prototype.colorInterval = 5;
prototype.jsonGranularity = 4;
function TileJsonGenerator(exportables){
this.exportables = exportables != null
? exportables
: [null];
}
prototype.generateJson = function(canvas){
var ctx, keys, data, grid, res$, i$, ref$, len$, y, cols, res1$, j$, ref1$, len1$, x, id, index, len, chr;
ctx = canvas.getContext('2d');
keys = ["0"];
data = {
"0": null
};
res$ = [];
for (i$ = 0, len$ = (ref$ = (fn$.call(this))).length; i$ < len$; ++i$) {
y = ref$[i$];
res1$ = [];
for (j$ = 0, len1$ = (ref1$ = (fn1$.call(this))).length; j$ < len1$; ++j$) {
x = ref1$[j$];
id = this.getJsonCellId(ctx, x, y);
index = keys.indexOf(id);
if (index === -1) {
len = keys.push(id);
data[id] = JSON.parse(this.exportables[id]);
index = len - 1;
}
chr = this.idToChar(index);
res1$.push(chr);
}
cols = res1$;
res$.push(cols.join(''));
}
grid = res$;
return {
grid: grid,
keys: keys,
data: data
};
function fn$(){
var i$, step$, to$, results$ = [];
for (i$ = 0, to$ = canvas.height, step$ = this.jsonGranularity; step$ < 0 ? i$ > to$ : i$ < to$; i$ += step$) {
results$.push(i$);
}
return results$;
}
function fn1$(){
var i$, step$, to$, results$ = [];
for (i$ = 0, to$ = canvas.width, step$ = this.jsonGranularity; step$ < 0 ? i$ > to$ : i$ < to$; i$ += step$) {
results$.push(i$);
}
return results$;
}
};
prototype.getExportableIndex = function(str){
var index, length;
index = this.exportables.indexOf(str);
if (index !== -1) {
return index;
}
length = this.exportables.push(str);
return length - 1;
};
prototype.getColorByIndex = function(index){
var color;
index *= this.colorInterval;
color = index.toString(16);
while (color.length < 6) {
color = "0" + color;
}
return "#" + color;
};
prototype.getJsonCellId = function(ctx, x, y){
var scores, attemptCount, i$, ref$, len$, dx, j$, ref1$, len1$, dy, that, maxScore, maxScoreId, id, score;
scores = {};
attemptCount = Math.pow(this.jsonGranularity, 2);
for (i$ = 0, len$ = (ref$ = (fn$.call(this))).length; i$ < len$; ++i$) {
dx = ref$[i$];
for (j$ = 0, len1$ = (ref1$ = (fn1$.call(this))).length; j$ < len1$; ++j$) {
dy = ref1$[j$];
if (that = this.getCellIdAtPixel(ctx, x + dx, y + dy)) {
scores[that] == null && (scores[that] = 0);
scores[that]++;
}
}
}
maxScore = -Infinity;
maxScoreId = null;
for (id in scores) {
score = scores[id];
if (score >= attemptCount / 2) {
return id;
} else if (score > maxScore) {
maxScore = score;
maxScoreId = id;
}
}
if (maxScore >= attemptCount / 4) {
return maxScoreId;
}
return 0;
function fn$(){
var i$, to$, results$ = [];
for (i$ = 0, to$ = this.jsonGranularity; i$ < to$; ++i$) {
results$.push(i$);
}
return results$;
}
function fn1$(){
var i$, to$, results$ = [];
for (i$ = 0, to$ = this.jsonGranularity; i$ < to$; ++i$) {
results$.push(i$);
}
return results$;
}
};
prototype.getCellIdAtPixel = function(ctx, x, y){
var data, r, g, b, id;
data = ctx.getImageData(x, y, 1, 1).data;
r = data[0], g = data[1], b = data[2];
id = r << 16 | g << 8 | b;
id /= this.colorInterval;
if (this.exportables[id]) {
return id;
} else {
return null;
}
};
prototype.idToChar = function(id){
id += 32;
if (id >= 34) {
id += 1;
}
if (id >= 92) {
id += 1;
}
return String.fromCharCode(id);
};
return TileJsonGenerator;
}());
}).call(this);