Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def bbox_geohashes_shapely(bbox_pts, accuracy=5):
- """
- Given a list of lat/lon points marking the bounding box, return all geohashes within the box.
- Bounding box can be an arbitrary simple polygon (i.e., any number of sides are okay, but inclusions are not).
- To see geohashes overlaid on map, visit http://geohash.gofreerange.com/
- All bounding points are checked for in/out. After converting bounding points to geohashes, all neighbors
- are checked for presence in/out of bounding box. Neighbors of those points still inside are also checked,
- until no more points remain.
- Parameters:
- -----------
- bbox_latlon : list of [lat,lon] points
- accuracy : length of geohash
- Returns:
- --------
- inside : set of hashes contained within bounding box
- """
- import geohash
- import shapely
- from shapely.geometry import Point
- unchecked = set()
- inside = set()
- outside = set()
- for pt in bbox_pts:
- tst_gh = geohash.encode(pt[0], pt[1], accuracy)
- unchecked.add(tst_gh)
- bbox = shapely.geometry.Polygon(bbox_pts)
- while unchecked:
- this = unchecked.pop()
- if bbox.contains(Point(geohash.decode(this))):
- inside.add(this)
- for gh in geohash.neighbors(this):
- if (gh not in inside) & (gh not in outside) & (gh not in unchecked):
- unchecked.add(gh)
- else:
- outside.add(this)
- return inside
Add Comment
Please, Sign In to add comment