DiamondJoe12
DiamondJoe12

Reputation: 1809

Unable to get bounding box of Geohash

I'm trying to get the bounding box (x,y coordinates) of geohashes using Python's geohash module. I'm able to successfully read in the geohashes and get their centroid, but when I try to use the geohash.bbox() method, it fails. Here's code:

#import modules
import Geohash
import csv




    dataArray = []
    
    with open('C:\Users\Desktop\data.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            geoHash = row[0] # this is the geohash identifier
            trips = row[1]
            share_of_trips = row[2]
            time_ID = row[3]
            perc_trips = row[4]
            geoStr = str(geoHash)
            latLong = Geohash.decode_exactly(geoStr)
            # Get Bounding Box
            boundingBox = Geohash.bbox(geoStr)
            print boundingBox

I'm able to successfully print the lat long pairs, but unable to get the bounding box. The documentation says:

The error I'm getting is:

AttributeError: 'module' object has no attribute 'bbox'

When I use geohash, as opposed to Geohash, it says geohash is not defined.

I've read the documentation:

geohash.bbox(hashcode) Bounding box for a geohash hashcode. This method returns a dictionary with keys of "s", "e", "w" and "n" which means South, East, West and North respectively.

>>> geohash.bbox('ezs42')
{'s': 42.5830078125, 'e': -5.5810546875, 'w': -5.625, 'n': 42.626953125}

Upvotes: 2

Views: 4242

Answers (2)

allesklarbeidir
allesklarbeidir

Reputation: 332

Try https://github.com/bashhike/libgeohash instead. Looks even better than the libraries you mentioned.

Here are some examples:

import libgeohash as gh

shapleypolygon = gh.geohash_to_polygon(["u1q"])
bounds = shapleypolygon.bounds
print(bounds)

Outputs:

(8.4375, 52.03125, 9.84375, 53.4375)

Or

import libgeohash as gh
bbox = gh.bbox("u1q")
print(bbox)

Outputs:

{'n': 53.4375, 's': 52.03125, 'w': 8.4375, 'e': 9.84375}

I find it very useful to convert the geohash to a shapely polygon using the geohash_to_polygon method.

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83537

but when I try to use the geohash.bbox() method, it fails

Your code has Geohash.bbox() which is not the same thing.

When I use geohash, as opposed to Geohash, it says geohash is not defined.

That is because you have import Geohash. Maybe you need to change this to import geohash instead.

My google search for "python geohash" turns up at least two libraries. The documentation for one shows that you need to do import Geohash, but this library does not appear to have a bbox() function. The documentation for the second library has a bbox() function, but requires import geohash. I suggest you figure out which library you are using and look closely at the documentation for that library in order to determine correct usage.

Upvotes: 1

Related Questions