Skip to content

Commit

Permalink
Merge 1a72ae7 into 4e84a64
Browse files Browse the repository at this point in the history
  • Loading branch information
philipn committed Jan 17, 2014
2 parents 4e84a64 + 1a72ae7 commit bf52224
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,30 @@ class LocationSerializer(GeoFeatureModelSerializer):
Filters
-------

Provides a `InBBOXFilter`, which is a subclass of DRF `BaseFilterBackend`.
Filters a queryset to only those instances within a certain bounding box.
We provide a `GeometryFilter` field as well as a `GeometryFilterSet` for usage with `django_filter`. You simply provide, in the query string, one of the textual types supported by `GEOSGeometry`. By default, this includes WKT, HEXEWKB, WKB (in a buffer), and GeoJSON.

### GeometryFilter

```python
from rest_framework_gis.filters import GeoFilterSet

class RegionFilter(GeoFilterSet):
slug = filters.CharFilter(name='slug', lookup_type='istartswith')
contains_geom = filters.GeometryFilter(name='geom', lookup_type='contains')

class Meta:
model = Region
```

We can then filter in the URL, using GeoJSON, and we will perform a `__contains` geometry lookup, e.g. `/region/?contains_geom={ "type": "Point", "coordinates": [ -123.26436996459961, 44.564178042345375 ] }`.

### GeoFilterSet

The `GeoFilterSet` provides a `django_filter` compatible `FilterSet` that will automatically create `GeometryFilters` for `GeometryFields`.

### InBBOXFilter

Provides a `InBBOXFilter`, which is a subclass of DRF `BaseFilterBackend`. Filters a queryset to only those instances within a certain bounding box.


Running the tests
Expand Down Expand Up @@ -225,4 +247,4 @@ Contributing
9. Send pull request


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/djangonauts/django-rest-framework-gis/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/djangonauts/django-rest-framework-gis/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
15 changes: 12 additions & 3 deletions rest_framework_gis/filters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from rest_framework.filters import BaseFilterBackend
from rest_framework.exceptions import ParseError

from django.db.models import Q
from django.contrib.gis.geos import Polygon
from django.contrib.gis import forms

from rest_framework.filters import BaseFilterBackend
from rest_framework.exceptions import ParseError
from django_filters import Filter


class InBBOXFilter(BaseFilterBackend):
Expand Down Expand Up @@ -38,3 +40,10 @@ def filter_queryset(self, request, queryset, view):
if not bbox:
return queryset
return queryset.filter(Q(**{'%s__%s' % (filter_field, geoDjango_filter): bbox}))


class GeometryFilter(Filter):
field_class = forms.GeometryField


from .filterset import *

0 comments on commit bf52224

Please sign in to comment.