Created
April 3, 2016 13:48
-
-
Save juanriaza/2f3a2077aeb353b3bcde9aed26b3d1b3 to your computer and use it in GitHub Desktop.
geo.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geopy | |
from geopy.distance import vincenty | |
def bounding_box_coordinates(location, radius): | |
''' | |
Given a latitude and longitude in degrees as a tuple and a radius in km | |
calculates the bounding box coordinates using the vincenty formula. | |
''' | |
origin = geopy.Point(*location) | |
COMPASS_BEARING = { | |
'NORTH': 0, | |
'EAST': 90, | |
'SOUTH': 180, | |
'WEST': 270} | |
max_lat = vincenty(kilometers=radius).destination( | |
origin, COMPASS_BEARING['NORTH']).latitude | |
min_lat = vincenty(kilometers=radius).destination( | |
origin, COMPASS_BEARING['SOUTH']).latitude | |
max_long = vincenty(kilometers=radius).destination( | |
origin, COMPASS_BEARING['EAST']).longitude | |
min_long = vincenty(kilometers=radius).destination( | |
origin, COMPASS_BEARING['WEST']).longitude | |
locations = ( | |
min_lat, | |
min_long, | |
max_lat, | |
max_long) | |
return locations | |
def bounduary_filter(center, location, radius=1): | |
''' | |
Given the center of the bounding circle and the location on degrees as | |
tuples it calculates if the location is within the bounduary radius given | |
in kilometers. | |
''' | |
distance = geopy.distance.vincenty( | |
geopy.Point(*center), | |
geopy.Point(*location) | |
).kilometers | |
return distance <= radius | |
print bounduary_filter((40.4183, -3.7028), (40.4093, -3.7028)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment