def build_query(self, **filters):
        """
        Build queries for geo spatial filtering.

        Expected query parameters are:
         - a `unit=value` parameter where the unit is a valid UNIT in the
           `django.contrib.gis.measure.Distance` class.
         - `from` which must be a comma separated latitude and longitude.

         Example query:
             /api/v1/search/?km=10&from=59.744076,10.152045

             Will perform a `dwithin` query within 10 km from the point
             with latitude 59.744076 and longitude 10.152045.
        """

        applicable_filters = None

        filters = dict((k, filters[k]) for k in chain(self.D.UNITS.keys(),
                                                      [constants.DRF_HAYSTACK_SPATIAL_QUERY_PARAM]) if k in filters)
        distance = dict((k, v) for k, v in filters.items() if k in self.D.UNITS.keys())

        try:
            latitude, longitude = map(float, self.tokenize(filters[constants.DRF_HAYSTACK_SPATIAL_QUERY_PARAM],
                                                           self.view.lookup_sep))
            point = self.Point(longitude, latitude, srid=constants.GEO_SRID)
        except ValueError:
            raise ValueError("Cannot convert `from=latitude,longitude` query parameter to "
                             "float values. Make sure to provide numerical values only!")
        except KeyError:
            # If the user has not provided any `from` query string parameter,
            # just return.
            pass
        else:
            for unit in distance.keys():
                if not len(distance[unit]) == 1:
                    raise ValueError("Each unit must have exactly one value.")
                distance[unit] = float(distance[unit][0])

            if point and distance:
                applicable_filters = {
                    "dwithin": {
                        "field": self.backend.point_field,
                        "point": point,
                        "distance": self.D(**distance)
                    },
                    "distance": {
                        "field": self.backend.point_field,
                        "point": point
                    }
                }

        return applicable_filters
Build queries for geo spatial filtering.

        Expected query parameters are:
         - a `unit=value` parameter where the unit is a valid UNIT in the
           `django.contrib.gis.measure.Distance` class.
         - `from` which must be a comma separated latitude and longitude.

         Example query:
             /api/v1/search/?km=10&from=59.744076,10.152045

             Will perform a `dwithin` query within 10 km from the point
             with latitude 59.744076 and longitude 10.152045.