Skip to main content
 首页 » 编程设计

python之google app engine python中基于地理空间和位置的搜索

2024年10月17日23lovecherry

我想在 airbnb 上实现类似 map 拖动搜索的功能 (https://www.airbnb.com/s/Paris--France?source=ds&page=1&s_tag=PNoY_mlz&allow_override%5B%5D=)

我正在将这样的数据保存在数据存储中

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

每当我拖放 map 或放大或缩小时,我都会在我的 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})) 

下面给出了我的模型类

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() 

我想改进查询。

提前致谢。

请您参考如下方法:

不,您不能通过检查查询中的所有 4 个条件来改进解决方案,因为 ndb 查询不支持多个属性的不等式过滤器。来自 NDB Queries (强调我的):

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.

和

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