Skip to content

Mapper

Classes to map Niva ID to IACS layer.

Mapper()

Bases: abc.ABC

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.

mapping_json str

Path a json file to load map_dcit from. Class attribute.

Source code in niva/core/mapper/niva_mapper.py
16
17
18
19
20
21
22
def __init__(self):
    """
    Attributes:
        map_dict (Dict[Any, int]): Dict that map any nomenclature to Niva ID.
        mapping_json (str): Path a json file to load map_dcit from. Class attribute.
    """
    self.map_dict = json.load(Path(self.mapping_json).open())

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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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(
        f"Polygon?crs={layer.crs().authid()}", "mapped niva", "memory"
    )
    mapped_layer_fields = layer.fields()
    mapped_layer_fields.append(QgsField("ID.NIVA", QVariant.Int, len=2))
    mapped_layer.dataProvider().addAttributes(mapped_layer_fields)
    mapped_layer.updateFields()

    mapped_features = []
    for feature in layer.getFeatures():
        feature: QgsFeature
        niva_code = 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)

    mapped_layer.dataProvider().addFeatures(mapped_features)
    return mapped_layer

LucasMapper()

Bases: niva.core.mapper.niva_mapper.Mapper

Mapper with LUCAS nomenclature.

>>> iacs = QgsVectorLayer('/path/to/iacs.shp')
>>> lucas_mapper = LucasMapper()
>>> mapped_iacs = lucas_mapper.map(iacs, field='LUCAS')
Source code in niva/core/mapper/niva_mapper.py
16
17
18
19
20
21
22
def __init__(self):
    """
    Attributes:
        map_dict (Dict[Any, int]): Dict that map any nomenclature to Niva ID.
        mapping_json (str): Path a json file to load map_dcit from. Class attribute.
    """
    self.map_dict = json.load(Path(self.mapping_json).open())

RPGMapper()

Bases: niva.core.mapper.niva_mapper.Mapper

Mapper with RPG code culture nomenclature.

>>> iacs = QgsVectorLayer('/path/to/iacs.shp')
>>> rpg_mapper = RPGMapper()
>>> mapped_iacs = rpg_mapper.map(iacs, field='CODE_CULTURE')
Source code in niva/core/mapper/niva_mapper.py
16
17
18
19
20
21
22
def __init__(self):
    """
    Attributes:
        map_dict (Dict[Any, int]): Dict that map any nomenclature to Niva ID.
        mapping_json (str): Path a json file to load map_dcit from. Class attribute.
    """
    self.map_dict = json.load(Path(self.mapping_json).open())

CustomCsvMapper(csv)

Bases: niva.core.mapper.niva_mapper.Mapper

Mapper with custom csv nomenclature.

Parameters:

Name Type Description Default
csv ``str``

Path to custom nomenclature as .csv file.

required
>>> iacs = QgsVectorLayer('/path/to/iacs.shp')
>>> csv_path = '/path/to/custom_nomenclature.csv'
>>> csv_mapper = CustomCsvMapper(csv='csv_path')
>>> mapped_iacs = csv_mapper.map(iacs, field='FIELD_TO_MAP')
Source code in niva/core/mapper/niva_mapper.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(self, csv: str):
    """
    Args:
        csv (``str``): Path to custom nomenclature as .csv file.

    ```python
    >>> iacs = QgsVectorLayer('/path/to/iacs.shp')
    >>> csv_path = '/path/to/custom_nomenclature.csv'
    >>> csv_mapper = CustomCsvMapper(csv='csv_path')
    >>> mapped_iacs = csv_mapper.map(iacs, field='FIELD_TO_MAP')
    ```
    """
    csv_layer = QgsVectorLayer(csv)
    map_dict = {}
    for feature in csv_layer.getFeatures():
        map_dict[feature["code"]] = int(feature["Niva.ID"])
    self.map_dict = map_dict