예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
    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)
예제 #11
0
 def to_python(self, value):
     if not value:
         return None
     if isinstance(value, str):
         return Geohash(value)
     return convert_to_point(value).geohash
예제 #12
0
 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)
예제 #13
0
 def test_tuple(self):
     point = convert_to_point((LAT, LON))
     self.assertEqual(point.latitude, LAT)
     self.assertEqual(point.longitude, LON)
예제 #14
0
 def to_python(self, value):
     if not value:
         return None
     if isinstance(value, basestring):
         return Geohash(value)
     return convert_to_point(value).geohash
예제 #15
0
 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)
예제 #16
0
 def test_tuple(self):
     point = convert_to_point((LAT, LON))
     self.assertEqual(point.latitude, LAT)
     self.assertEqual(point.longitude, LON)