QgsGeometry geom(QgsPointXY(10, 20)); // create a point geometry if (geom.isNull()) { qDebug() << "Geometry is null"; } else { qDebug() << "Geometry is not null"; }
QgsVectorLayer* layer = qgis.utils().createVectorLayer("point", "my_layer", "memory"); // add some features with null geometries QgsFeature feat1, feat2; feat1.setGeometry(QgsGeometry()); feat2.setGeometry(nullptr); layer->dataProvider()->addFeatures(QListIn this example, we create a memory layer with a point geometry type and add two features with null geometries (one using an empty QgsGeometry object, the other using a nullptr). We then iterate over the features and handle null geometries by checking if their geometry is null using the isNull() method. Depending on the application, we might skip or delete features with null geometries, or perform some other action. The QgsGeometry class is part of the QgsCore library, which is included in the QGIS application and plugin development framework.() << feat1 << feat2); // iterate over features and handle null geometries for (const QgsFeature& feat : layer->getFeatures()) { if (feat.geometry().isNull()) { qDebug() << "Found null geometry in feature" << feat.id(); // do something with null geometries, e.g. skip or delete feature } else { // process valid geometries } }