I am using Python and Sqlalchemy to store latitude and longitude values in a Sqlite database. I have created a hybrid method for my Location object,
@hybrid_method
def great_circle_distance(self, other):
"""
Tries to calculate the great circle distance between the two locations
If it succeeds, it will return the great-circle distance
multiplied by 3959, which calculates the distance in miles.
If it cannot, it will return None.
"""
return math.acos( self.cos_rad_lat
* other.cos_rad_lat
* math.cos(self.rad_lng - other.rad_lng)
+ self.sin_rad_lat
* other.sin_rad_lat
) * 3959
All the values like cos_rad_lat
and sin_rad_lat
are values I pre-calculated to optimize the calculation. Anyhow, when I run the following query,
pq = Session.query(model.Location).filter(model.Location.great_circle_distance(loc) < 10)
I get the following error,
line 809, in great_circle_distance
* math.cos(self.rad_lng - other.rad_lng)
TypeError: a float is required
When I print the values for self.rad_lng
and other.rad_lng
I get, for example,
self.rad_lng: Location.rad_lng
other.rad_lng: -1.29154947064
What am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…