I have an application which reads in gridded coordinate data with values (i.e. lat, lon, value) and then plots this data on a map. The map that is drawn uses a lambert conformal conic projection and therefore all the coordinate data has to be converted from lat/lon to easting/northing. This all works great, however there is a noticeable performance issue when reading in several data files. Since all the data files contain the same set of points (although not necessarily in the same order) I’m thinking some sort of lookup table would be useful for the coordinate conversions. However I’ve never used lookup tables before and am having some trouble figuring out the design.
In a nutshell – any suggestions on the fastest way to take a lat/lon coordinate pair (float values) and look up the corresponding E/N pair (float values) assuming there is a 1:1 relationship between the coordinate pairs, no missing values, etc?
Since the lat/lon are float values I can’t use them as array indices (ex: lookup_array[lat][lon]) obviously so this is really where I’m getting the most tripped up.
Note: this solution can be in either C or C++, whichever has the optimal solution.
Because the space of possible lat/longs is large, I don’t think it’s feasible to build the whole table beforehand. However, you could remember the result of each transformation in (say) a hash table. Then each time you go to make a transformation, you first check to see if it’s in the table. If it isn’t, you do the calculation and store it in the hash table for next time.
However, it’s not a good idea to use floats as keys in any lookup based data structure (see this question). In the case of lat/long data, if your input is in a consistent format, then you could avoid this problem by using strings as keys (and if it isn’t, then you could use
sprintf()or similar to make a lat/long string in a consistent format down to a given precision).If you’d like an out of the box hash table implementation for C++, have a look at
std::unordered_mapand this wikipedia article.