# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Geohash encoding and decoding

Contributed by:
Anton Antonov

ResourceFunction["GeoHash"][l finds the geohash for the geo location with coordinates | |

ResourceFunction["GeoHash"][GeoPosition[ finds the geohash of a geo position. | |

ResourceFunction["GeoHash"][ decodes the given | |

ResourceFunction["GeoHash"][ give the decoded result in the form |

ResourceFunction["GeoHash"] is overloaded to do both encoding and decoding.

ResourceFunction["GeoHash"] is a geo coding system that encodes geographic coordinates into a short string of letters and digits.

The system allows arbitrary precision and can gradually lose precision by removing characters from the end of the code.

The system divides space into buckets of grid shape, using an application of a Z-order curve and space-filling curves.

The longer the shared prefix between two geohashes, the closer they are spatially. This does not apply in reverse though -- two points can be very close but may not share a prefix.

Geohashes can be used as unique identifiers and to represent point data in databases.

Geohashes have been proposed for geotagging.

The data structure offers advantages in databases. Geohashed data makes queries on a single index easier or faster than multiple-index queries. The structure can also provide a quick, though not entirely accurate, proximity search.

ResourceFunction["GeoHash"] uses a spatial index of base 4 and is transformed into a discrete grid for a more compact code. It uses base 32 and represents the values using an alphabet of standard ASCII characters.

The function ResourceFunction["GeoHash"] is overloaded to do encoding, decoding and provide properties. Here is table showing different forms of input and corresponding types.

Encode a given geo position:

In[1]:= |

Out[1]= |

Decode:

In[2]:= |

Out[2]= |

By default the decoding gives the mean point of the corresponding geo rectangle as a GeoPosition object:

In[3]:= |

Out[3]= |

If Mean is given a second argument then the mean point of the corresponding geo rectangle is returned:

In[4]:= |

Out[4]= |

Using Association as a second argument gives the geo rectangle coordinates in a nested association:

In[5]:= |

Out[5]= |

Often it is convenient to get the decoding result as a list of min-min and max-max geo positions of the corresponding geo rectangle:

In[6]:= |

Out[6]= |

Visualize the boundary of the result above:

In[7]:= |

Out[7]= |

If the first argument is a valid geohash string and the second argument is "Neighbors" then a list of neighboring geohashes is returned:

In[8]:= |

Out[4]= |

Here is a corresponding geo plot:

In[9]:= |

Out[9]= |

If the first argument is "Alphabet" then the Geohash encoding alphabet is returned:

In[10]:= |

Out[10]= |

Precision is used to confine the given location into into geo rectangle with an universal identifier (i.e. the geohash). Here are geohashes of the same geo point with different precisions:

In[11]:= |

Out[4]= |

Here are maps with the corresponding bounding regions:

In[12]:= |

Out[12]= |

Find nearest neighbors of a geohash at different precision levels:

In[13]:= |

Out[4]= |

Here is a corresponding Geographics plot:

In[14]:= |

Out[14]= |

Get all cities in Illinois, USA:

In[15]:= |

Out[4]= |

Make an association of the city names to corresponding coordinates:

In[16]:= |

Out[6]= |

Compute geohashes for all cities:

In[17]:= |

Out[17]= |

Find the geo coordinates of a certain focus location:

In[18]:= |

Out[18]= |

Find the corresponding geohash:

In[19]:= |

Out[19]= |

Find the Nearest Neighbors (NNs) of the location by matching a prefix of the focus location geohash in the association of Illinois-geohashes:

In[20]:= |

Out[21]= |

Plot the focus location and the found NNs:

In[22]:= |

Out[22]= |

Using a shorter prefix produces more NNs:

In[23]:= |

Out[24]= |

The geohashes represent nested rectangles -- adding a new "legal" alphabet character to geohash produces nested rectangles of finer granularity:

In[25]:= |

Out[4]= |

Here is another illustration of the geohash nesting -- the core geohash is extended with all characters of the alphabet twice:

In[26]:= |

Out[6]= |

Geohashes with an even number of digits result in a regular grid. Those with an odd number of digits result in an irregular intermediary grid.

The system can be used for proximity searches, but limitations exist. Locations close but on either side of the equator or Greenwich meridian will not have a shared prefix. The proximity search also breaks down at the 180 - degree meridians and the poles.

Here is how Rectangle and RegionMember can be used to verify that a geo position point belongs to the geo rectangle given by GeoHash:

In[27]:= |

Out[6]= |

Not every ASCII string is a valid geohash. Here are the characters * not* used for GeoHash encoding:

In[28]:= |

Out[28]= |

Visualize the Z-filling curve that corresponds to found hashes:

In[29]:= |

Out[22]= |

Wolfram Language 13.0 (December 2021) or above

- 1.0.0 – 10 June 2024

This work is licensed under a Creative Commons Attribution 4.0 International License