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[str, str]

Dict that map any nomenclature to Niva ID.

Source code in niva/core/mapper/niva_mapper.py
24
25
26
27
28
29
30
def __init__(self, map_dict: Dict[str, str], *args, **kwargs):
    """
    Attributes:
        map_dict (Dict[str, str]): 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. Field should be of type string or integer.

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
41
42
43
44
45
46
47
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
def map(self, layer: QgsVectorLayer, field: str) -> QgsVectorLayer:
    """Map NIVA ID to layer based on field. Field should be of type string or integer.

    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[str(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