Skip to content

Commit 16cb244

Browse files
authoredApr 26, 2017
Merge pull request #4416 from nyalldawson/feature_sink
Add a QgsFeatureSink interface
2 parents 13b4472 + 4293de6 commit 16cb244

30 files changed

+414
-108
lines changed
 

‎doc/api_break.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,8 @@ displayExpression instead. For the map tip use mapTipTemplate() instead.
21132113
- snapPoint() has been removed - use QgsPointLocator class instead.
21142114
- snapWithContext() has been removed - use QgsPointLocator class instead.
21152115
- insertSegmentVerticesForSnap() has been removed - use addTopologicalPoints() directly.
2116+
- addFeature() no longer accepts an alsoUpdateExtent boolean - this extra argument has been ignored for some time
2117+
- addFeatures() no longer accepts a makeSelected boolean, and will not automatically select newly added features. If desired, features must be manually selected by calling selectByIds() after addFeatures()
21162118

21172119

21182120
QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer}

‎python/auto_sip.blacklist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ core/qgsvectorsimplifymethod.sip
117117
core/qgscachedfeatureiterator.sip
118118
core/qgscacheindex.sip
119119
core/qgscacheindexfeatureid.sip
120-
core/qgsfeaturestore.sip
121120
core/qgsgeometrycache.sip
122121
core/qgslayerdefinition.sip
123122
core/qgsprojectfiletransform.sip

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
%Include qgsfeaturefilterprovider.sip
5757
%Include qgsfeatureiterator.sip
5858
%Include qgsfeaturerequest.sip
59+
%Include qgsfeaturesink.sip
5960
%Include qgsfeedback.sip
6061
%Include qgsfield.sip
6162
%Include qgsfieldconstraints.sip

‎python/core/qgsfeaturesink.sip

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsfeaturesink.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsFeatureSink
13+
{
14+
%Docstring
15+
An interface for objects which accept features via addFeature(s) methods.
16+
17+
.. versionadded:: 3.0
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgsfeaturesink.h"
22+
%End
23+
public:
24+
25+
virtual ~QgsFeatureSink();
26+
27+
virtual bool addFeature( QgsFeature &feature );
28+
%Docstring
29+
Adds a single ``feature`` to the sink.
30+
\see addFeatures()
31+
:return: true in case of success and false in case of failure
32+
:rtype: bool
33+
%End
34+
35+
virtual bool addFeatures( QgsFeatureList &features ) = 0;
36+
%Docstring
37+
Adds a list of ``features`` to the sink.
38+
\see addFeature()
39+
:return: true in case of success and false in case of failure
40+
:rtype: bool
41+
%End
42+
43+
virtual bool addFeatures( QgsFeatureIterator &iterator );
44+
%Docstring
45+
Adds all features from the specified ``iterator`` to the sink.
46+
:return: true if all features were added successfully, or false if any feature could not be added
47+
:rtype: bool
48+
%End
49+
50+
};
51+
52+
/************************************************************************
53+
* This file has been generated automatically from *
54+
* *
55+
* src/core/qgsfeaturesink.h *
56+
* *
57+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
58+
************************************************************************/

‎python/core/qgsfeaturestore.sip

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,92 @@
1-
class QgsFeatureStore
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsfeaturestore.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
class QgsFeatureStore : QgsFeatureSink
211
{
12+
%Docstring
13+
A container for features with the same fields and crs.
14+
%End
15+
316
%TypeHeaderCode
4-
#include <qgsfeaturestore.h>
17+
#include "qgsfeaturestore.h"
518
%End
619
public:
7-
//! Constructor
820
QgsFeatureStore();
21+
%Docstring
22+
Constructor
23+
%End
924

10-
//! Constructor
11-
QgsFeatureStore( const QgsFields& fields, const QgsCoordinateReferenceSystem& crs );
25+
QgsFeatureStore( const QgsFields &fields, const QgsCoordinateReferenceSystem &crs );
26+
%Docstring
27+
Constructor
28+
%End
1229

13-
/** Get fields list */
14-
QgsFields& fields();
30+
QgsFields fields() const;
31+
%Docstring
32+
Returns the store's field list.
33+
\see setFields()
34+
:rtype: QgsFields
35+
%End
1536

16-
/** Set fields. Resets feature's fields to pointer to new internal fields. */
17-
void setFields( const QgsFields & fields );
37+
void setFields( const QgsFields &fields );
38+
%Docstring
39+
Sets the store's ``fields``. Every contained feature's fields will be reset to match ``fields``.
40+
\see fields()
41+
%End
1842

19-
/** Get crs */
2043
QgsCoordinateReferenceSystem crs() const;
44+
%Docstring
45+
Returns the store's coordinate reference system.
46+
\see setCrs()
47+
:rtype: QgsCoordinateReferenceSystem
48+
%End
49+
50+
void setCrs( const QgsCoordinateReferenceSystem &crs );
51+
%Docstring
52+
Sets the store's ``crs``.
53+
\see crs()
54+
%End
2155

22-
/** Set crs */
23-
void setCrs( const QgsCoordinateReferenceSystem& crs );
56+
virtual bool addFeature( QgsFeature &feature );
2457

25-
/** Add feature. Feature's fields will be set to pointer to the store fields.
26-
* @param feature
27-
* @note added in 2.1
28-
*/
29-
void addFeature( const QgsFeature& feature );
58+
virtual bool addFeatures( QgsFeatureList &features );
3059

31-
/** Get features list reference */
32-
QgsFeatureList& features();
3360

34-
/** Set map of optional parameters */
35-
void setParams( const QMap<QString, QVariant> &params );
61+
QgsFeatureList features() const;
62+
%Docstring
63+
Returns the list of features contained in the store.
64+
:rtype: QgsFeatureList
65+
%End
66+
67+
void setParams( const QMap<QString, QVariant> &parameters );
68+
%Docstring
69+
Sets a map of optional ``parameters`` for the store.
70+
\see params()
71+
%End
3672

37-
/** Get map of optional parameters */
3873
QMap<QString, QVariant> params() const;
74+
%Docstring
75+
Returns the map of optional parameters.
76+
\see setParams()
77+
:rtype: QMap<str, QVariant>
78+
%End
79+
3980
};
81+
82+
typedef QList<QgsFeatureStore> QgsFeatureStoreList;
83+
84+
85+
86+
/************************************************************************
87+
* This file has been generated automatically from *
88+
* *
89+
* src/core/qgsfeaturestore.h *
90+
* *
91+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
92+
************************************************************************/

‎python/core/qgsvectordataprovider.sip

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
class QgsVectorDataProvider : QgsDataProvider
2+
class QgsVectorDataProvider : QgsDataProvider, QgsFeatureSink
33
{
44
%TypeHeaderCode
55
#include <qgsvectordataprovider.h>
@@ -197,10 +197,6 @@ class QgsVectorDataProvider : QgsDataProvider
197197
*/
198198
virtual void enumValues( int index, QStringList& enumList /Out/ ) const;
199199

200-
/**
201-
* Adds a list of features
202-
* @return true in case of success and false in case of failure
203-
*/
204200
virtual bool addFeatures( QList<QgsFeature> &flist /In,Out/ );
205201

206202
/**

‎python/core/qgsvectorfilewriter.sip

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
1. static call to QgsVectorFileWriter::writeAsVectorFormat(...) which saves the whole vector layer
55
2. create an instance of the class and issue calls to addFeature(...)
66
*/
7-
class QgsVectorFileWriter
7+
class QgsVectorFileWriter : QgsFeatureSink
88
{
99
%TypeHeaderCode
1010
#include <qgsvectorfilewriter.h>
@@ -380,8 +380,11 @@ class QgsVectorFileWriter
380380
/** Retrieves error message */
381381
QString errorMessage();
382382

383+
bool addFeature( QgsFeature &feature );
384+
bool addFeatures( QgsFeatureList &features );
385+
383386
/** Add feature to the currently opened data source */
384-
bool addFeature( QgsFeature& feature, QgsFeatureRenderer* renderer = 0, QgsUnitTypes::DistanceUnit outputUnit = QgsUnitTypes::DistanceMeters );
387+
bool addFeature( QgsFeature& feature, QgsFeatureRenderer* renderer, QgsUnitTypes::DistanceUnit outputUnit = QgsUnitTypes::DistanceMeters );
385388

386389
//! @note not available in python bindings
387390
// QMap<int, int> attrIdxToOgrIdx();

‎python/core/qgsvectorlayer.sip

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef QList<QgsPointV2> QgsPointSequence;
1919

2020

2121

22-
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
22+
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSink
2323
{
2424
%Docstring
2525
Represents a vector layer which manages a vector based data sets.
@@ -901,14 +901,8 @@ Return the provider type for this layer
901901
:rtype: QgsFeatureIterator
902902
%End
903903

904-
bool addFeature( QgsFeature &feature, bool alsoUpdateExtent = true );
905-
%Docstring
906-
Adds a feature
907-
\param feature feature to add
908-
\param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
909-
:return: True in case of success and False in case of error
910-
:rtype: bool
911-
%End
904+
virtual bool addFeature( QgsFeature &feature );
905+
912906

913907
bool updateFeature( QgsFeature &f );
914908
%Docstring
@@ -1256,11 +1250,8 @@ Delete an attribute field (but does not commit it)
12561250
:rtype: bool
12571251
%End
12581252

1259-
bool addFeatures( QgsFeatureList features, bool makeSelected = true );
1260-
%Docstring
1261-
Insert a copy of the given features into the layer (but does not commit it)
1262-
:rtype: bool
1263-
%End
1253+
virtual bool addFeatures( QgsFeatureList &features );
1254+
12641255

12651256
bool deleteFeature( QgsFeatureId fid );
12661257
%Docstring

‎src/app/qgisapp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7632,7 +7632,7 @@ void QgisApp::mergeSelectedFeatures()
76327632
vl->deleteFeature( *feature_it );
76337633
}
76347634

7635-
vl->addFeature( newFeature, false );
7635+
vl->addFeature( newFeature );
76367636

76377637
vl->endEditCommand();
76387638

@@ -7934,7 +7934,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
79347934
// now create new feature using pasted feature as a template. This automatically handles default
79357935
// values and field constraints
79367936
QgsFeature newFeature = QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context );
7937-
pasteVectorLayer->addFeature( newFeature, false );
7937+
pasteVectorLayer->addFeature( newFeature );
79387938
newIds << newFeature.id();
79397939

79407940
++featureIt;
@@ -8131,7 +8131,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
81318131
feature.setGeometry( g );
81328132
}
81338133
}
8134-
if ( ! layer->addFeatures( features, false ) || !layer->commitChanges() )
8134+
if ( ! layer->addFeatures( features ) || !layer->commitChanges() )
81358135
{
81368136
QgsDebugMsg( "Cannot add features or commit changes" );
81378137
delete layer;

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,8 @@ void QgsIdentifyResultsDialog::copyFeature()
18981898
}
18991899

19001900
QgsFeatureStore featureStore( item->fields(), item->crs() );
1901-
featureStore.features().append( item->feature() );
1901+
QgsFeature f( item->feature() );
1902+
featureStore.addFeature( f );
19021903
emit copyToClipboard( featureStore );
19031904
}
19041905

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ SET(QGIS_CORE_SRCS
136136
qgsfeature.cpp
137137
qgsfeatureiterator.cpp
138138
qgsfeaturerequest.cpp
139+
qgsfeaturesink.cpp
139140
qgsfeaturestore.cpp
140141
qgsfield.cpp
141142
qgsfieldconstraints.cpp
@@ -720,6 +721,7 @@ SET(QGIS_CORE_HDRS
720721
qgsfeaturefilterprovider.h
721722
qgsfeatureiterator.h
722723
qgsfeaturerequest.h
724+
qgsfeaturesink.h
723725
qgsfeaturestore.h
724726
qgsfieldformatter.h
725727
qgsfield_p.h

‎src/core/qgsfeaturesink.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***************************************************************************
2+
qgsfeaturesink.cpp
3+
------------------
4+
begin : April 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsfeaturestore.h"
19+
20+
bool QgsFeatureSink::addFeature( QgsFeature &feature )
21+
{
22+
QgsFeatureList features;
23+
features << feature;
24+
bool result = addFeatures( features );
25+
26+
// need to update the passed feature reference to the updated copy from the features list
27+
feature = features.at( 0 );
28+
return result;
29+
}
30+
31+
bool QgsFeatureSink::addFeatures( QgsFeatureIterator &iterator )
32+
{
33+
QgsFeature f;
34+
bool result = true;
35+
while ( iterator.nextFeature( f ) )
36+
{
37+
result = result && addFeature( f );
38+
}
39+
return result;
40+
}
41+

0 commit comments

Comments
 (0)