diff options
Diffstat (limited to 'chimere/views.py')
| -rw-r--r-- | chimere/views.py | 30 | 
1 files changed, 25 insertions, 5 deletions
| diff --git a/chimere/views.py b/chimere/views.py index 40db95a..6e0eb40 100644 --- a/chimere/views.py +++ b/chimere/views.py @@ -29,7 +29,7 @@ from itertools import groupby  import re  from django.conf import settings -from django.contrib.gis.geos import GEOSGeometry +from django.contrib.gis.geos import GEOSGeometry, Polygon as GEOSPolygon  from django.contrib.gis.gdal.error import OGRException  from django.contrib.gis.measure import D  from django.contrib.sites.models import get_current_site @@ -750,7 +750,8 @@ def checkDate(q):  def _getGeoObjects(area_name, category_ids, status='A', getjson=True,                     aggregated=False, item_types=('Marker', 'Route', -                                                 'Polygon')): +                                                 'Polygon'), +                   bounding_box=None):      '''      Get geo objects      ''' @@ -761,7 +762,10 @@ def _getGeoObjects(area_name, category_ids, status='A', getjson=True,      if 'Marker' in item_types:          try:              q = checkDate(Q(status__in=status, categories__in=category_ids)) -            query = Marker.objects.filter(q).distinct('pk').order_by('-pk') +            query = Marker.objects.filter(q) +            if bounding_box: +                query = query.filter(point__contained=bounding_box) +            query = query.distinct('pk').order_by('-pk')          except:              return empty @@ -776,7 +780,10 @@ def _getGeoObjects(area_name, category_ids, status='A', getjson=True,      if 'Polygon' in item_types:          try:              q = checkDate(Q(status__in=status, categories__in=category_ids)) -            query = Polygon.objects.filter(q).distinct('pk').order_by('-pk') +            query = Polygon.objects.filter(q) +            if bounding_box: +                query = query.filter(polygon__contained=bounding_box) +            query = query.distinct('pk').order_by('-pk')          except:              return empty @@ -785,6 +792,7 @@ def _getGeoObjects(area_name, category_ids, status='A', getjson=True,              query = AggregatedPolygon.objects.filter(                  status__in=status, subcategory__in=category_ids).order_by(                  'subcategory', '-pk') +            # no bounding box filter              if getjson:                  for poly in query.all():                      items.append(json.loads(poly.getGeoJSON())) @@ -800,6 +808,7 @@ def _getGeoObjects(area_name, category_ids, status='A', getjson=True,      # routes      if 'Route' in item_types: +        # TODO: manage non aggregated and bounding box in non-aggregated          query = AggregatedRoute.objects.filter(              status__in=status, subcategory__in=category_ids).order_by(              'subcategory', '-pk') @@ -844,7 +853,18 @@ def getGeoObjects(request, area_name, category_ids, status):      status = status.split('_')      category_ids = unicode(category_ids).split('_') -    jsons = _getGeoObjects(area_name, category_ids, status) +    bounding_box = [] +    for attr in ['min_lon', 'min_lat', 'max_lon', 'max_lat']: +        value = request.GET.get(attr, None) +        if not value: +            bounding_box = None +            break +        bounding_box.append(value) +    if bounding_box: +        bounding_box = GEOSPolygon.from_bbox(bounding_box) + +    jsons = _getGeoObjects(area_name, category_ids, status, +                           bounding_box=bounding_box)      if not jsons:          return HttpResponse("[]", content_type="application/json")      data = json.dumps({"type": "FeatureCollection", "features": jsons}) | 
