geohash icon indicating copy to clipboard operation
geohash copied to clipboard

bounds to geohash

Open jtarchie opened this issue 5 months ago • 0 comments

For a project, I was looking for a way to convert a bounds to a geohash. Given the difference in precision levels of geohash, I thought it would get close enough.

Would you be open to a PR adding the feature?

func BoundingBoxToPrecision(bounds Box) {
	geohashPrecisions := [][2]float64{
		{0.0372, 0.0186},
		{0.149, 0.149},
		{1.19, 0.596},
		{4.77, 4.77},
		{38.2, 19.1},
		{153, 153},
		{1220, 610},
		{4890, 4890},
		{39100, 19500},
		{156000, 156000},
		{1250000, 625000},
		{5000000, 5000000},
	}

	height := 111131.75 * (b.MaxLat[1] - b.MinLat[1])

	for index, precision := range geohashPrecisions {
		if height <= precision[0] {
			return max(0, uint(len(geohashPrecisions) - index))
		}
	}

	return uint(len(geohashPrecisions))
} 

Example implementation is above.

jtarchie avatar Aug 29 '24 18:08 jtarchie