Exemplo n.º 1
0
    def layerConfig(self, params):
        config = {}
        translate = params.translate
        settings = params.request.registry.settings
        wmsHost = settings['wmshost']
        defaultResolution = 0.5
        for k in self.__dict__.keys():
            val = self.__dict__[k]
            if not k.startswith("_") and not k.startswith('geojsonUrl') and \
                    val is not None and k not in ('staging', 'srid'):
                if k == 'maps':
                    config['topics'] = val
                elif k == 'layerBodId':
                    config['label'] = translate(val)
                elif k == 'attribution':
                    config[k] = translate(val)
                elif k == 'tilematrix_resolution_max':
                    if val != defaultResolution and \
                            self.__dict__['srid'] != u'4326':
                        config[
                            'resolutions'] = self._getResolutionsFromMatrixSet(
                                val, params.srid)
                elif k == 'extent':  # Used for the shop, are still in lv03
                    if val and params.srid == 2056:
                        config['extent'] = shift_to(val, 2056)
                    else:
                        config['extent'] = val
                else:
                    config[k] = val

        if config['type'] in ('wmts', 'aggregate', 'geojson'):
            del config['singleTile']

        if not config['timeEnabled']:
            del config['timeBehaviour']

        if config['type'] == 'wms':
            config['wmsUrl'] = 'https://%s' % wmsHost
        elif config['type'] == 'geojson':
            api_url = params.request.registry.settings['api_url']
            config['styleUrl'] = make_agnostic(api_url +
                                               '/static/vectorStyles/' +
                                               self.layerBodId + '.json')
            config['geojsonUrl'] = self._getGeoJsonUrl(params.lang)
            if 'format' in config:
                del config['format']
        # sublayers don't have attributions
        if 'attribution' in config:
            config['attributionUrl'] = translate(self.__dict__['attribution'] +
                                                 '.url')

        # adding __queryable_attributes__ if they have them
        models = models_from_bodid(self.layerBodId)
        if models is not None:
            queryable_attributes = get_models_attributes_keys(
                models, params.lang, True)
            if len(queryable_attributes) > 0:
                config['queryableAttributes'] = queryable_attributes

        return {self.layerBodId: config}
Exemplo n.º 2
0
    def layerConfig(self, params):
        config = {}
        translate = params.translate
        settings = params.request.registry.settings
        wmsHost = settings['wmshost']
        defaultResolution = 0.5
        for k in self.__dict__.keys():
            val = self.__dict__[k]
            if not k.startswith("_") and not k.startswith('geojsonUrl') and \
                    val is not None and k not in ('staging', 'srid'):
                if k == 'maps':
                    config['topics'] = val
                elif k == 'layerBodId':
                    config['label'] = translate(val)
                elif k == 'attribution':
                    config[k] = translate(val)
                elif k == 'tilematrix_resolution_max':
                    if val != defaultResolution and \
                            self.__dict__['srid'] != u'4326':
                        config['resolutions'] = self._getResolutionsFromMatrixSet(
                            val, params.srid)
                elif k == 'extent':  # Used for the shop, are still in lv03
                    if val and params.srid == 2056:
                        config['extent'] = shift_to(val, 2056)
                    else:
                        config['extent'] = val
                else:
                    config[k] = val

        if config['type'] in ('wmts', 'aggregate', 'geojson'):
            del config['singleTile']

        if not config['timeEnabled']:
            del config['timeBehaviour']

        if config['type'] == 'wms':
            config['wmsUrl'] = 'https://%s' % wmsHost
        elif config['type'] == 'geojson':
            api_url = params.request.registry.settings['api_url']
            config['styleUrl'] = make_agnostic(
                api_url + '/static/vectorStyles/' + self.layerBodId + '.json')
            config['geojsonUrl'] = self._getGeoJsonUrl(params.lang)
            if 'format' in config:
                del config['format']
        # sublayers don't have attributions
        if 'attribution' in config:
            config['attributionUrl'] = translate(self.__dict__['attribution'] + '.url')

        # adding __queryable_attributes__ if they have them
        models = models_from_bodid(self.layerBodId)
        if models is not None:
            queryable_attributes = get_models_attributes_keys(models, params.lang, True)
            if len(queryable_attributes) > 0:
                config['queryableAttributes'] = queryable_attributes

        return {self.layerBodId: config}
Exemplo n.º 3
0
def feature_attributes(request):
    ''' This service is used to expose the
    attributes of vector layers. '''
    params = BaseLayersValidation(request)
    layerId = request.matchdict.get('layerId')
    models = models_from_bodid(layerId, srid=params.srid)
    # Models for the same layer have the same attributes
    if models is None:
        raise exc.HTTPBadRequest('No Vector Table was found for %s' % layerId)

    # Take into account all models and remove duplicated keys
    attributes = get_models_attributes_keys(models, params.lang, False)
    trackAttributesNames = []
    fields = []

    def insert_value_at(field, attrName, value):
        if field['name'] == attrName:
            if len(field['values']) < MAX_ATTRIBUTES_VALUES and \
               value not in field['values']:
                field['values'].append(value)
                field['values'].sort()
        return field

    for model in models:
        query = params.request.db.query(model)
        query = query.limit(SAMPLE_SIZE)

        for rowIndex, row in enumerate(query):
            # attrName as defined in the model
            for attrIndex, attrName in enumerate(attributes):
                featureAttrs = row.get_attributes(exclude_pkey=False)
                if attrName not in trackAttributesNames and \
                   attrName in featureAttrs:
                    fieldType = _find_type(model(), attrName)
                    fields.append({
                        'name':
                        attrName,
                        'type':
                        str(fieldType),
                        'alias':
                        params.translate("%s.%s" % (layerId, attrName)),
                        'values': []
                    })
                    trackAttributesNames.append(attrName)
                if attrName in featureAttrs:
                    for fieldsIndex, field in enumerate(fields):
                        value = featureAttrs[attrName]
                        if isinstance(value, (decimal.Decimal, datetime.date,
                                              datetime.datetime)):
                            value = str(value)
                        fields[fieldsIndex] = insert_value_at(
                            field, attrName, value)

    return {'id': layerId, 'name': params.translate(layerId), 'fields': fields}
Exemplo n.º 4
0
    def layerConfig(self, params):
        config = {}
        translate = params.translate
        excludedAttrs = ('geojsonUrlde', 'geojsonUrlfr', 'geojsonUrlit',
                         'geojsonUrlrm', 'geojsonUrlen')
        wmsHost = params.request.registry.settings['wmshost']
        for k in self.__dict__.keys():
            if not k.startswith("_") and k not in excludedAttrs and \
                self.__dict__[k] is not None and \
                    k not in ('staging'):
                if k == 'maps':
                    config['topics'] = self.__dict__[k]
                elif k == 'layerBodId':
                    config['label'] = translate(self.__dict__[k])
                elif k == 'attribution':
                    config[k] = translate(self.__dict__[k])
                elif k == 'matrixSet':
                    if self.__dict__[k] != '21781_26':
                        config[
                            'resolutions'] = self._getResolutionsFromMatrixSet(
                                self.__dict__[k])
                else:
                    config[k] = self.__dict__[k]

        layerStaging = self.__dict__['staging']
        if config['type'] == 'wmts':
            del config['singleTile']
        elif config['type'] == 'wms':
            if layerStaging != 'prod':
                config['wmsUrl'] = make_agnostic(config['wmsUrl'].replace(
                    'wms.geo.admin.ch', wmsHost))
        elif config['type'] == 'geojson':
            api_url = params.request.registry.settings['api_url']
            config['styleUrl'] = make_agnostic(api_url +
                                               '/static/vectorStyles/' +
                                               self.layerBodId + '.json')
            config['geojsonUrl'] = self._getGeoJsonUrl(params.lang)
        # sublayers don't have attributions
        if 'attribution' in config:
            config['attributionUrl'] = translate(self.__dict__['attribution'] +
                                                 '.url')

        # adding __queryable_attributes__ if they have them
        models = models_from_name(self.layerBodId)
        if models is not None:
            queryable_attributes = get_models_attributes_keys(
                models, params.lang, True)
            if len(queryable_attributes) > 0:
                config['queryableAttributes'] = queryable_attributes

        return {self.layerBodId: config}
Exemplo n.º 5
0
    def layerConfig(self, params):
        config = {}
        translate = params.translate
        wmsHost = params.request.registry.settings['wmshost']
        for k in self.__dict__.keys():
            val = self.__dict__[k]
            if not k.startswith("_") and not k.startswith('geojsonUrl') and \
                    val is not None and k not in ('staging', 'srid'):
                if k == 'maps':
                    config['topics'] = val
                elif k == 'layerBodId':
                    config['label'] = translate(val)
                elif k == 'attribution':
                    config[k] = translate(val)
                elif k == 'matrixSet':
                    if val != self.defaultMatrixSet21781 and \
                            self.__dict__['srid'] != '4326':
                        config['resolutions'] = self._getResolutionsFromMatrixSet(
                            val)
                else:
                    config[k] = val

        layerStaging = self.__dict__['staging']
        if config['type'] in ('wmts', 'aggregate', 'geojson'):
            del config['singleTile']

        if config['type'] == 'wms':
            if layerStaging != 'prod':
                config['wmsUrl'] = make_agnostic(
                    config['wmsUrl'].replace('wms.geo.admin.ch', wmsHost))
        elif config['type'] == 'geojson':
            api_url = params.request.registry.settings['api_url']
            config['styleUrl'] = make_agnostic(
                api_url + '/static/vectorStyles/' + self.layerBodId + '.json')
            config['geojsonUrl'] = self._getGeoJsonUrl(params.lang)
        # sublayers don't have attributions
        if 'attribution' in config:
            config['attributionUrl'] = translate(self.__dict__['attribution'] + '.url')

        # adding __queryable_attributes__ if they have them
        models = models_from_bodid(self.layerBodId)
        if models is not None:
            queryable_attributes = get_models_attributes_keys(models, params.lang, True)
            if len(queryable_attributes) > 0:
                config['queryableAttributes'] = queryable_attributes

        return {self.layerBodId: config}
Exemplo n.º 6
0
def feature_attributes(request):
    ''' This service is used to expose the
    attributes of vector layers. '''
    params = BaseLayersValidation(request)
    layerId = request.matchdict.get('layerId')
    models = models_from_bodid(layerId, srid=params.srid)
    # Models for the same layer have the same attributes
    if models is None:
        raise exc.HTTPBadRequest('No Vector Table was found for %s' % layerId)

    # Take into account all models and remove duplicated keys
    attributes = get_models_attributes_keys(models, params.lang, False)
    trackAttributesNames = []
    fields = []

    def insert_value_at(field, attrName, value):
        if field['name'] == attrName:
            if len(field['values']) < MAX_ATTRIBUTES_VALUES and \
               value not in field['values']:
                field['values'].append(value)
                field['values'].sort()
        return field

    for model in models:
        query = params.request.db.query(model)
        query = query.limit(SAMPLE_SIZE)

        for rowIndex, row in enumerate(query):
            # attrName as defined in the model
            for attrIndex, attrName in enumerate(attributes):
                featureAttrs = row.get_attributes(exclude_pkey=False)
                if attrName not in trackAttributesNames and \
                   attrName in featureAttrs:
                    fieldType = _find_type(model(), attrName)
                    fields.append({'name': attrName, 'type': str(fieldType),
                                   'alias': params.translate("%s.%s" % (layerId, attrName)),
                                   'values': []
                                   })
                    trackAttributesNames.append(attrName)
                if attrName in featureAttrs:
                    for fieldsIndex, field in enumerate(fields):
                        value = featureAttrs[attrName]
                        if isinstance(value, (decimal.Decimal, datetime.date, datetime.datetime)):
                            value = str(value)
                        fields[fieldsIndex] = insert_value_at(field, attrName, value)

    return {'id': layerId, 'name': params.translate(layerId), 'fields': fields}