def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess['field_name'] location = self._postprocess['location'] radius = self._postprocess['radius'] distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = getattr(result, field_name) distance_from_location = result_location.point.distance_from( convert_to_point(location)) setattr(result, distance_property_name, distance_from_location) if distance_from_location < radius: results.append(result) if self._postprocess.get('sort'): return iter( sorted(results, key=lambda item: getattr(item, distance_property_name))) return iter(results)
def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess.get('field_name') location = self._postprocess.get('location') radius = self._postprocess.get('radius') distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = self.__collapse_relations__(result, field_name) distance_from_location = result_location.point.distance_from( convert_to_point(location)) setattr(result, distance_property_name, distance_from_location.km) # was giving an unorderable types error comparing distance_from_location with radius. Not sure why. Works if converted to km if not radius or distance_from_location.km < radius.km: results.append(result) if self._postprocess.get('sort'): return iter( sorted(results, key=lambda item: getattr(item, distance_property_name))) return iter(results)
def _create_approx_distance_filter(self, field_name, location, radius): geohash_length = geohash_length_for_error(radius.kilometers) geohash = convert_to_point(location).geohash.trim(geohash_length) expanded = geohash.expand() filters = models.Q() for item in expanded: filters.add(models.Q(**{"%s__startswith" % field_name: item}), models.Q.OR) return filters
def test_object_with_short_properties(self): class Dummy(object): pass location = Dummy() location.lat = LAT location.lon = LON point = convert_to_point(location) self.assertEqual(point.latitude, LAT) self.assertEqual(point.longitude, LON)
def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess['field_name'] location = self._postprocess['location'] radius = self._postprocess['radius'] distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = getattr(result, field_name) distance_from_location = result_location.point.distance_from(convert_to_point(location)) setattr(result, distance_property_name, distance_from_location) if distance_from_location.kilometers < radius.kilometers: results.append(result) if self._postprocess.get('sort'): return iter(sorted(results, key=lambda item: getattr(item, distance_property_name).kilometers)) return iter(results)
def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess["field_name"] location = self._postprocess["location"] radius = self._postprocess["radius"] distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = getattr(result, field_name) distance_from_location = result_location.point.distance_from(convert_to_point(location)) setattr(result, distance_property_name, distance_from_location) if distance_from_location < radius: results.append(result) if self._postprocess.get("sort"): return iter(sorted(results, key=lambda item: getattr(item, distance_property_name))) return iter(results)
def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess.get('field_name') location = self._postprocess.get('location') radius = self._postprocess.get('radius') distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = self.__collapse_relations__(result, field_name) distance_from_location = result_location.point.distance_from(convert_to_point(location)) setattr(result, distance_property_name, distance_from_location) if not radius or distance_from_location < radius: results.append(result) if self._postprocess.get('sort'): return iter(sorted(results, key=lambda item: getattr(item, distance_property_name))) return iter(results)
def iterator(self): result_iter = super(GeoQuerySet, self).iterator() if not self._postprocess: return result_iter field_name = self._postprocess.get('field_name') location = self._postprocess.get('location') radius = self._postprocess.get('radius') distance_property_name = "%s_distance" % field_name results = [] for result in list(result_iter): result_location = self.__collapse_relations__(result, field_name) distance_from_location = result_location.point.distance_from(convert_to_point(location)) setattr(result, distance_property_name, distance_from_location.km) # was giving an unorderable types error comparing distance_from_location with radius. Not sure why. Works if converted to km if not radius or distance_from_location.km < radius.km: results.append(result) if self._postprocess.get('sort'): return iter(sorted(results, key=lambda item: getattr(item, distance_property_name))) return iter(results)
def to_python(self, value): if not value: return None if isinstance(value, str): return Geohash(value) return convert_to_point(value).geohash
def test_dict_with_short_keys(self): location = {'lat': LAT, 'lon': LON} point = convert_to_point(location) self.assertEqual(point.latitude, LAT) self.assertEqual(point.longitude, LON)
def test_tuple(self): point = convert_to_point((LAT, LON)) self.assertEqual(point.latitude, LAT) self.assertEqual(point.longitude, LON)
def to_python(self, value): if not value: return None if isinstance(value, basestring): return Geohash(value) return convert_to_point(value).geohash