This question already has an answer here:

In order to convert coordinates to geohash I need to know the precision as well but unable to figure how to do so..

Any suggestions?

** EDIT **

What I try to do is to find the precision in leaflet using VueJS based on zoom level or even using leaflet itself. I don't want to show the geohash squares on the map. Just use them somewhere else.

It's not really a duplicate since the given solution is in python and I try to do it in Javascript using VueJS.

marked as duplicate by Kadir Şahbaz, Fran Raga, Erik, Jochen Schwarze, BERA Mar 28 at 14:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Untested but something like:

import shapely
import geohash

#create a shapely polygon type from leaflet polygon
#unsure how leaftlet holds polygon coordinates though
pol = shapely.geometry.Polygon(leaflet_polygon_structure)

check = False
c = 1

#continue to increase the geohash precision
#until the polygon can't fit in the geohash anymore

while check == False:
    #centre point of polygon
    centroid = pol.centroid

    #create a geohash starting with length of c from the centroid lat and lon
    geohash = geohash.encode(centroid.x centroid.y, c)

    lat_centroid, lng_centroid, lat_offset, lng_offset = geohash.decode_exactly(geohash)

    corner_1 = (lat_centroid - lat_offset, lng_centroid - lng_offset)[::-1]
    corner_2 = (lat_centroid - lat_offset, lng_centroid + lng_offset)[::-1]
    corner_3 = (lat_centroid + lat_offset, lng_centroid + lng_offset)[::-1]
    corner_4 = (lat_centroid + lat_offset, lng_centroid - lng_offset)[::-1]

    geohashPolygon = shapely.geometry.Polygon([corner_1, corner_2, corner_3, corner_4, corner_1])

    #check whether or not leaflet polygon is within the geohash polygon
    #if so increase geohash precision for next iteration

    if pol.within(geohashPolygon):
        c += 1
    #if the polygon cant fit inside the geohash polygon anymore
    #stop the while loop and print the geohash and length
        check = False
        print(geohash, c)

Not the answer you're looking for? Browse other questions tagged or ask your own question.