Created
January 13, 2021 19:08
-
-
Save ohaal/72d8b4767a858be9bc22ac4de2bdb256 to your computer and use it in GitHub Desktop.
bad positive coord float compression
This file contains hidden or 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
// use geohash instead! | |
function encodePositiveFloat(number) { | |
return compressInt(number | |
.toString() | |
.indexOf('.') | |
.toString() | |
.concat(number | |
.toString() | |
.replace(".", ""))); | |
} | |
function decodePositiveFloat(str) { | |
let d = decompressInt(str); | |
let i = parseInt(d.slice(0,1), 10); | |
return parseFloat(d.slice(1,i+1) + "." + d.slice(i+1)); | |
} | |
function compressInt(integer) { | |
let num = 4; | |
let str = integer.toString(); | |
let len = str.length; | |
return str | |
.padEnd(len + (num-(len % num)) % num, '0') | |
.match(new RegExp(".{"+num+"}","g")) | |
.map((str) => String.fromCharCode(str)) | |
.join(''); | |
} | |
function decompressInt(str) { | |
return str | |
.split('') | |
.map((chr) => chr.charCodeAt(0).toString().padStart(4, '0')) | |
.join(''); | |
} | |
const coordinates = [ | |
59.012529932141405, | |
5.825963706152152, | |
58.900780849517250, | |
5.634868640551076 | |
]; | |
const param = coordinates.map(encodePositiveFloat).join(String.fromCharCode(10000)); | |
console.log(param); | |
param | |
.split(String.fromCharCode(10000)) | |
.forEach(coord => console.log(decodePositiveFloat(coord))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment