Skip to content

Prediction

Classes to perform multidiversity index prediction.

BaseDataManager(*args, **kwargs)

Bases: niva.core.mixins.QgsLogicMixin

Base class for data management. Contain layers to extract features needed for computation.

Source code in niva/core/prediction/data_manager.py
28
29
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

crs property

Return CRS of data.

compute_metrics() abstractmethod

Compute metrics from data layers and output results as Sample. Sample will not have mdi, novelty & branch informations.

Source code in niva/core/prediction/data_manager.py
40
41
42
@abstractmethod
def compute_metrics(self) -> Sample:
    """Compute metrics from data layers and output results as Sample. Sample will not have mdi, novelty & branch informations."""

filter_iacs(layer)

Filter IACS on Niva.ID fields.

Parameters:

Name Type Description Default
layer qgis.core.QgsVectorLayer

IACS layer.

required

Returns:

Type Description
qgis.core.QgsVectorLayer

Filtered IACS.

Source code in niva/core/prediction/data_manager.py
44
45
46
47
48
49
50
51
52
53
54
55
56
def filter_iacs(self, layer: QgsVectorLayer) -> QgsVectorLayer:
    """Filter IACS on Niva.ID fields.

    Args:
        layer (QgsVectorLayer): IACS layer.

    Returns:
        Filtered IACS.
    """
    filtered = layer.materialize(
        QgsFeatureRequest().setFilterExpression(f'"Niva.ID" < 41')
    )
    return filtered

GridDataManager(grid, *args, **kwargs)

Bases: niva.core.prediction.data_manager.BaseDataManager

DataManager for grid based predictor.

Source code in niva/core/prediction/data_manager.py
67
68
69
def __init__(self, grid: QgsVectorLayer, *args, **kwargs):
    self.grid = grid
    super().__init__(*args, **kwargs)

compute_metrics(cell) abstractmethod

Compute cell metrics from data layers and output results as Sample. Sample will not have mdi, novelty & branch informations.

Parameters:

Name Type Description Default
cell qgis.core.QgsGeometry

Cell to compute metrics on.

required

Returns:

Type Description
niva.core.samples.sample.Sample

Sample of variables.

Source code in niva/core/prediction/data_manager.py
71
72
73
74
75
76
77
78
79
80
81
@abstractmethod
def compute_metrics(self, cell: QgsGeometry) -> Sample:
    """Compute cell metrics from data layers and output results as Sample. Sample will not have mdi, novelty & branch informations.

    Args:
        cell (QgsGeometry): Cell to compute metrics on.

    Returns:
        Sample of variables.
    """
    pass

__getitem__(x)

Get feature from self.grid or use passed feature to compute metrics on a Sample.

Parameters:

Name Type Description Default
x int

Grids feature id or QgsFeature.

required

Returns:

Type Description
niva.core.samples.sample.Sample

Sample corresponding to cell geometry.

Source code in niva/core/prediction/data_manager.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def __getitem__(self, x: Union[int, QgsFeature]) -> Sample:
    """Get feature from self.grid or use passed feature to compute metrics on a Sample.

    Args:
        x (int): Grids feature id or QgsFeature.

    Returns:
        Sample corresponding to cell geometry.
    """

    if not isinstance(x, QgsFeature):
        cell = self.grid.getFeature(x)
    sample = self.compute_metrics(cell.geometry())
    sample.geometry = cell.geometry()
    return sample

T1GridDataManager(grid, iacs, artificial, naa=None, *args, **kwargs)

Bases: niva.core.prediction.data_manager.GridDataManager

Data manager for Contra Tier 1 with grid based predictions.

Parameters:

Name Type Description Default
iacs qgis.core.QgsVectorLayer

Layer of IACS data. Warning: Provide IACS with Niva.ID field.

required
artificial qgis.core.QgsVectorLayer

Layer of artificial data.

required
naa qgis.core.QgsVectorLayer

Layer of NAA data.

None

Attributes:

Name Type Description
iacs qgis.core.QgsVectorLayer

Layer of IACS data.

artificial qgis.core.QgsVectorLayer

Layer of artificial data.

naa typing.Union[qgis.core.QgsVectorLayer, None]

Layer of NAA data or None if not passed to constructor.

iacs_index qgis.core.QgsSpatialIndex

Spatial index for layer iacs.

artificial_index qgis.core.QgsSpatialIndex

Spatial index for layer artificial.

naa_index qgis.core.QgsSpatialIndex

Spatial index for layer naa.

Source code in niva/core/prediction/data_manager.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def __init__(
    self,
    grid: QgsVectorLayer,
    iacs: QgsVectorLayer,
    artificial: QgsVectorLayer,
    naa: QgsVectorLayer = None,
    *args,
    **kwargs,
):
    super().__init__(grid, *args, **kwargs)
    self.iacs = self.filter_iacs(iacs)
    self.naa = naa
    self.artificial = artificial

    layers = [self.iacs, self.artificial]
    self.iacs_index = QgsSpatialIndex(self.iacs.getFeatures())
    self.artificial_index = QgsSpatialIndex(self.artificial.getFeatures())
    if self.naa:
        layers.append(self.naa)
        self.naa_index = QgsSpatialIndex(self.naa.getFeatures())

compute_metrics(cell)

Compute metrics and build Sample for cell.

Parameters:

Name Type Description Default
cell qgis.core.QgsGeometry

Cell geometry to compute metric on.

required

Returns:

Type Description
niva.core.samples.sample.T1Sample

Tier 1 sample for cell.

Source code in niva/core/prediction/data_manager.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def compute_metrics(self, cell: QgsGeometry) -> T1Sample:
    """Compute metrics and build Sample for cell.

    Args:
        cell (QgsGeometry): Cell geometry to compute metric on.

    Returns:
        Tier 1 sample for cell.
    """
    # Gather features
    iacs_feats = self._get_cell_features(self.iacs, self.iacs_index, cell)
    artificial_feats = self._get_cell_features(
        self.artificial, self.artificial_index, cell, crop=False
    )
    # Group geometries
    iacs = QgsGeometry.unaryUnion([f.geometry() for f in iacs_feats])
    artificial = QgsGeometry.unaryUnion([f.geometry() for f in artificial_feats])
    # get buildings with aoe geometry
    buildings = self._get_buildings(artificial_feats)
    # if naa apply geometries transformations.
    if self.naa:
        naa_feats = self._get_cell_features(self.naa, self.naa_index, cell)
        naa_union = QgsGeometry.unaryUnion([f.geometry() for f in naa_feats])
        naa_union = naa_union.difference(artificial)
        iacs = iacs.difference(naa_union)
        buildings = buildings.difference(naa_union)
    else:
        naa_union = QgsGeometry.fromWkt("POLYGON EMPTY")

    artificial = QgsGeometry.unaryUnion([artificial, buildings])
    if not naa_union.isEmpty():
        artificial = artificial.difference(naa_union)
    artificial = artificial.intersection(cell)

    # compute metrics
    snc = semi_natural_cover(artificial, iacs, cell.area())
    mfs = mean_field_size(iacs_feats)
    rich, simpson = diversity(iacs_feats)

    return T1Sample(richness=rich, simpson=simpson, snc=snc, mfs=mfs, geometry=cell)