| 1 |
.. _geospatial-label: |
| 2 |
|
| 3 |
GEO spatial locations |
| 4 |
===================== |
| 5 |
|
| 6 |
Some search backends support geo spatial searching. In order to take advantage of this we |
| 7 |
have the :class:`drf_haystack.filters.HaystackGEOSpatialFilter`. |
| 8 |
|
| 9 |
.. note:: |
| 10 |
|
| 11 |
The ``HaystackGEOSpatialFilter`` depends on ``geopy`` and ``libgeos``. Make sure to install these |
| 12 |
libraries in order to use this filter. |
| 13 |
|
| 14 |
.. code-block:: none |
| 15 |
|
| 16 |
$ pip install geopy |
| 17 |
$ apt-get install libgeos-c1 (for debian based linux distros) |
| 18 |
or |
| 19 |
$ brew install geos (for homebrew on OS X) |
| 20 |
|
| 21 |
|
| 22 |
The geospatial filter is somewhat special, and for the time being, relies on a few assumptions. |
| 23 |
|
| 24 |
#. The index model **must** to have a ``LocationField`` (See :ref:`search-index-example-label` for example). |
| 25 |
If your ``LocationField`` is named something other than ``coordinates``, subclass the ``HaystackGEOSpatialFilter`` |
| 26 |
and make sure to set the :attr:`drf_haystack.filters.HaystackGEOSpatialFilter.point_field` to the name of the field. |
| 27 |
#. The query **must** contain a ``unit`` parameter where the unit is a valid ``UNIT`` in the ``django.contrib.gis.measure.Distance`` class. |
| 28 |
#. The query **must** contain a ``from`` parameter which is a comma separated longitude and latitude value. |
| 29 |
|
| 30 |
You may also change the query param ``from`` by defining ``DRF_HAYSTACK_SPATIAL_QUERY_PARAM`` on your settings. |
| 31 |
|
| 32 |
**Example Geospatial view** |
| 33 |
|
| 34 |
.. code-block:: python |
| 35 |
|
| 36 |
class DistanceSerializer(serializers.Serializer): |
| 37 |
m = serializers.FloatField() |
| 38 |
km = serializers.FloatField() |
| 39 |
|
| 40 |
|
| 41 |
class LocationSerializer(HaystackSerializer): |
| 42 |
|
| 43 |
distance = SerializerMethodField() |
| 44 |
|
| 45 |
class Meta: |
| 46 |
index_classes = [LocationIndex] |
| 47 |
fields = ["address", "city", "zip_code"] |
| 48 |
|
| 49 |
def get_distance(self, obj): |
| 50 |
if hasattr(obj, "distance"): |
| 51 |
return DistanceSerializer(obj.distance, many=False).data |
| 52 |
|
| 53 |
|
| 54 |
class LocationGeoSearchViewSet(HaystackViewSet): |
| 55 |
|
| 56 |
index_models = [Location] |
| 57 |
serializer_class = LocationSerializer |
| 58 |
filter_backends = [HaystackGEOSpatialFilter] |
| 59 |
|
| 60 |
|
| 61 |
**Example subclassing the HaystackGEOSpatialFilter** |
| 62 |
|
| 63 |
Assuming that your ``LocationField`` is named ``location``. |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
from drf_haystack.filters import HaystackGEOSpatialFilter |
| 68 |
|
| 69 |
class CustomHaystackGEOSpatialFilter(HaystackGEOSpatialFilter): |
| 70 |
point_field = 'location' |
| 71 |
|
| 72 |
|
| 73 |
class LocationGeoSearchViewSet(HaystackViewSet): |
| 74 |
|
| 75 |
index_models = [Location] |
| 76 |
serializer_class = LocationSerializer |
| 77 |
filter_backends = [CustomHaystackGEOSpatialFilter] |
| 78 |
|
| 79 |
Assuming the above code works as it should, we would be able to do queries like this: |
| 80 |
|
| 81 |
.. code-block:: none |
| 82 |
|
| 83 |
/api/v1/search/?zip_code=0351&km=10&from=59.744076,10.152045 |
| 84 |
|
| 85 |
|
| 86 |
The above query would return all entries with zip_code 0351 within 10 kilometers |
| 87 |
from the location with latitude 59.744076 and longitude 10.152045. |