0

我正在用 Python 在 Google App Engine 上编写一个网站,并且我有一个 Location 数据存储实体,它有一个包含 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)"

你能想出任何方法来基本上做到这一点,而不必改变我的数据存储的设置方式吗?有没有什么方法可以获取这些信息,而不仅仅是制作两个属性,一个用于纬度,一个用于经度?

4

2 回答 2

2

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

该proximity_fetch 看起来像你需要的

def proximity_fetch(query, center, max_results=10, max_distance=0):
于 2012-10-10T03:58:18.470 回答
1

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

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

计算属性 (ComputedProperty) 是只读属性,其值由应用程序提供的函数根据其他属性值计算得出。计算的值被写入Datastore,以便在Datastore查看器中查询和显示,但是当从Datastore读回实体时,存储的值会被忽略;相反,每当请求该值时,都会通过调用该函数来重新计算该值。

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

x = SomeEntity(name='Nick')

因此,您需要某种函数而不是上面的 lower(),它计算然后更新模型中的新 LatFloat、LongFloat。因此,您可以将数据保存为两个浮点数和一个字符串。我相信您可以添加它并且您现有的数据不会受到影响,而是当您尝试读取或搜索该数据时,它将被计算并返回。

于 2012-10-10T10:34:35.557 回答