heremaps / flexible-polyline

Flexible Polyline encoding: a lossy compressed representation of a list of coordinate pairs or triples
MIT License
89 stars 76 forks source link

Is it possible to enconde with a different charset? #78

Closed pedro-cf closed 3 months ago

pedro-cf commented 3 months ago

Greetings Is it possible to enconde with a different charset? like only include alphanumerical characters?

VeaaC commented 3 months ago

The default encoding uses url-safe characters, but you could modify it to encode with a different character set. If you only want alpha-numeric characters you only have 62 characters to work with (2 * 26 + 10), though, so you cannot encode 6 bits at a time, but e.g. only 5 (using 32 characters).

If that's what you want you can have a look at the functions that handle the base64 encoding/decoding, e.g. in Rust:

If you just want to modify the character set, but keep 64 characters then you simply need to replace the character tables/lookups, if you want to switch to only using 32 characters you need to also change the number of bits used (5->4, 6->5, 0x1F -> 0xF, 0x20 -> 0x10, etc)

Be advised, though, that going to a 5-bit encoding is much less efficient. Due to how the variable integer encoding works one bit is reserved, so the 6 bit encoding only has 5 bits payload. Going to a 5-bit encoding will be ~20-30% bigger (depends a lot on you data, it could also double the size if you are unlucky).

VeaaC commented 3 months ago

Closing. Feel free to re-open @pedro-cf in case you have more questions