示例#1
0
    def _get_distance(self):
        from haystack.utils.geo import Distance

        if self._distance is None:
            # We didn't get it from the backend & we haven't tried calculating
            # it yet. Check if geopy is available to do it the "slow" way
            # (even though slow meant 100 distance calculations in 0.004 seconds
            # in my testing).
            if geopy_distance is None:
                raise SpatialError("The backend doesn't have 'DISTANCE_AVAILABLE' enabled & the 'geopy' library could not be imported, so distance information is not available.")

            if not self._point_of_origin:
                raise SpatialError("The original point is not available.")

            if not hasattr(self, self._point_of_origin['field']):
                raise SpatialError("The field '%s' was not included in search results, so the distance could not be calculated." % self._point_of_origin['field'])

            po_lng, po_lat = self._point_of_origin['point'].get_coords()
            location_field = getattr(self, self._point_of_origin['field'])

            if location_field is None:
                return None

            lf_lng, lf_lat  = location_field.get_coords()
            self._distance = Distance(km=geopy_distance.distance((po_lat, po_lng), (lf_lat, lf_lng)).km)

        # We've either already calculated it or the backend returned it, so
        # let's use that.
        return self._distance
示例#2
0
def ensure_geometry(geom):
    """
    Makes sure the parameter passed in looks like a GEOS ``GEOSGeometry``.
    """
    if not hasattr(geom, 'geom_type'):
        raise SpatialError("Point '%s' doesn't appear to be a GEOS geometry." % geom)

    return geom
示例#3
0
def ensure_polygon(geom):
    """
    Makes sure the parameter passed in looks like a GEOS ``Polygon``.
    """
    ensure_geometry(geom)

    if geom.geom_type != 'Polygon':
        raise SpatialError("Provided geometry '%s' is not a 'Polygon'." % geom)
    return geom
示例#4
0
def ensure_multipoint(geom):
    """
    Makes sure the parameter passed in looks like a GEOS ``MultiPoint``.
    """
    ensure_geometry(geom)

    if geom.geom_type != 'MultiPoint':
        raise SpatialError("Provided geometry '%s' is not a 'MultiPoint'." % geom)

    return geom
示例#5
0
def ensure_distance(dist):
    """
    Makes sure the parameter passed in is a 'Distance' object.
    """
    try:
        # Since we mostly only care about the ``.km`` attribute, make sure
        # it's there.
        km = dist.km
    except AttributeError:
        raise SpatialError("'%s' does not appear to be a 'Distance' object." % dist)

    return dist
示例#6
0
def ensure_relation(relation):
    if relation not in VALID_RELATIONS:
        raise SpatialError("'{}' must be one of {}".format(
            relation, VALID_RELATIONS))
    return relation