In a system level programming language like C, C++ or D, what is the best type/encoding for storing latitude and longitude?
The options I see are:
- IEEE-754 FP as degrees or radians
- degrees or radians stored as a fixed point value in an 32 or 64 bit int
- mapping of an integer range to the degree range: ->
deg = (360/2^32)*val
- degrees, minutes, seconds and fractional seconds stored as bit fields in an int
- a struct of some kind.
The easy solution (FP) has the major down side that it has highly non uniform resolution (somewhere in England it can measure in microns, over in Japan, it can't). Also this has all the issues of FP comparison and whatnot. The other options require extra effort in different parts of the data's life cycle. (generation, presentation, calculations etc.)
One interesting option is a floating precision type that where as the Latitude increase it gets more bits and the Longitude gets less (as they get closer together towards the poles).
Related questions that don't quite cover this:
- What is the ideal data type to use when storing latitude / longitudes in a MySQL
- Working with latitude/longitude values in Java
BTW: 32 bits gives you an E/W resolution at the equator of about 0.3 in. This is close to the scale that high grade GPS setups can work at (IIRC they can get down to about 0.5 in in some modes)
I don't have a specific use in mind right now but have worked with this kind of thing before and expect to again, at some point.