arrays - Position compression using relative coordinates in Javascript -
goodafternoon. have set of coordinates
[ [ 52.52132, 4.52342 ], [ 52.52144, 4.52352 ], [ 52.52154, 4.52354 ], [ 52.52166, 4.52376 ] ]
how can transform first position (the first 2 coordinates) become base. , following positions relative distances base?
so, pseudo example:
this
[ [ 52.52132, 4.52342 ], [ 52.52144, 4.52352 ], [ 52.52154, 4.52354 ], [ 52.52166, 4.52376 ] ]
would become this:
[ [ 52.52132, 4.52342 ], [ 0.4123, 0.1232 ], [ 0.1232, 0.5523 ], [ 0.1233, 0.1232 ] ]
where first part [ 52.52132, 4.52342 ] starting point. , other coordinates relative previous one.
is after..
i'm not sure how getting 0.4123, 0.1232, 52.52144 - 52.52132 = 0.00012
also if after simple latlng compression / decompression system. i've done little 1 here, it's simple compressor,.. after works out different. multiple loops of multiplying diffs 1 10 100 1000, etc. , keeps track of return smallest stringified result. in store multiplier first element.
eg. example compress -> 5,52.52132,4.52342,12,10,10,2,12,22
the 5 equal difference mutliplier of 100000, use work out divide difference by. adding zlib compress more.
var g = [ [ 52.52132, 4.52342 ], [ 52.52144, 4.52352 ], [ 52.52154, 4.52354 ], [ 52.52166, 4.52376 ] ], n = g.slice(0,1); function compresslatlng(g) { var smallest = null, r, mul; if (!g.length) return ''; (var l = 1; l < 6; l ++) { mul = math.pow(10, l); r = [l,g[0][0],g[0][1]]; (var k = 1; k < g.length; k ++) { r.push( ((g[k][0] - g[k-1][0])*mul).tofixed(5)*1, ((g[k][1] - g[k-1][1])*mul).tofixed(5)*1 ); } var j = r.join(','); if (!smallest) smallest = j; else if (j.length < smallest.length) smallest = j; } return smallest; } function uncompresslatlng(s) { var r = s.split(','); if (!r.length) return []; var mul = math.pow(10,r[0]); var j = [[r[1]*1, r[2]*1]]; var last = j[0]; (var l = 3; l < r.length; l += 2) { var t = [ (last[0] + r[l] / mul).tofixed(5)*1, (last[1] + r[l+1] / mul).tofixed(5)*1 ]; j.push(t); last = t; } return j; } (var l = 1; l < g.length; l ++) { n.push([ (g[l][0] - g[l-1][0]).tofixed(5)*1, (g[l][1] - g[l-1][1]).tofixed(5)*1 ]); } console.log('o:original s:simple c:compressed u:uncompressed'); console.log('o: ' + json.stringify(g)); console.log('s: ' + json.stringify(n)); var compressed = compresslatlng(g); console.log('c: ' + compressed); console.log('u: ' + json.stringify(uncompresslatlng(compressed)));
Comments
Post a Comment