Vault 42
A refreshing Nuka-lear taste
By TOMISLAV FILIPČIĆ

C-squares global grid codes

I needed to cache weather API requests that I was making to yr.no with memcached. Since every request was for a specific latitude/longitude coordinate there was no unique key I could use. This can be solved by using C-squares.

C-squares is a system for storage, querying, display, and exchange of “spatial data” locations and extents in a simple, text-based, human– and machine– readable format. It uses numbered (coded) squares on the earth’s surface measured in degrees (or fractions of degrees) of latitude and longitude as fundamental units of spatial information, which can then be quoted as single squares (similar to a “global postcode”) in which one or more data points are located, or be built up into strings of codes to represent a wide variety of shapes and sizes of spatial data “footprints”.

For every latitude/longitude coordinate it is possible to get a machine and human readable code at the specified resolution. For example a coordinate with latitude 45.80636702443359 and longitude 15.978755950927734 at a 0.1° resolution (10 km2) has the global grid code 1401:455:489. This can be then used a key for memcached as every coordinate within that 10 km2 has the same code.

I have posted a c-squares implementation in PHP on GitHub.

More in the Archive