def convert(self, value): if value is None: return None point = point_from_lat_long(value) return (point.y, point.x)
def prepare(self, obj): value = SearchField.prepare(self, obj) if value is None: return None point = point_from_lat_long(value) return "%s,%s" % (point.y, point.x)
def prepare(self, obj): value = SearchField.prepare(self, obj) if value is None: return None point = point_from_lat_long(value) return '%s,%s' % (point.y, point.x)
def setUp(self): # Ensure we have results. total = SearchQuerySet().count() self.assertTrue(total) # Get some real coords from the index point = point_from_long_lat((151.9491543, -27.4581498)) distance = Distance(km=100000000) result = (SearchQuerySet().dwithin('geoposition', point, distance).distance( 'geoposition', point).order_by('distance'))[0] self.point = point_from_lat_long(result.geoposition)
def setUp(self): # Ensure we have results. total = SearchQuerySet().count() self.assertTrue(total) # Get some real coords from the index point = point_from_long_lat((151.9491543, -27.4581498)) distance = Distance(km=100000000) result = ( SearchQuerySet() .dwithin('geoposition', point, distance) .distance('geoposition', point) .order_by('distance') )[0] self.point = point_from_lat_long(result.geoposition)
def radius_filter(self, min_km=None, max_km=None, order_by=None, **geo_lookup): """ Perform a radius filter from the specified coordinates (lat, long) or a Point instance. If "order_by" is provided, an extra field is created on each result with that name, containing the distance from the coordinates. Results are then ordered by the distance. Suggested values to use are '-distance' (closest first) or 'distance' (farthest first). For example, to search for results within 0 and 5km from the coordinates, with the closest results first, you could do: SearchQuerySet().radius(0, 5, geoposition=(lng, lat), order_by='distance') """ assert len(geo_lookup) == 1, 'You must supply field_name=coordinates' # Ensure the coords are in the right order and recreate the lookup. # Always provide coordinates in lat,lng and not the other way around! field_name, coordinates = geo_lookup.items()[0] point = point_from_lat_long(coordinates) geo_lookup = { field_name: point.get_coords() } queryset = self if min_km: queryset = queryset.dminimum(field_name, point, Distance(km=min_km)) if max_km: queryset = queryset.dwithin(field_name, point, Distance(km=max_km)) if order_by: # Add a distance field to each result. assert order_by in ('distance', '-distance') queryset = queryset.distance(field_name, point) # Sort by that field. queryset = queryset.order_by(order_by) return queryset