Пример #1
0
    def before_search(self,search_params):
        if 'extras' in search_params and 'ext_bbox' in search_params['extras'] \
            and search_params['extras']['ext_bbox']:

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            extents = bbox_query(bbox)

            if extents.count() == 0:
                # We don't need to perform the search
                search_params['abort_search'] = True
            else:
                # We'll perform the existing search but also filtering by the ids
                # of datasets within the bbox
                bbox_query_ids = [extent.package_id for extent in extents]

                q = search_params.get('q','')
                new_q = '%s AND ' % q if q else ''
                new_q += '(%s)' % ' OR '.join(['id:%s' % id for id in bbox_query_ids])

                search_params['q'] = new_q

        return search_params
Пример #2
0
    def before_search(self, search_params):
        from ckanext.spatial.lib import validate_bbox
        from ckan.lib.search import SearchError

        if search_params.get('extras', None) and search_params['extras'].get(
                'ext_bbox', None):

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            # Adjust easting values
            while (bbox['minx'] < -180):
                bbox['minx'] += 360
                bbox['maxx'] += 360
            while (bbox['minx'] > 180):
                bbox['minx'] -= 360
                bbox['maxx'] -= 360

            if self.search_backend == 'solr':
                search_params = self._params_for_solr_search(
                    bbox, search_params)
            elif self.search_backend == 'solr-spatial-field':
                search_params = self._params_for_solr_spatial_field_search(
                    bbox, search_params)
            elif self.search_backend == 'postgis':
                search_params = self._params_for_postgis_search(
                    bbox, search_params)

        return search_params
Пример #3
0
    def before_search(self, search_params):
        if 'extras' in search_params and 'ext_bbox' in search_params['extras'] \
            and search_params['extras']['ext_bbox']:

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            extents = bbox_query(bbox)

            if extents.count() == 0:
                # We don't need to perform the search
                search_params['abort_search'] = True
            else:
                # We'll perform the existing search but also filtering by the ids
                # of datasets within the bbox
                bbox_query_ids = [extent.package_id for extent in extents]

                q = search_params.get('q', '')
                new_q = '%s AND ' % q if q else ''
                new_q += '(%s)' % ' OR '.join(
                    ['id:%s' % id for id in bbox_query_ids])

                search_params['q'] = new_q

        return search_params
Пример #4
0
    def before_search(self, search_params):
        """overwrites the search parameters of the spatial extension

        we support only *solr* and *solr-spatial-field* backends.
        """
        # we treat only our own requests:
        if not request.referer or \
           not "/" in request.referer or \
           not request.referer.split("/")[-1] == 'mapsearch':
            return search_params
        # only requests with bbox
        if 'ext_bbox' not in search_params['extras'].keys():
            return search_params
        backend = config.get('ckanext.spatial.search_backend', 0)
        supported_backends = ['solr', 'solr-spatial-field']
        if not backend in supported_backends:
            raise RuntimeError('{0} is not implemented. '.format(backend) +
                               'This extension needs one of ' +
                               '{0} as the search backend'.format(
                                   supported_backends))
        scale = search_params['extras'].get('ext_scale', 'normal')
        # solr-spatial-field backend requires clipped coordinates
        raw_bbox = search_params['extras'].get('ext_bbox')
        bbox = clip_bbox(validate_bbox(raw_bbox))
        if backend == 'solr-spatial-field':
            search_params = self._params_for_solr_spatial_field_search(
                bbox, search_params, scale)
        elif backend == 'solr':
            search_params = self._params_for_solr_search(
                bbox, search_params, scale)
        return search_params
Пример #5
0
    def before_search(self, search_params):
        from ckanext.spatial.lib import  validate_bbox
        from ckan.lib.search import SearchError

        if search_params.get('extras', None) and search_params['extras'].get('ext_bbox', None):

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            # Adjust easting values
            while (bbox['minx'] < -180):
                bbox['minx'] += 360
                bbox['maxx'] += 360
            while (bbox['minx'] > 180):
                bbox['minx'] -= 360
                bbox['maxx'] -= 360

            if self.search_backend == 'solr':
                search_params = self._params_for_solr_search(bbox, search_params)
            elif self.search_backend == 'solr-spatial-field':
                search_params = self._params_for_solr_spatial_field_search(bbox, search_params)
            elif self.search_backend == 'postgis':
                search_params = self._params_for_postgis_search(bbox, search_params)

        return search_params
Пример #6
0
    def before_search(self,search_params):
        if 'extras' in search_params and 'ext_bbox' in search_params['extras'] \
            and search_params['extras']['ext_bbox']:

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            if 'sort' in search_params and search_params['sort'] == 'spatial desc':
                if search_params['q'] or search_params['fq']:
                    raise SearchError('Spatial ranking cannot be mixed with other search parameters')
                    # ...because it is too inefficient to use SOLR to filter
                    # results and return the entire set to this class and
                    # after_search do the sorting and paging.
                extents = bbox_query_ordered(bbox)
                are_no_results = not extents
                search_params['extras']['ext_rows'] = search_params['rows']
                search_params['extras']['ext_start'] = search_params['start']
                # this SOLR query needs to return no actual results since
                # they are in the wrong order anyway. We just need this SOLR
                # query to get the count and facet counts.
                rows = 0
                search_params['sort'] = None # SOLR should not sort.
                # Store the rankings of the results for this page, so for
                # after_search to construct the correctly sorted results
                rows = search_params['extras']['ext_rows'] = search_params['rows']
                start = search_params['extras']['ext_start'] = search_params['start']
                search_params['extras']['ext_spatial'] = [
                    (extent.package_id, extent.spatial_ranking) \
                    for extent in extents[start:start+rows]]
            else:
                extents = bbox_query(bbox)
                are_no_results = extents.count() == 0

            if are_no_results:
                # We don't need to perform the search
                search_params['abort_search'] = True
            else:
                # We'll perform the existing search but also filtering by the ids
                # of datasets within the bbox
                bbox_query_ids = [extent.package_id for extent in extents]

                q = search_params.get('q','').strip() or '""'
                new_q = '%s AND ' % q if q else ''
                new_q += '(%s)' % ' OR '.join(['id:%s' % id for id in bbox_query_ids])

                search_params['q'] = new_q

        return search_params
Пример #7
0
    def before_search(self, search_params):
        if search_params.get('extras', None) and search_params['extras'].get('ext_bbox', None):

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            if self.search_backend == 'solr':
                search_params = self._params_for_solr_search(bbox, search_params)
            elif self.search_backend == 'solr-spatial-field':
                search_params = self._params_for_solr_spatial_field_search(bbox, search_params)
            elif self.search_backend == 'postgis':
                search_params = self._params_for_postgis_search(bbox, search_params)

        return search_params
Пример #8
0
    def before_search(self, search_params):
        if search_params.get('extras', None) and search_params['extras'].get('ext_bbox', None):

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')

            if self.search_backend == 'solr':
                search_params = self._params_for_solr_search(bbox, search_params)
            elif self.search_backend == 'solr-spatial-field':
                search_params = self._params_for_solr_spatial_field_search(bbox, search_params)
            elif self.search_backend == 'postgis':
                search_params = self._params_for_postgis_search(bbox, search_params)

        return search_params
Пример #9
0
    def before_search(self,search_params):
        if 'extras' in search_params and 'ext_bbox' in search_params['extras'] \
            and search_params['extras']['ext_bbox']:

            bbox = validate_bbox(search_params['extras']['ext_bbox'])
            if not bbox:
                raise SearchError('Wrong bounding box provided')
            if search_params.get('sort') == 'spatial desc':
                if search_params.get('q') or search_params.get('fq'):
                    raise SearchError('Spatial ranking cannot be mixed with other search parameters')
                    # ...because it is too inefficient to use SOLR to filter
                    # results and return the entire set to this class and
                    # after_search do the sorting and paging.
                extents = bbox_query_ordered(bbox)
                are_no_results = not extents
                search_params['extras']['ext_rows'] = search_params.get('rows', 50)
                search_params['extras']['ext_start'] = search_params.get('start', 0)
                # this SOLR query needs to return no actual results since
                # they are in the wrong order anyway. We just need this SOLR
                # query to get the count and facet counts.
                rows = 0
                search_params['sort'] = None # SOLR should not sort.
                # Store the rankings of the results for this page, so for
                # after_search to construct the correctly sorted results
                rows = search_params['extras']['ext_rows'] = search_params.get('rows', 50)
                start = search_params['extras']['ext_start'] = search_params.get('start', 0)
                search_params['extras']['ext_spatial'] = [
                    (extent.package_id, extent.spatial_ranking) \
                    for extent in extents[start:start+rows]]
            else:
                extents = bbox_query(bbox)
                are_no_results = extents.count() == 0

            if are_no_results:
                # We don't need to perform the search
                search_params['abort_search'] = True
            else:
                # We'll perform the existing search but also filtering by the ids
                # of datasets within the bbox
                bbox_query_ids = [extent.package_id for extent in extents]

                q = search_params.get('q','').strip() or '""'
                new_q = '%s AND ' % q if q else ''
                new_q += '(%s)' % ' OR '.join(['id:%s' % id for id in bbox_query_ids])

                search_params['q'] = new_q

        return search_params
Пример #10
0
    def spatial_query(self):

        error_400_msg = 'Please provide a suitable bbox parameter [minx,miny,maxx,maxy]'

        if not 'bbox' in request.params:
            abort(400,error_400_msg)

        bbox = validate_bbox(request.params['bbox'])

        if not bbox:
            abort(400,error_400_msg)

        srid = get_srid(request.params.get('crs')) if 'crs' in request.params else None

        extents = bbox_query(bbox,srid)

        format = request.params.get('format','')

        return self._output_results(extents,format)
Пример #11
0
    def spatial_query(self):

        error_400_msg = 'Please provide a suitable bbox parameter [minx,miny,maxx,maxy]'

        if not 'bbox' in request.params:
            abort(400,error_400_msg)

        bbox = validate_bbox(request.params['bbox'])

        if not bbox:
            abort(400,error_400_msg)

        srid = get_srid(request.params.get('crs')) if 'crs' in request.params else None

        extents = bbox_query(bbox,srid)

        format = request.params.get('format','')

        return self._output_results(extents,format)
Пример #12
0
def spatial_query(register):
    error_400_msg = \
        'Please provide a suitable bbox parameter [minx,miny,maxx,maxy]'

    if 'bbox' not in request.args:
        return _finish_bad_request(error_400_msg)

    bbox = validate_bbox(request.params['bbox'])

    if not bbox:
        return _finish_bad_request(error_400_msg)

    srid = get_srid(request.args.get('crs')) if 'crs' in \
        request.args else None

    extents = bbox_query(bbox, srid)

    ids = [extent.package_id for extent in extents]
    output = dict(count=len(ids), results=ids)

    return _finish_ok(output)
Пример #13
0
    def spatial_query(self):
        ''' '''

        error_400_msg = \
            u'Please provide a suitable bbox parameter [minx,miny,maxx,maxy]'

        if not u'bbox' in toolkit.request.params:
            toolkit.abort(400, error_400_msg)

        bbox = validate_bbox(toolkit.request.params[u'bbox'])

        if not bbox:
            toolkit.abort(400, error_400_msg)

        srid = get_srid(toolkit.request.params.get(
            u'crs')) if u'crs' in toolkit.request.params else None

        extents = bbox_query(bbox, srid)

        format = toolkit.request.params.get(u'format', u'')

        return self._output_results(extents, format)
Пример #14
0
 def test_bad_2(self):
     res = validate_bbox('random')
     assert_equal(res, None)
Пример #15
0
 def test_bad(self):
     res = validate_bbox([-4.96, 55.70, -3.78])
     assert_equal(res, None)
Пример #16
0
 def test_list(self):
     res = validate_bbox([-4.96, 55.70, -3.78, 56.43])
     assert_equal(res, self.bbox_dict)
Пример #17
0
 def test_string(self):
     res = validate_bbox("-4.96,55.70,-3.78,56.43")
     assert_equal(res, self.bbox_dict)
 def test_bad_2(self):
     res = validate_bbox('random')
     assert_equal(res, None)
 def test_bad(self):
     res = validate_bbox([-4.96, 55.70, -3.78])
     assert_equal(res, None)
 def test_list(self):
     res = validate_bbox([-4.96, 55.70, -3.78, 56.43])
     assert_equal(res, self.bbox_dict)
 def test_string(self):
     res = validate_bbox("-4.96,55.70,-3.78,56.43")
     assert_equal(res, self.bbox_dict)
Пример #22
0
 def test_bad_2(self):
     res = validate_bbox("random")
     assert (res is None)
Пример #23
0
 def test_bad(self):
     res = validate_bbox([-4.96, 55.70, -3.78])
     assert (res is None)