Skip to content

Commit

Permalink
Added GeoJsonPagination
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jul 17, 2015
1 parent 0e315be commit 04fd1bf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
52 changes: 49 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Compatibility with DRF, Django and Python

=============== ============================ ==================== ==================================
DRF-gis version DRF version Django version Python version
**0.9.3** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
**0.9.2** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
**0.9.1** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
**0.9** **3.1.X** **1.5.x** to **1.8** **2.6**, **2.7**, **3.3**, **3.4**
Expand Down Expand Up @@ -138,9 +139,6 @@ the above example, the ``GeoFeatureModelSerializer`` will output:
If you are serializing an object list, ``GeoFeatureModelSerializer``
will create a ``FeatureCollection``:

(**NOTE:** This currenty does not work with the default pagination
serializer)

.. code-block:: javascript
{
Expand Down Expand Up @@ -294,6 +292,54 @@ be saved as Polygons. Example:
geo_field = 'geometry'
bbox_geo_field = 'bbox_geometry'
Pagination
----------

We provide a ``GeoJsonPagination`` class.

GeoJsonPagination
~~~~~~~~~~~~~~~~~

Based on ``rest_framework.pagination.PageNumberPagination``.

Code example:

.. code-block:: python
from rest_framework_gis.pagination import GeoJsonPagination
# --- other omitted imports --- #
class GeojsonLocationList(generics.ListCreateAPIView):
# -- other omitted view attributes --- #
pagination_class = GeoJsonPagination
Example result response (cut to one element only instead of 10):

.. code-block:: javascript
{
"type": "FeatureCollection",
"count": 25,
"next": "http://localhost:8000/geojson/?page=2",
"previous": null,
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
42.0,
50.0
]
},
"properties": {
"name": "test"
}
}
]
}
Filters
-------

Expand Down
22 changes: 22 additions & 0 deletions rest_framework_gis/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
try:
from collections import OrderedDict
# python 2.6
except ImportError: # pragma: no cover
from ordereddict import OrderedDict

from rest_framework import pagination
from rest_framework.response import Response


class GeoJsonPagination(pagination.PageNumberPagination):
"""
A geoJSON implementation of a pagination serializer.
"""
def get_paginated_response(self, data):
return Response(OrderedDict([
('type', 'FeatureCollection'),
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('features', data['features'])
]))
10 changes: 0 additions & 10 deletions tests/django_restframework_gis_tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'LocationGeoFeatureSerializer',
'LocationGeoFeatureSlugSerializer',
'LocationGeoFeatureFalseIDSerializer',
'PaginatedLocationGeoFeatureSerializer',
'LocatedFileGeoFeatureSerializer',
'BoxedLocationGeoFeatureSerializer',
'LocationGeoFeatureBboxSerializer',
Expand Down Expand Up @@ -68,15 +67,6 @@ class Meta:
id_field = False


class PaginatedLocationGeoFeatureSerializer(pagination.PageNumberPagination):
page_size_query_param = 'limit'
page_size = 40
max_page_size = 10000

class Meta:
object_serializer_class = LocationGeoFeatureSerializer


class LocatedFileGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer):
""" located file geo serializer """
details = serializers.HyperlinkedIdentityField(view_name='api_geojson_located_file_details')
Expand Down
11 changes: 11 additions & 0 deletions tests/django_restframework_gis_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,14 @@ class Meta:
exclude = ('geometry', )
with self.assertRaises(ImproperlyConfigured):
LocationGeoFeatureSerializer(instance=self.l1)

def test_geojson_pagination(self):
self._create_locations()
response = self.client.get(self.geojson_location_list_url)
self.assertEqual(response.data['type'], 'FeatureCollection')
self.assertEqual(len(response.data['features']), 2)
response = self.client.get('{0}?page_size=1'.format(self.geojson_location_list_url))
self.assertEqual(response.data['type'], 'FeatureCollection')
self.assertEqual(len(response.data['features']), 1)
self.assertIn('next', response.data)
self.assertIn('previous', response.data)
4 changes: 3 additions & 1 deletion tests/django_restframework_gis_tests/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import generics
from rest_framework.filters import DjangoFilterBackend
from rest_framework_gis.filters import *
from rest_framework_gis.pagination import GeoJsonPagination

from .models import *
from .serializers import *
Expand Down Expand Up @@ -28,7 +29,8 @@ class GeojsonLocationList(generics.ListCreateAPIView):
model = Location
serializer_class = LocationGeoFeatureSerializer
queryset = Location.objects.all()
pagination_class = PaginatedLocationGeoFeatureSerializer
pagination_class = GeoJsonPagination
paginate_by_param = 'page_size'

geojson_location_list = GeojsonLocationList.as_view()

Expand Down

0 comments on commit 04fd1bf

Please sign in to comment.