skip to Main Content

I have an array that has lat/lng coordinates in a angular typescript web app.

const myArr = [
  [lat, lng],
  [lat, lng],
  ...x 500 lines
]

I want to be able to convert this into a url query param for the user to revisit or share. Are there algarithms to hash the data so it’s vaiable to fit to a url? I know we can hash passwords 1 way via SHA-256. Are there two way hashes ideal for this scenario?

?mapSelection=myArrHash

2

Answers


  1. According to your questions I would recommend to use rison

    npm install rison
    

    This library is focused on getting, encoded strings that are ideal for your application.

    • human-readable
    • good compression level
    • URL-safe
    Login or Signup to reply.
  2. So lets do some back-of-a-napkin math:

    A lat/lng coordinate is going to have 2 digits in the integer part, an optional negative sign, and probably 3~5 digits worth of the fractional part depending on how precise you want to be. Lets call that 7 characters per coordinate, and you have two of them per point, plus a separator character. And you have ~500 points. That’s (7 + 7 + separator) * 500 = 7500 characters. While that is within the theoretical limit for some browsers (assuming you don’t have a 500 character domain lol) it won’t work in all of them and it’s close enough that if those assumptions I made were a little optimistic you’re going to be going over the limit.

    Plus you say you want people to be able to share these links, and I don’t think you want people to try posting links the length of a multi-page article.

    So I don’t think it’s going to work. You really only have about one option: use fewer lat/lng pairs (500 is a lot) and/or a lower degree of precision. Obligatory XKCD:
    https://xkcd.com/2170/

    If you only need one significant digit Number.prototype.toFixed(1) (like if you are doing routes between cities), and you drop the dot (you can always divide by 100 when deserializing), that takes it down to 3~4 characters per coordinate so lets say an average of 7 per pair (rather than per coordinate above). Lets also say we cut the number of required pairs in half to 250, that gives us 250 * (7 + separator) = 2000 characters. Which is probably still too much, but it’s a start.

    One last bit, be sure to pick a URL-friendly separator: if you use a separator character that has to be percent encoded you will blow up an additional 1000 characters or so.

    All that being said, this probably isn’t going to work at all, frankly there’s probably just too much data here to encode in a URL, but at least this hopefully gives you a jumping-off point to see how much you can compress it down.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search