Reputation: 1237
I recently came across a YouTube video that discussed handling GPS coordinates by using space filled curves. There was a formula provided to accomplish this so I've decided to try and replicate it, however I have not been able to figure out how to get to the same result using JavaScript.
The formula was as follows:
Scale Latitude and longitude to use 16 available bits each:
scaled_x = (-122.4012 + 180) / 360 * 2 ^ 16 // result = 10485
scale_y = (37.7839 + 90) / 180 * 2 ^ 16 // result = 46524
I've tries several ways of writing the formula and my results are far off from what I should be getting based on what was shown in the presentation. Either the presentation was inaccurate or I've not landed on the correct way of getting to this.
Here are some of my attempts, all fail.
Using Pow
base = Math.round((lat + 180) / 360);
scale = Math.pow(base * 2, 16); // Result = 0
Exact Formula
base = Math.round((lat + 180) / 360 * 2 ^ 16); // Result = 16
Inline Power
base = Math.round((lat + 180) / 360 * Math.pow(2, 16)); // Result = -22282
Does anyone know how this formula needs to be structured in JavaScript to get the expected outcome?
Upvotes: 2
Views: 37
Reputation:
base = Math.round((lat + 180) / 360);
scale = Math.pow(base * 2, 16)
This doesn't work because you're rounding the value before multiplying it by 216 -- so it will round to either 0 or 1. This is not what you want.
base = Math.round((lat + 180) / 360 * 2 ^ 16);
In Javascript -- and many other languages -- ^
is used for bitwise XOR, not exponentiation. 2 ^ 16
is 18, not 65536.
base = Math.round((lat + 180) / 360 * Math.pow(2, 16));
This looks correct. The result you've quoted isn't right for lat = -122.4012
, though -- did you leave out the + 180
, perhaps?
Upvotes: 1