QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsfeaturesource.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturesource.cpp
3  -------------------
4  begin : May 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 "qgsfeaturesource.h"
19 #include "qgsfeaturerequest.h"
20 #include "qgsfeatureiterator.h"
21 #include "qgsmemoryproviderutils.h"
22 #include "qgsfeedback.h"
23 #include "qgsvectorlayer.h"
24 #include "qgsvectordataprovider.h"
25 
26 QSet<QVariant> QgsFeatureSource::uniqueValues( int fieldIndex, int limit ) const
27 {
28  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
29  return QSet<QVariant>();
30 
33  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
34 
35  QSet<QVariant> values;
36  QgsFeatureIterator it = getFeatures( req );
37  QgsFeature f;
38  while ( it.nextFeature( f ) )
39  {
40  values.insert( f.attribute( fieldIndex ) );
41  if ( limit > 0 && values.size() >= limit )
42  return values;
43  }
44  return values;
45 }
46 
47 QVariant QgsFeatureSource::minimumValue( int fieldIndex ) const
48 {
49  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
50  return QVariant();
51 
54  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
55 
56  QVariant min;
57  QgsFeatureIterator it = getFeatures( req );
58  QgsFeature f;
59  while ( it.nextFeature( f ) )
60  {
61  QVariant v = f.attribute( fieldIndex );
62  if ( v.isValid() && qgsVariantLessThan( v, min ) )
63  {
64  min = v;
65  }
66  }
67  return min;
68 }
69 
70 QVariant QgsFeatureSource::maximumValue( int fieldIndex ) const
71 {
72  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
73  return QVariant();
74 
77  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
78 
79  QVariant max;
80  QgsFeatureIterator it = getFeatures( req );
81  QgsFeature f;
82  while ( it.nextFeature( f ) )
83  {
84  QVariant v = f.attribute( fieldIndex );
85  if ( v.isValid() && qgsVariantGreaterThan( v, max ) )
86  {
87  max = v;
88  }
89  }
90  return max;
91 }
92 
94 {
95  QgsRectangle r;
96 
99 
100  QgsFeatureIterator it = getFeatures( req );
101  QgsFeature f;
102  while ( it.nextFeature( f ) )
103  {
104  if ( f.hasGeometry() )
106  }
107  return r;
108 }
109 
111 {
113  .setFlags( QgsFeatureRequest::NoGeometry )
114  .setSubsetOfAttributes( QgsAttributeList() ) );
115 
116  QgsFeatureIds ids;
117 
118  QgsFeature fet;
119  while ( fit.nextFeature( fet ) )
120  {
121  ids << fet.id();
122  }
123 
124  return ids;
125 }
126 
128 {
131 
132  QgsAttributeList requestedAttrs = request.subsetOfAttributes();
133 
134  QgsFields outFields;
136  {
137  int i = 0;
138  const QgsFields sourceFields = fields();
139  for ( const QgsField &field : sourceFields )
140  {
141  if ( requestedAttrs.contains( i ) )
142  outFields.append( field );
143  i++;
144  }
145  }
146  else
147  {
148  outFields = fields();
149  }
150 
151  std::unique_ptr< QgsVectorLayer > layer( QgsMemoryProviderUtils::createMemoryLayer(
152  sourceName(),
153  outFields,
154  outWkbType,
155  crs ) );
156  QgsFeature f;
157  QgsFeatureIterator it = getFeatures( request );
158  int fieldCount = fields().count();
159  while ( it.nextFeature( f ) )
160  {
161  if ( feedback && feedback->isCanceled() )
162  break;
163 
165  {
166  // remove unused attributes
167  QgsAttributes attrs;
168  for ( int i = 0; i < fieldCount; ++i )
169  {
170  if ( requestedAttrs.contains( i ) )
171  {
172  attrs.append( f.attributes().at( i ) );
173  }
174  }
175 
176  f.setAttributes( attrs );
177  }
178 
179  layer->dataProvider()->addFeature( f, QgsFeatureSink::FastInsert );
180  }
181 
182  return layer.release();
183 }
184 
QgsFeatureId id
Definition: qgsfeature.h:71
Wrapper for iterator of features from vector data provider or vector layer.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
A rectangle specified with double values.
Definition: qgsrectangle.h:40
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
const Flags & flags() const
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature&#39;s geometries, or an invalid QgsCoordi...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
Definition: qgsfields.h:42
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
QgsVectorLayer * materialize(const QgsFeatureRequest &request, QgsFeedback *feedback=nullptr)
Materializes a request (query) made against this feature source, by running it over the source and re...
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is greater than the second.
Definition: qgis.cpp:214
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
int count() const
Returns number of items.
Definition: qgsfields.cpp:115
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition: qgis.cpp:146
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
virtual QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const
Returns the set of unique values contained within the specified fieldIndex from this source...
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
Definition: qgsrectangle.h:352
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
This class represents a coordinate reference system (CRS).
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Creates a new memory layer using the specified parameters.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error...
QList< int > QgsAttributeList
Definition: qgsfield.h:27
bool nextFeature(QgsFeature &f)
virtual QString sourceName() const =0
Returns a friendly display name for the source.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
A vector of attributes.
Definition: qgsattributes.h:58
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
QgsAttributes attributes
Definition: qgsfeature.h:72
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.