我正在开发一款Android应用程序,该应用程序必须与服务器通信才能在数据库中存储/检索模型。模型有纬度和经度两个参数,即FloatProperty
class Model(ndb.Model):
latitude = ndb.FloatProperty()
longitude = ndb.FloatProperty()
现在我有了一个请求处理程序,当它接收到带有特定标志和经度和纬度参数的GET请求时,它应该查询数据库,以找到处于特定距离的所有模型。
我做的是
latitude = float(self.request.get('latitude'))
longitude = float(self.request.get('longitude'))
max_fetch = 20
...
sin_lat = math.sin(latitude)
cos_lat = math.cos(latitude)
R = 6371 * math.pow(10, 3)
max_distance = 1000
around_you = Model.query(math.acos(sin_lat*math.sin(Model.latitude) +
cos_lat*math.cos(Model.latitude)*math.cos(longitude - Model.longitude)) *
R <= max_distance).fetch(max_fetch)
但这样做会给我一个
TypeError: a float is required
当我尝试做时
math.sin(Model.latitude)
有关于如何解决这个问题的提示吗?
数据存储本身不支持任何数学运算。您可以将其中任何一个存储在ComputedProperty
中(因为这实际上是在使用.put()
将数据发送到存储之前在应用程序的Python代码中计算的),但不支持外部"参数"——它必须完全根据实例上的值的属性来计算。
因此,ComputedProperty
可以是FloatProperty
的math.sin
,但这里似乎需要基于"外部参数"latitude
和longitude
混合。
更确切地说,看看应用引擎搜索API,https://cloud.google.com/appengine/docs/python/search/--,而不是数据存储,似乎提供了所需的功能(特别是通过GeoPoint
类型和GeoField
字段类型)。