geoSpatial & Location based search in google appen

2019-07-17 07:33发布

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条回答
2楼-- · 2019-07-17 08:11

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.

查看更多
登录 后发表回答