Example #1
0
def get_symbol(request):
    """
    Returns the symbol for the requested theme and type code from database.

    Args:
        request (pyramid.request.Request): The request containing the codes as matchdict parameters.

    Returns:
        pyramid.response.Response: The generated response object.
    """

    theme_code = request.matchdict.get('theme_code')
    view_service_id = request.matchdict.get('view_service_id')
    type_code = request.matchdict.get('type_code')

    plr = None
    for p in Config.get('plrs'):
        if str(p.get('code')).lower() == str(theme_code).lower():
            plr = p
            break

    if plr is None:
        raise HTTPNotFound('No theme with code {}.'.format(theme_code))

    session = database_adapter.get_session(
        plr.get('source').get('params').get('db_connection'))

    try:
        config_parser = StandardThemeConfigParser(**plr)
        models = config_parser.get_models()
        model = models.LegendEntry
        legend_entry = session.query(model).filter(
            cast(model.type_code, Text) == cast(type_code, Text)).filter(
                model.view_service_id == view_service_id).first()
        if legend_entry:
            symbol = getattr(legend_entry, 'symbol', None)
            if symbol:
                response = request.response
                response.status_int = 200
                if isinstance(symbol, str):
                    response.body = b64.decode(symbol)
                if isinstance(symbol, bytes):
                    response.body = b64.decode(
                        binascii.b2a_base64(symbol).decode('ascii'))
                response.content_type = ImageRecord.get_mimetype(
                    bytearray(response.body))
                return response
        raise HTTPNotFound()

    finally:
        session.close()
Example #2
0
    def from_db_to_plr_record(self, params, public_law_restriction_from_db,
                              legend_entries_from_db):
        thresholds = self._plr_info.get('thresholds')
        min_length = thresholds.get('length').get('limit')
        length_unit = thresholds.get('length').get('unit')
        min_area = thresholds.get('area').get('limit')
        area_unit = thresholds.get('area').get('unit')
        legend_entry_record = self.from_db_to_legend_entry_record(
            public_law_restriction_from_db.legend_entry)
        legend_entry_records = self.from_db_to_legend_entry_records(
            legend_entries_from_db, legend_entry_record.theme.sub_code)
        symbol = ImageRecord(
            b64.decode(
                binascii.b2a_base64(
                    public_law_restriction_from_db.legend_entry.symbol).decode(
                        'ascii')))
        view_service_record = self.from_db_to_view_service_record(
            public_law_restriction_from_db.view_service, legend_entry_records,
            self._plr_info.get('code'))
        theme = Config.get_theme_by_code_sub_code(
            public_law_restriction_from_db.legend_entry.theme,
            public_law_restriction_from_db.legend_entry.sub_theme)
        document_records = eliminate_duplicated_document_records(
            theme.document_records,
            self.get_document_records(params, public_law_restriction_from_db))
        geometry_records = self.from_db_to_geometry_records(
            public_law_restriction_from_db.geometries)
        law_status = Config.get_law_status_by_data_code(
            self._plr_info.get('code'),
            public_law_restriction_from_db.law_status)
        sub_theme_legend_entry_record = \
            legend_entry_record.theme if legend_entry_record.theme.sub_code else None
        plr_record = self._plr_record_class(
            Config.get_theme_by_code_sub_code(legend_entry_record.theme.code),
            legend_entry_record,
            law_status,
            public_law_restriction_from_db.published_from,
            public_law_restriction_from_db.published_until,
            self.from_db_to_office_record(
                public_law_restriction_from_db.responsible_office),
            symbol,
            view_service_record,
            geometry_records,
            sub_theme=sub_theme_legend_entry_record,
            type_code=public_law_restriction_from_db.legend_entry.type_code,
            type_code_list=public_law_restriction_from_db.legend_entry.
            type_code_list,
            documents=document_records,
            min_area=min_area,
            min_length=min_length,
            area_unit=area_unit,
            length_unit=length_unit,
            view_service_id=public_law_restriction_from_db.view_service.t_id)

        return plr_record
Example #3
0
    def __init__(self, code, image_dict):

        if not isinstance(code, str):
            warnings.warn('Type of "code" should be "str"')
        if not isinstance(image_dict, dict):
            warnings.warn('Type of "image_dict" should be "dict"')

        self.code = code
        self.image_dict = {}
        for key in image_dict.keys():
            self.image_dict[key] = ImageRecord(b64.decode(image_dict[key]))
Example #4
0
 def from_db_to_legend_entry_record(self, legend_entry_from_db):
     theme = Config.get_theme_by_code_sub_code(
         legend_entry_from_db.theme, legend_entry_from_db.sub_theme)
     legend_entry_record = self._legend_entry_record_class(
         ImageRecord(b64.decode(legend_entry_from_db.symbol)),
         legend_entry_from_db.legend_text,
         legend_entry_from_db.type_code,
         legend_entry_from_db.type_code_list,
         theme,
         view_service_id=legend_entry_from_db.view_service_id,
     )
     return legend_entry_record
Example #5
0
 def from_db_to_legend_entry_record(self, legend_entry_from_db):
     theme = Config.get_theme_by_code_sub_code(
         legend_entry_from_db.theme, legend_entry_from_db.sub_theme)
     legend_entry_record = self._legend_entry_record_class(
         ImageRecord(
             b64.decode(
                 binascii.b2a_base64(
                     legend_entry_from_db.symbol).decode('ascii'))),
         from_multilingual_text_to_dict(
             de=legend_entry_from_db.legend_text_de,
             fr=legend_entry_from_db.legend_text_fr,
             it=legend_entry_from_db.legend_text_it,
             rm=legend_entry_from_db.legend_text_rm,
             en=legend_entry_from_db.legend_text_en),
         legend_entry_from_db.type_code,
         legend_entry_from_db.type_code_list,
         theme,
         view_service_id=legend_entry_from_db.view_service_id,
     )
     return legend_entry_record