Skip to content

Mapper

Classes to map Niva ID to IACS layer.

Mapper(map_dict, *args, **kwargs)

Bases: niva.core.mixins.QgsLogicMixin

Mapper base class to join NIVA ID.

Attributes:

Name Type Description
map_dict typing.Dict[typing.Any, int]

Dict that map any nomenclature to Niva ID.

Source code in niva/core/mapper/niva_mapper.py
28
29
30
31
32
33
34
def __init__(self, map_dict: Dict[Any, str], *args, **kwargs):
    """
    Attributes:
        map_dict (Dict[Any, int]): Dict that map any nomenclature to Niva ID.
    """
    self.map_dict = map_dict
    super().__init__(*args, **kwargs)

map(layer, field)

Map NIVA ID to layer based on field.

Parameters:

Name Type Description Default
layer qgis.core.QgsVectorLayer

Layer to add NIVA ID.

required
field str

Mapping field.

required

Returns:

Type Description
qgis.core.QgsVectorLayer

Layer with NIVA ID field.

Source code in niva/core/mapper/niva_mapper.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def map(self, layer: QgsVectorLayer, field: str) -> QgsVectorLayer:
    """Map NIVA ID to layer based on field.

    Args:
        layer (QgsVectorLayer): Layer to add NIVA ID.
        field (str): Mapping field.

    Returns:
        Layer with NIVA ID field.
    """

    mapped_layer: QgsVectorLayer = (
        VectorLayerBuilder().from_layer(layer, copy_feat=False).build().load()
    )
    niva_field = FieldFactory().create("int32", "Niva.ID")
    VectorLayerManager().field.add(mapped_layer, niva_field)

    mapped_features = []
    total = layer.featureCount()
    for i, feature in enumerate(layer.getFeatures()):
        feature: QgsFeature
        niva_code = int(self.map_dict[feature[field]])
        mapped_feat = QgsFeature()
        mapped_feat.setFields(mapped_layer.fields())
        mapped_feat.setGeometry(feature.geometry())
        mapped_feat.setAttributes(feature.attributes() + [niva_code])
        mapped_features.append(mapped_feat)
        self.feedback.setProgress((i * 100 / total))
        self.cancel()

    VectorLayerManager().feature.add(mapped_layer, mapped_features)
    return mapped_layer