Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Understanding Geohashes

Posted on Jul 24 Hello there.Are you curious about how geographic locations can be represented as a short string of letters and numbers? Or how this technique helps in fast retrieval of location data?This post will take you on a deep dive into geohashes, explaining what they are, how they work, and their practical uses. Whether you're a data scientist, a software engineer, or just someone with a knack for geography and data, this post will help you grasp the concept of geohashes and their significance.The purpose of this post is to explain in depth how a Geohash works and its subtleties step-by-step.Chat GPT says:A geohash is a location encoding system that represents a geographic location using a short string of letters and numbers. It works by dividing the world into a grid of squares, assigning each square a unique hash. The length of the hash controls the precision, with longer hashes corresponding to smaller, more precise squares.That gives us a general idea. You use a geohash to identify areas on a map. The easiest way to visualize them is with an interactive mapDatabases are better optimized for text than geolocation queries. If you need fast retrieval over a large collection of data, R-Tree indexes might not be enough.Quoting Chris Hewett's website:Geohash avoids/minimises the use of (usually) slow geospacial functions. Who you going to call when your ST_CONTAINS() is slow and an EXPLAIN shows it's already using an index...Also, geohashes can be used as an alternative to tile coordinates in order to load chunks of a map and possibly cache them on the client side.As chat gpt said, a geohash length describes its precision. For instance, dr7 is a geohash with precision 3. That means that its parent, dr (precision 2), encloses a larger area that also contains its neighbors.There are 2 types of geohash grids (width x height): 8x4 and 4x8. That means that for any given zoom level, there are 32 possible squares inside.We start with an 8x4 grid with the whole world inside. Each time you go one precision deeper, the type of grid you get alternates. This is made this way, because if we kept grids in the same format for all zoom levels, we would eventually get super thin areas.Since there are 8x4 and 4x8 grids only, we use these 32 characters to encode each precision: 0123456789bcdefghjkmnpqrstuvwxyz.They range from 0 to z, but beware, there are a few letters missing in this specific encoding, such as a;i;l and o.If you want to find out the geohash in which a point lies into, you have to bisect it 5 times. Why 5? Because 2^5 is 32, which is the total amount of geohashes for each precision level. Let's do a dry run of the algorithm to figure out in which geohash1 a location L in argentina lies into.Notice that we always have two possible moves for each step: choose either the first or the second half. The moves alternate between latitude and longitude, so we are able to move in 2D.Also, we have to somehow translate the accumulated result of those moves into an index for the string 0123456789bcdefghjkmnpqrstuvwxyz.The rules for that are:That means we are using even numbers to identify first-half moves and odd numbers for second-half moves.The highest value we can get is 31:Since our index starts at 0, that would be 32 possibilities.This is exactly what the code is doing:Then we stop once the precision is met.If you check the h3geo comparison page, you should see plenty of alternatives to geohash, such as s2 or even h3 itself.ST_CONTAINS: postgis function that finds geometries inside areas;EXPLAIN: postgres statement that calculates query costTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Simon Pfeiffer - Jul 24 Alexis Boucouvalas - Jul 24 Yaroslav - Jul 24 Brian Barbour - Jul 24 Once suspended, trickstival will not be able to comment or publish posts until their suspension is removed. Once unsuspended, trickstival will be able to comment and publish posts again. Once unpublished, all posts by trickstival will become hidden and only accessible to themselves. If trickstival is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Patrick Stival. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag trickstival: trickstival consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging trickstival will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Understanding Geohashes

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×