geoSpatial & Location based search in google appengine python

独自空忆成欢 提交于 2019-12-11 03:01:16

问题


I want to achieve something like the map drag search on airbnb (https://www.airbnb.com/s/Paris--France?source=ds&page=1&s_tag=PNoY_mlz&allow_override%5B%5D=)

I am saving the data like this in datastore

 user.lat = float(lat)
     user.lon = float(lon)
     user.geoLocation = ndb.GeoPt(float(lat),float(lon))

and whenever I drag & drop map or zoom in or zoom out I get following parameters in my controller

    def get(self):
    """
    This is an ajax function. It gets the place name, north_east, and south_west
    coordinates. Then it fetch the results matching the search criteria and
    create a result list. After that it returns the result in json format.
    :return: result
    """
    self.response.headers['Content-type'] = 'application/json'
    results = []
    north_east_latitude = float(self.request.get('nelat'))
    north_east_longitude = float(self.request.get('nelon'))
    south_west_latitude = float(self.request.get('swlat'))
    south_west_longitude = float(self.request.get('swlon'))
    points = Points.query(Points.lat<north_east_latitude,Points.lat>south_west_latitude)
    for row in points:
        if  row.lon > north_east_longitude and row.lon < south_west_longitude:
            listingdic = {'name': row.name, 'desc': row.description, 'contact': row.contact, 'lat': row.lat, 'lon': row.lon}
            results.append(listingdic)
    self.write(json.dumps({'listings':results}))

My model class is given below

class Points(ndb.Model):
    name = ndb.StringProperty(required=True)
    description = ndb.StringProperty(required=True)
    contact = ndb.StringProperty(required=True)
    lat = ndb.FloatProperty(required=True)
    lon = ndb.FloatProperty(required=True)
    geoLocation = ndb.GeoPtProperty()

I want to improve the query.

Thanks in advance.


回答1:


No, you cannot improve the solution by checking all 4 conditions in the query because ndb queries do not support inequality filters on multiple properties. From NDB Queries (emphasis mine):

Limitations: The Datastore enforces some restrictions on queries. Violating these will cause it to raise exceptions. For example, combining too many filters, using inequalities for multiple properties, or combining an inequality with a sort order on a different property are all currently disallowed. Also filters referencing multiple properties sometimes require secondary indexes to be configured.

and

Note: As mentioned earlier, the Datastore rejects queries using inequality filtering on more than one property.



来源:https://stackoverflow.com/questions/40550590/geospatial-location-based-search-in-google-appengine-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!