Main features
Here you will find basic usage of API. For more informations check the API documentation.
0. Setup¶
0.1 Init QGIS¶
In [1]:
Copied!
from qgis.core import QgsApplication
from niva.niva_provider import NivaProvider
qgs = QgsApplication([], False)
qgs.setPrefixPath("/path/to/qgis/install", True)
qgs.initQgis()
provider = NivaProvider()
qgs.processingRegistry().addProvider(provider)
from processing.core.Processing import Processing
from niva.niva_provider import NivaProvider
Processing.initialize()
# Shut down warnings
import warnings
warnings.filterwarnings('ignore')
from qgis.core import QgsApplication
from niva.niva_provider import NivaProvider
qgs = QgsApplication([], False)
qgs.setPrefixPath("/path/to/qgis/install", True)
qgs.initQgis()
provider = NivaProvider()
qgs.processingRegistry().addProvider(provider)
from processing.core.Processing import Processing
from niva.niva_provider import NivaProvider
Processing.initialize()
# Shut down warnings
import warnings
warnings.filterwarnings('ignore')
Application path not initialized qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in ""
0.2 Data¶
Enter paths to your layers to run the tutorial.
In [ ]:
Copied!
from qgis.core import QgsVectorLayer
roi_file_path = (
"/path/to/roi.shp" # Region of interest
)
artificial_file_path = (
"/path/to/data/artif.shp" # Artificial layer
)
iacs_file_path = "/path/to/data/iacs.shp" # IACS layer
naa_file_path = (
"/path/to/data/naa.shp" # NAA layer optionnal
)
roi = QgsVectorLayer(roi_file_path)
artificial = QgsVectorLayer(artificial_file_path)
iacs = QgsVectorLayer(iacs_file_path)
naa = QgsVectorLayer(naa_file_path)
print(roi.isValid(), artificial.isValid(), iacs.isValid(), naa.isValid())
from qgis.core import QgsVectorLayer
roi_file_path = (
"/path/to/roi.shp" # Region of interest
)
artificial_file_path = (
"/path/to/data/artif.shp" # Artificial layer
)
iacs_file_path = "/path/to/data/iacs.shp" # IACS layer
naa_file_path = (
"/path/to/data/naa.shp" # NAA layer optionnal
)
roi = QgsVectorLayer(roi_file_path)
artificial = QgsVectorLayer(artificial_file_path)
iacs = QgsVectorLayer(iacs_file_path)
naa = QgsVectorLayer(naa_file_path)
print(roi.isValid(), artificial.isValid(), iacs.isValid(), naa.isValid())
True True True True
In [3]:
Copied!
list(iacs.fields())
list(iacs.fields())
Out[3]:
[<QgsField: source (String)>, <QgsField: CODE_CULTU (String)>, <QgsField: ID_PARCEL (String)>, <QgsField: layer (String)>, <QgsField: path (String)>, <QgsField: ID.NIVA (Integer)>]
1. Grid¶
Create a grid that fits your ROI (Region Of Interest).
In [ ]:
Copied!
from niva.core import GridBuilder
grid_builder = GridBuilder(roi)
# Comment to keep only one
# Create
grid = grid_builder.create()
print(grid.featureCount())
# Extract
grid = grid_builder.extract(QgsVectorLayer("/path/to/data/laea_grid.shp"))
print(grid.featureCount())
from niva.core import GridBuilder
grid_builder = GridBuilder(roi)
# Comment to keep only one
# Create
grid = grid_builder.create()
print(grid.featureCount())
# Extract
grid = grid_builder.extract(QgsVectorLayer("/path/to/data/laea_grid.shp"))
print(grid.featureCount())
1239 767
2. Map Niva ID to IACS¶
In [ ]:
Copied!
from niva.core.mapper.niva_mapper import RPGMapper, LucasMapper, CustomCsvMapper
# Choose the mapper you need according to the nomenclature you use to map Niva field.
print(list(iacs.fields()))
mapper = RPGMapper()
iacs_niva = mapper.map(iacs, field="CODE_CULTU")
from niva.core.mapper.niva_mapper import RPGMapper, LucasMapper, CustomCsvMapper
# Choose the mapper you need according to the nomenclature you use to map Niva field.
print(list(iacs.fields()))
mapper = RPGMapper()
iacs_niva = mapper.map(iacs, field="CODE_CULTU")
[<QgsField: source (String)>, <QgsField: CODE_CULTU (String)>, <QgsField: ID_PARCEL (String)>, <QgsField: layer (String)>, <QgsField: path (String)>, <QgsField: ID.NIVA (Integer)>]
3. DataManager¶
DataManager is a class that provide logic to extract landscape features at lower level. T1GridDataManager is a subclass to compute landscape features for a cell for Tier 1. It returns a Sample object withou niva index, branch & novelty index.
In [6]:
Copied!
from niva.core.prediction import T1GridDataManager
data_manager = T1GridDataManager(iacs=iacs, artificial=artificial, naa=naa)
cell = grid.getFeature(100).geometry() # extract target cell geometry
cell_prediction = data_manager.compute(cell)
print(cell_prediction, cell_prediction.to_dict(), cell_prediction.to_feature())
from niva.core.prediction import T1GridDataManager
data_manager = T1GridDataManager(iacs=iacs, artificial=artificial, naa=naa)
cell = grid.getFeature(100).geometry() # extract target cell geometry
cell_prediction = data_manager.compute(cell)
print(cell_prediction, cell_prediction.to_dict(), cell_prediction.to_feature())
T1Sample(mdi=0.0, branch='', novelty=<class 'float'>, richness=7, simpson=4.513337042783619, snc=24.109810250065202, mfs=2.4794393381967956) {'mdi': 0.0, 'branch': '', 'novelty': <class 'float'>, 'richness': 7, 'simpson': 4.513337042783619, 'snc': 24.109810250065202, 'mfs': 2.4794393381967956} <qgis._core.QgsFeature object at 0x7ba3ffe879b0>
4. Predict¶
Predict with ContraTree. If 'mdi' is None it's because the novelty index is too high so the prediction isn't valable.
In [7]:
Copied!
from niva.core.contra import ContraTreeFactory
tree = ContraTreeFactory.build("T1") # build contra tree for Tier 1
cell_prediction.mdi, cell_prediction.novelty, cell_prediction.branch = tree.predict(cell_prediction)
print(cell_prediction.to_dict())
from niva.core.contra import ContraTreeFactory
tree = ContraTreeFactory.build("T1") # build contra tree for Tier 1
cell_prediction.mdi, cell_prediction.novelty, cell_prediction.branch = tree.predict(cell_prediction)
print(cell_prediction.to_dict())
{'mdi': 55.587498256657234, 'branch': 'FFFU', 'novelty': np.float64(0.3365562464779253), 'richness': 7, 'simpson': 4.513337042783619, 'snc': 24.109810250065202, 'mfs': 2.4794393381967956}
5. Full prediction¶
You can run a full prediction with NivaMdi subclasses. It will return a new layer with Niva index prediction.
In [8]:
Copied!
from niva.core.prediction import NivaMdiGridT1
niva_mdi = NivaMdiGridT1(grid, iacs=iacs, artificial=artificial, naa=naa)
niva_prediction = niva_mdi.predict_mdi()
from niva.core.prediction import NivaMdiGridT1
niva_mdi = NivaMdiGridT1(grid, iacs=iacs, artificial=artificial, naa=naa)
niva_prediction = niva_mdi.predict_mdi()
In [9]:
Copied!
niva_prediction.isValid()
values = niva_prediction.getFeature(100).attributes()
fields = niva_prediction.getFeature(100).fields()
print(dict(zip([f.name() for f in fields], values)))
niva_prediction.isValid()
values = niva_prediction.getFeature(100).attributes()
fields = niva_prediction.getFeature(100).fields()
print(dict(zip([f.name() for f in fields], values)))
{'mdi': 55.587498256657234, 'branch': 'FFFU', 'novelty': 0.3365562464779253, 'richness': 7.0, 'simpson': 4.513337042783619, 'snc': 24.109810250065202, 'mfs': 2.4794393381967956}