'How convert latitude & longitude to Morton code(z order curve)
I search on the Internet, but find merely a little information about How to convert the latitude & longitude to Morton code(z order curve). From the link I know how to make two int to Morton Code. But, if I have float values like latitude or longitude, How should I convert the float value to int? Then I can convert the int to Morton code. For exmaple, c# code:
float value a=43.2345f;
int aint1=43.2345*10000;
int aint2=(int)BitConverter.DoubleToInt64Bits(43.2345);
Here, I don't have any idea which I should choose. Could you please help me? I didn't find a method like 'BitConverter.FloatToInt32Bits'. As of now, I don't know the reason.
Upate 1: I found an answer here, but I don't quite understand the answer.
Solution 1:[1]
I have spent sometime searching for answer to that question. It appears that spatial curves (z-order curve, Hilbert curve, etc) need to be build on positive integers, which is a problem because lat/lon can be negative floating numbers.
Found several projects where solution is to convert to positive integers, for example:
// lat: -90 .. +90
var iLat = Math.round((lat + 90.0) * 100000); // 5 digits
// lng: -180 .. +180
var iLng = Math.round((lng + 180.0) * 100000); // 6 digits
After converting to ints some convert integers to bit arrays and build curve on those values, while others just use the integer representation to build curve. I guess for large space length of the curve might be too big to fit into an integer and that is why arrays are used. However, I am still looking into that.
Solution 2:[2]
first, you must create boxes, I mean you should devide surface into some bins (a matrix). Then you can describe each geographical point with a new <x,y> coordinates (cartesian coordinates) related to its position in the matrix.
after that, you can convert the cartesian coordinates into morton coordinates .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alex Protyagov |
Solution 2 | morteza |