Skip to content

Commit 574c672

Browse files
authored
add QgsVectorLayer::getGeometry(fid) (#8174)
* add QgsVectorLayer::getGeometry(fid) * update docs to say geom is invalid
1 parent d5e57d3 commit 574c672

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

python/core/auto_generated/qgsvectorlayer.sip.in

+6
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,12 @@ Query the layer for features matching a given expression.
10541054
%Docstring
10551055
Query the layer for the feature with the given id.
10561056
If there is no such feature, the returned feature will be invalid.
1057+
%End
1058+
1059+
QgsGeometry getGeometry( QgsFeatureId fid ) const;
1060+
%Docstring
1061+
Query the layer for the geometry at the given id.
1062+
If there is no such feature, the returned geometry will be invalid.
10571063
%End
10581064

10591065
QgsFeatureIterator getFeatures( const QgsFeatureIds &fids );

src/core/qgsvectorlayer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,16 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest &request
941941
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
942942
}
943943

944+
QgsGeometry QgsVectorLayer::getGeometry( QgsFeatureId fid ) const
945+
{
946+
QgsFeature feature;
947+
getFeatures( QgsFeatureRequest( fid ).setFlags( QgsFeatureRequest::SubsetOfAttributes ) ).nextFeature( feature );
948+
if ( feature.isValid() )
949+
return feature.geometry();
950+
else
951+
return QgsGeometry();
952+
}
953+
944954
bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
945955
{
946956
if ( !mValid || !mEditBuffer || !mDataProvider )

src/core/qgsvectorlayer.h

+6
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
10381038
return feature;
10391039
}
10401040

1041+
/**
1042+
* Query the layer for the geometry at the given id.
1043+
* If there is no such feature, the returned geometry will be invalid.
1044+
*/
1045+
QgsGeometry getGeometry( QgsFeatureId fid ) const;
1046+
10411047
/**
10421048
* Query the layer for the features with the given ids.
10431049
*/

0 commit comments

Comments
 (0)