1

I managed to learn the fundamentals of Django, GeoDjango and know OpenLayers for a while. Now I realize a project with a lot of objects with point geometries. To make the user map fast and responsive, I like to transfer only the points that are in the bounding box of the current view.

OpenLayers has on part of the solution on clientside solution with the BBOX strategy (new OpenLayers.Strategy.BBOX()) which appends the extends to the data URL e.g. /api/node?bbox=13.898900146484,51.04243240501,13.341099853516,51.117559096132

Now my question is, how I can deal with that on server side, without needing to reimplement attribute parsing and filtering on my own?
If there is no ready solution, might anybody please give me a rough snippet on what to do on django side?

1

Without needing to write any code, i'm not sure. But there are many solutions that don't involve writing much code. Im a big fan of Django-Rest-Framework-GIS, it makes writing api's that return GeoJSON very quick. It provides an InBBOX filter that can handle parsing BBOXes in the standard format, or you can provide your own get_queryset method to handle filtering.

def get_queryset(self):
    queryset = PointFeatureObject.objects

    bbox = self.request.GET.get('bbox')
    if bbox is not None and len(bbox) > 0:
        try:
            bbox = Polygon.from_bbox(bbox.split(','))
            queryset = queryset.filter(geometry__within=bbox)
        except Exception, e:
            logging.error("error" + str(e))

    return queryset

Then you will need a serializer

from rest_framework_gis import serializers as geo_serializers
from models import PointObjectFeature

class PointObjectFeatureSerializer(geo_serializers.GeoFeatureModelSerializer):
    class Meta:
        model = PointObjectFeature
        geo_field = 'geometry'

And you'll need a view, which is well covered in The Django Rest Framework Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.