Skip to content

Sample

Sample(*, mdi=float(), branch=str(), novelty=float, geometry=None) dataclass

Base class that wraps sample values for each of variables.

Attributes:

Name Type Description
mdi float

Multi diversity index [0,100].

branch str,**optionnal**

Nearest branch of Contra decision tree.

novelty float, **optionnal**

Value of novelty index.

geometry QgsGeometry, **optionnal**

Geometry linked to sample.

variables_names() classmethod

Return variables names for this sample class.

Returns:

Type Description
typing.List[str]

variables names for this sample class.

Source code in niva/core/samples/sample.py
24
25
26
27
28
29
30
31
@classmethod
def variables_names(cls) -> List[str]:
    """Return variables names for this sample class.

    Returns:
        variables names for this sample class.
    """
    return list(cls.__dataclass_fields__.keys())[4:]

to_dict()

Return sample content as dict.

Returns:

Type Description
typing.Dict[str, typing.Any]

Dict of sample content.

Source code in niva/core/samples/sample.py
33
34
35
36
37
38
39
40
41
42
43
44
def to_dict(self) -> Dict[str, Any]:
    """Return sample content as dict.

    Returns:
        Dict of sample content.
    """
    geom = self.geometry
    self.geometry = None
    sample_dict = asdict(self)
    sample_dict["geometry"] = geom
    self.geometry = geom
    return sample_dict

to_feature()

Export sample as QgsFeature with fields in attribute definitions order.

Returns:

Type Description
qgis.core.QgsFeature

QgsFeature with varaibles as fields.

Source code in niva/core/samples/sample.py
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 to_feature(self) -> QgsFeature:
    """Export sample as QgsFeature with fields in attribute definitions order.

    Returns:
        QgsFeature with varaibles as fields.
    """
    fields = []
    class_fields: List[Field] = self.__dataclass_fields__.values()
    for class_field in class_fields:
        if class_field.name == "geometry":
            continue
        if class_field.type in [float, int]:
            field_type = QVariant.Double
        elif class_field.type == str:
            field_type = QVariant.String
        elif class_field.type == bool:
            field_type = QVariant.Bool
        fields.append(QgsField(class_field.name, field_type))

    feat = QgsFeature()
    feat.setFields(QgsFields(fields))
    sample_dict = self.to_dict()
    del sample_dict["geometry"]
    feat.setAttributes(list(sample_dict.values()))
    feat.setGeometry(self.geometry)

    return feat

T1Sample(*, mdi=float(), branch=str(), novelty=float, geometry=None, richness=float(), simpson=float(), snc=float(), mfs=float()) dataclass

Bases: niva.core.samples.sample.Sample

Sample for NIVA Tier 1.

Attributes:

Name Type Description
richness float

Crop richness (number of different crops in area).

simpson float

Simpson diversity index for crops.

snc float

Semi-Natural Cover.

mfs float

Mean field size.