按GPS定位排序数据库查询

2024-09-29 23:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一个关于googleappengine的网站,我有一个位置数据存储实体,它有一个包含GPS坐标的字符串属性。我想让用户通过GPS坐标搜索,我想返回所有在经纬度+/-10点范围内的位置。基本上,我想做的是在下面的代码中,但我不能这样对GPS排序,一是因为它是一个字符串,二是因为它是同一个属性。在

  inputlocation = self.request.get("userlocation")

        g = geocoders.Google()
        try:
            place, (lat, lng) = g.geocode(inputlocation)
        except ValueError:
            geocodespot = g.geocode(inputlocation, exactly_one=False)
            place, (lat, lng) = geocodespot[0]
         GPSlocation = "("+str(lat)+", "+str(lng)+")"
         GPSlocation = float(GPSlocation)

         bound = 10
         upper = GPSlocation + bound
         lower = GPSlocation - bound
         left = GPSlocation + bound
         right = GPSlocation - bound

        if GPSlocation:
            locations = db.GqlQuery("select * from Location where GPSlocation>:1 and where GPSlocation>:2 and where GPSlocation <:3 and where GPSlocation <:4 order by created desc limit 20", upper, lower, left, right)
#example GPSlocation in the datastore = "(37.7699298, -93.4469157)"

你能想出什么方法来基本上做到这一点而不必改变数据存储的设置方式吗?有没有什么方法可以不使用两个属性一个是纬度,一个是经度?在


Tags: and数据字符串属性placewheregpsgeocode
2条回答

也许您想添加一个计算属性:

https://developers.google.com/appengine/docs/python/ndb/properties#computed

Computed properties (ComputedProperty) are read-only properties whose value is computed from other property values by an application-supplied function. The computed value is written to the Datastore so that it can be queried and displayed in the Datastore viewer, but the stored value is ignored when the entity is read back from the Datastore; rather, the value is recomputed by calling the function whenever the value is requested.

class SomeEntity(ndb.Model):
  name = ndb.StringProperty()
  name_lower = ndb.ComputedProperty(lambda self: self.name.lower())

x = SomeEntity(name='Nick')

因此,您需要某种类型的函数,而不是上面的lower(),它计算并更新模型中新的LatFloat,longfoat。因此,您可以将数据保存为两个浮点和一个字符串。我相信你只需添加这个,你现有的数据不会受到影响,相反,当你试图阅读或搜索这些数据时,它会被计算并返回。在

看起来Google GAE有一些工具可以让你做你想做的事情:

接近取回看起来像你需要的

def proximity_fetch(query, center, max_results=10, max_distance=0):

相关问题 更多 >

    热门问题