2011-09-29 16:41:45 +02:00
|
|
|
from ckan.lib.base import request, config, abort
|
2011-05-26 16:34:21 +02:00
|
|
|
from ckan.controllers.api import ApiController as BaseApiController
|
2011-11-14 19:00:56 +01:00
|
|
|
from ckan.model import Session, Package
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
from ckanext.spatial.lib import get_srid, validate_bbox, bbox_query
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-09-29 16:41:45 +02:00
|
|
|
from geoalchemy import WKTSpatialElement, functions
|
2011-04-11 19:04:28 +02:00
|
|
|
|
|
|
|
class ApiController(BaseApiController):
|
2011-09-29 16:41:45 +02:00
|
|
|
|
2011-04-11 19:04:28 +02:00
|
|
|
def spatial_query(self):
|
2011-09-29 16:41:45 +02:00
|
|
|
|
|
|
|
error_400_msg = 'Please provide a suitable bbox parameter [minx,miny,maxx,maxy]'
|
|
|
|
|
2011-04-11 19:04:28 +02:00
|
|
|
if not 'bbox' in request.params:
|
2011-09-29 16:41:45 +02:00
|
|
|
abort(400,error_400_msg)
|
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
bbox = validate_bbox(request.params['bbox'])
|
2011-09-29 16:41:45 +02:00
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
if not bbox:
|
2011-09-29 16:41:45 +02:00
|
|
|
abort(400,error_400_msg)
|
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
srid = get_srid(request.params.get('crs')) if 'crs' in request.params else None
|
2011-09-29 16:41:45 +02:00
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
extents = bbox_query(bbox,srid)
|
2011-09-29 16:41:45 +02:00
|
|
|
|
2011-12-13 18:40:47 +01:00
|
|
|
format = request.params.get('format','')
|
|
|
|
|
|
|
|
return self._output_results(extents,format)
|
|
|
|
|
|
|
|
def _output_results(self,extents,format=None):
|
2011-11-14 19:00:56 +01:00
|
|
|
|
2011-09-29 16:41:45 +02:00
|
|
|
ids = [extent.package_id for extent in extents]
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-09-29 16:41:45 +02:00
|
|
|
output = dict(count=len(ids),results=ids)
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-09-29 16:41:45 +02:00
|
|
|
return self._finish_ok(output)
|