Esempio n. 1
0
    def get_egrid_ident(self):
        """
        Returns a list with the matched EGRIDs for the given NBIdent and property number.

        Returns:
            pyramid.response.Response: The `getegrid` response.
        """
        params = Parameter('json' if self._is_json() else 'xml')
        identdn = self._request.matchdict.get('identdn')
        number = self._request.matchdict.get('number')
        try:
            if identdn and number:
                processor = create_processor()
                records = processor.real_estate_reader.read(
                    params,
                    **{
                        'nb_ident': identdn,
                        'number': number
                    }
                )
                response = self.__get_egrid_response__(records)
            else:
                raise HTTPBadRequest('IDENTDN and NUMBER must be defined.')
        except HTTPNoContent as err:
            response = HTTPNoContent('{}'.format(err))
        except HTTPBadRequest as err:
            response = HTTPBadRequest('{}'.format(err))
        response.extras = OerebStats(service='GetEgridIdent',
                                     params={'identdn': identdn,
                                             'number': number})
        return response
Esempio n. 2
0
    def get_egrid_coord(self):
        """
        Returns a list with the matched EGRIDs for the given coordinates.

        Returns:
            pyramid.response.Response: The `getegrid` response.
        """
        params = Parameter('json' if self._is_json() else 'xml')
        xy = self._params.get('XY')
        gnss = self._params.get('GNSS')
        try:
            if xy or gnss:
                geom_wkt = 'SRID={0};{1}'
                if xy:
                    geom_wkt = geom_wkt.format(Config.get('srid'),
                                               self.__parse_xy__(xy, buffer_dist=1.0).wkt)
                elif gnss:
                    geom_wkt = geom_wkt.format(Config.get('srid'), self.__parse_gnss__(gnss).wkt)
                processor = create_processor()
                records = processor.real_estate_reader.read(params, **{'geometry': geom_wkt})
                response = self.__get_egrid_response__(records)
            else:
                raise HTTPBadRequest('XY or GNSS must be defined.')
        except HTTPNoContent as err:
            response = HTTPNoContent('{}'.format(err))
        except HTTPBadRequest as err:
            response = HTTPBadRequest('{}'.format(err))
        response.extras = OerebStats(service='GetEgridCoord',
                                     params={'xy': xy,
                                             'gnss': gnss})
        return response
Esempio n. 3
0
def test_process():
    request = MockRequest()
    request.matchdict.update(request_matchdict)
    processor = create_processor()
    webservice = PlrWebservice(request)
    params = webservice.__validate_extract_params__()
    real_estate = processor.real_estate_reader.read(params, egrid=u'TEST')
    extract = processor.process(real_estate[0], params, 'http://test.ch')
    assert isinstance(extract, ExtractRecord)
Esempio n. 4
0
def test_properties():
    processor = create_processor()
    assert isinstance(processor.extract_reader, ExtractReader)
    assert isinstance(processor.municipality_reader, MunicipalityReader)
    assert isinstance(processor.exclusion_of_liability_reader,
                      ExclusionOfLiabilityReader)
    assert isinstance(processor.glossary_reader, GlossaryReader)
    assert isinstance(processor.plr_sources, list)
    assert isinstance(processor.real_estate_reader, RealEstateReader)
Esempio n. 5
0
def test_process_geometry_testing():
    request = MockRequest()
    request.matchdict.update(request_matchdict)
    processor = create_processor()
    webservice = PlrWebservice(request)
    params = webservice.__validate_extract_params__()
    real_estate = processor.real_estate_reader.read(params, egrid=u'TEST')
    extract = processor.process(real_estate[0], params, 'http://test.ch')
    for plr in extract.real_estate.public_law_restrictions:
        for g in plr.geometries:
            assert g._test_passed
Esempio n. 6
0
def test_processor_without_images():
    request = MockRequest()
    request.matchdict.update(request_matchdict)
    request.params.update({'LANG': 'de'})
    processor = create_processor()
    webservice = PlrWebservice(request)
    params = webservice.__validate_extract_params__()
    real_estate = processor.real_estate_reader.read(params, egrid=u'TEST')
    extract = processor.process(real_estate[0], params, 'http://test.ch')
    assert extract.real_estate.plan_for_land_register.image is None
    for plr in extract.real_estate.public_law_restrictions:
        assert plr.view_service.image is None
Esempio n. 7
0
def test_filter_published_documents():
    request = MockRequest()
    request.matchdict.update(request_matchdict)
    processor = create_processor()
    webservice = PlrWebservice(request)
    params = webservice.__validate_extract_params__()
    real_estate = processor.real_estate_reader.read(params, egrid=u'TEST')
    extract = processor.process(real_estate[0], params, 'http://test.ch')
    for plr in extract.real_estate.public_law_restrictions:
        if plr.theme.code == u'MotorwaysBuildingLines':
            assert len(plr.documents) == 1
            assert len(plr.documents[0].references) == 1
Esempio n. 8
0
    def get_capabilities(self):
        """
        Returns the capabilities of this service.

        Returns:
            pyramid.response.Response: The `capabilities` response.
        """

        params = Parameter('json' if self._is_json() else 'xml')

        supported_languages = Config.get_language()
        themes = list()
        for theme in Config.get_themes():
            text = list()
            for lang in theme.text:
                if lang in supported_languages:
                    text.append({
                        'Language': lang,
                        'Text': theme.text[lang]
                    })
            themes.append({
                'Code': theme.code,
                'Text': text
            })
        processor = create_processor()
        capabilities = {
            u'GetCapabilitiesResponse': {
                u'topic': themes,
                u'municipality': [record.fosnr for record in processor.municipality_reader.read(params)],
                u'flavour': Config.get_flavour(),
                u'language': supported_languages,
                u'crs': [Config.get_crs()]
            }
        }

        # Try - catch for backward compatibility with old specification.
        try:
            output_format = self.__validate_format_param__(['xml', 'json'])
        except HTTPBadRequest:
            output_format = None
            log.warning('Deprecated way to specify the format. Use "/capabilities/{format}" instead')
        renderer_name = 'json' if output_format == 'json' or self._is_json() else 'pyramid_oereb_capabilities_xml'  # noqa: E501
        response = render_to_response(renderer_name, capabilities, request=self._request)
        if self._is_json():
            response.content_type = 'application/json; charset=UTF-8'
        response.extras = OerebStats(service='GetCapabilities', output_format=output_format)
        return response
Esempio n. 9
0
    def get_egrid_address(self):
        """
        Returns a list with the matched EGRIDs for the given postal address.

        Returns:
            pyramid.response.Response: The `getegrid` response.
        """
        params = Parameter('json' if self._is_json() else 'xml')
        postalcode = self._request.matchdict.get('postalcode')
        localisation = self._request.matchdict.get('localisation')
        number = self._request.matchdict.get('number')
        try:
            if postalcode and localisation and number:
                reader = AddressReader(
                    Config.get_address_config().get('source').get('class'),
                    **Config.get_address_config().get('source').get('params')
                )
                addresses = reader.read(params, localisation, int(postalcode), number)
                if len(addresses) == 0:
                    raise HTTPNoContent()
                geometry = 'SRID={srid};{wkt}'.format(srid=Config.get('srid'),
                                                      wkt=addresses[0].geom.wkt)
                processor = create_processor()
                records = processor.real_estate_reader.read(params, **{'geometry': geometry})
                response = self.__get_egrid_response__(records)
            else:
                raise HTTPBadRequest('POSTALCODE, LOCALISATION and NUMBER must be defined.')
        except HTTPNoContent as err:
            response = HTTPNoContent('{}'.format(err))
        except HTTPBadRequest as err:
            response = HTTPBadRequest('{}'.format(err))
        response.extras = OerebStats(service='GetEgridAddress',
                                     params={'postalcode': postalcode,
                                             'localisation': localisation,
                                             'number': number})
        return response
Esempio n. 10
0
    def get_extract_by_id(self):
        """
        Returns the extract in the specified format and flavour.

        Returns:
            pyramid.response.Response: The `extract` response.
        """
        start_time = timer()
        log.debug("get_extract_by_id() start")
        try:
            params = self.__validate_extract_params__()
            processor = create_processor()
            # read the real estate from configured source by the passed parameters
            real_estate_reader = processor.real_estate_reader
            if params.egrid:
                real_estate_records = real_estate_reader.read(params, egrid=params.egrid)
            elif params.identdn and params.number:
                real_estate_records = real_estate_reader.read(
                    params,
                    nb_ident=params.identdn,
                    number=params.number
                )
            else:
                raise HTTPBadRequest("Missing required argument")
            # check if result is strictly one (we queried with primary keys)
            if len(real_estate_records) == 1:
                extract = processor.process(
                    real_estate_records[0],
                    params,
                    self._request.route_url('{0}/sld'.format(route_prefix))
                )
                if params.format == 'json':
                    log.debug("get_extract_by_id() calling json")
                    response = render_to_response(
                        'pyramid_oereb_extract_json',
                        (extract, params),
                        request=self._request
                    )
                elif params.format == 'xml':
                    log.debug("get_extract_by_id() calling xml")
                    response = render_to_response(
                        'pyramid_oereb_extract_xml',
                        (extract, params),
                        request=self._request
                    )
                elif params.format == 'pdf':
                    log.debug("get_extract_by_id() calling pdf")
                    response = render_to_response(
                        'pyramid_oereb_extract_print',
                        (extract, params),
                        request=self._request
                    )
                else:
                    raise HTTPBadRequest("The format '{}' is wrong".format(params.format))
                end_time = timer()
                log.debug("DONE with extract, time spent: {} seconds".format(end_time - start_time))
            else:
                raise HTTPNoContent("No real estate found")
        except HTTPNoContent as err:
            response = HTTPNoContent('{}'.format(err))
        except HTTPBadRequest as err:
            response = HTTPBadRequest('{}'.format(err))
        try:
            response.extras = OerebStats(service='GetExtractById',
                                         output_format=params.format,
                                         params=vars(params))
        except UnboundLocalError:
            response.extras = OerebStats(service='GetExtractById', params={'error': response.message})
        except Exception:
            # if params is not set we get UnboundLocalError
            # or we could get ValueError
            # in any case, the logging should never crash the response delivery
            try:
                response.extras = OerebStats(service='GetExtractById', params={'error': response.message})
            except AttributeError:
                response.extras = OerebStats(service='GetExtractById')
        return response