Guest User

Untitled

a guest
Apr 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. def bbox_geohashes_shapely(bbox_pts, accuracy=5):
  2. """
  3. Given a list of lat/lon points marking the bounding box, return all geohashes within the box.
  4. Bounding box can be an arbitrary simple polygon (i.e., any number of sides are okay, but inclusions are not).
  5.  
  6. To see geohashes overlaid on map, visit http://geohash.gofreerange.com/
  7.  
  8. All bounding points are checked for in/out. After converting bounding points to geohashes, all neighbors
  9. are checked for presence in/out of bounding box. Neighbors of those points still inside are also checked,
  10. until no more points remain.
  11.  
  12. Parameters:
  13. -----------
  14. bbox_latlon : list of [lat,lon] points
  15. accuracy : length of geohash
  16.  
  17. Returns:
  18. --------
  19. inside : set of hashes contained within bounding box
  20.  
  21. """
  22. import geohash
  23. import shapely
  24. from shapely.geometry import Point
  25.  
  26. unchecked = set()
  27. inside = set()
  28. outside = set()
  29.  
  30. for pt in bbox_pts:
  31. tst_gh = geohash.encode(pt[0], pt[1], accuracy)
  32. unchecked.add(tst_gh)
  33.  
  34. bbox = shapely.geometry.Polygon(bbox_pts)
  35. while unchecked:
  36. this = unchecked.pop()
  37.  
  38. if bbox.contains(Point(geohash.decode(this))):
  39. inside.add(this)
  40.  
  41. for gh in geohash.neighbors(this):
  42. if (gh not in inside) & (gh not in outside) & (gh not in unchecked):
  43. unchecked.add(gh)
  44.  
  45. else:
  46. outside.add(this)
  47.  
  48. return inside
Add Comment
Please, Sign In to add comment