-
Notifications
You must be signed in to change notification settings - Fork 1
algorithm for locating services
eric-jahn edited this page Dec 8, 2014
·
1 revision
##In resource.py
-
def list(self, kind, lat, lng, radius):
db.resource.find({'location': {'$within':{'$center':[[float(lat),float(lng)],radius/100]}}, 'resource_type':kind}) returns all resources of type variable kind within radius
-
calls distance_on_unit_sphere(self, lat1, long1, lat2, long2) to calculate distance from user
-
sort resource (of type kind) based on distance
distance calculation: def distance_on_unit_sphere(self, lat1, long1, lat2, long2):
# Convert latitude and longitude to
# spherical coordinates in radians.
degrees_to_radians = math.pi/180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1)*degrees_to_radians
phi2 = (90.0 - lat2)*degrees_to_radians
# theta = longitude
theta1 = long1*degrees_to_radians
theta2 = long2*degrees_to_radians
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
arc = math.acos( cos )
# Remember to multiply arc by the radius of the earth
# in your favorite set of units to get length.
return arc