Wednesday, April 13, 2016

QuadKey - what it is?

To start with, let me share an excerpt from MSDN as below:



Bing Maps Tile System (Reference:  https://msdn.microsoft.com/en-us/library/bb259689.aspx )
Bing Maps provides a world map that users can directly manipulate to pan and zoom. To make this interaction as fast and responsive as possible, we chose to pre-render the map at many different levels of detail, and to cut each map into tiles for quick retrieval and display. This document describes the projection, coordinate systems, and addressing scheme of the map tiles, which collectively are called the Bing Maps Tile System.
Bb259689.5cff54de-5133-4369-8680-52d2723eb756(en-us,MSDN.10).jpg
So if you follow the above references Bing Maps Tile System, you would get an idea that  - how the world map is kind of apportioned into a tile system and depending on what level of detail or zoom you are interested in, you would get a <N> character long quad-key. 
For what I am involved with currently, we care to go for the maximum allowable detail to uniquely identify each geo-location, and hence Zoom Level = 23 !
Show me the code!
Well - there is not much you need to do if you are a .Net shop or a developer - other than grabbing the code from the above referenced URL on MSDN. For some, (like myself) our need is to actually run the generation of the QuadKey on the actual data store and that is EXASOL. 
EXASOL by itself is a fantastic in-memory database for Analytics (URL : http://www.exasol.com/en/ ) but it primarily supports running either Oracle PL/SQL scripts or Lua (https://www.lua.org/about.html) ... and then, there was a language that I liked to explore and must say, it is quite intuitive and easy to learn (well - I admit, I needed a crash course on Pluralsight)
How to generate QuadKey in Lua?




































The story does not end there, though! As mentioned earlier, my required level of detail is specified as 23 - which means I get 23 character long, string - all numeric values (from 0 through 3) but can't fit that into long numeric value!! Also, it is critical to have a computed, numeric value as part of the data schema for efficient search and retrieval of data, in addition to be able to encode and decode the actual Quad Key String.

So, then that led me to write up a function that would compute numeric value for that. Similar to how binary (base 2) are converted into decimal,. as Quad Keys are numeric values from 0, through 4 - as you might have figured can be treated as base 4 - and taking a cue from the binary conversion to numeric, the below is my encoding or numeric computation working function in Lua.

Now go ahead and give it a shot .. for a sample LATITUDE/LONGITUDE combination the below is a sample output..

--Test Data
latitudeSample  =  43.750546
longitudeSample = -79.716408

quadKey = GetQuadKey(latitudeSample, longitudeSample, levelOfDetail)

---> 03022313030302020021221
quadKeyEncoded = GetEncodedQuadKey(quadKey)
---> 13940604568169

No comments:

Post a Comment