QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
27 {
29 }
30 
31 QSet<QVariant> QgsFeatureSource::uniqueValues( int fieldIndex, int limit ) const
32 {
33  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
34  return QSet<QVariant>();
35 
38  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
39 
40  QSet<QVariant> values;
41  QgsFeatureIterator it = getFeatures( req );
42  QgsFeature f;
43  while ( it.nextFeature( f ) )
44  {
45  values.insert( f.attribute( fieldIndex ) );
46  if ( limit > 0 && values.size() >= limit )
47  return values;
48  }
49  return values;
50 }
51 
52 QVariant QgsFeatureSource::minimumValue( int fieldIndex ) const
53 {
54  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
55  return QVariant();
56 
59  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
60 
61  QVariant min;
62  QgsFeatureIterator it = getFeatures( req );
63  QgsFeature f;
64  while ( it.nextFeature( f ) )
65  {
66  const QVariant v = f.attribute( fieldIndex );
67  if ( !v.isNull() && ( qgsVariantLessThan( v, min ) || min.isNull() ) )
68  {
69  min = v;
70  }
71  }
72  return min;
73 }
74 
75 QVariant QgsFeatureSource::maximumValue( int fieldIndex ) const
76 {
77  if ( fieldIndex < 0 || fieldIndex >= fields().count() )
78  return QVariant();
79 
82  req.setSubsetOfAttributes( QgsAttributeList() << fieldIndex );
83 
84  QVariant max;
85  QgsFeatureIterator it = getFeatures( req );
86  QgsFeature f;
87  while ( it.nextFeature( f ) )
88  {
89  const QVariant v = f.attribute( fieldIndex );
90  if ( !v.isNull() && ( qgsVariantGreaterThan( v, max ) || max.isNull() ) )
91  {
92  max = v;
93  }
94  }
95  return max;
96 }
97 
99 {
100  QgsRectangle r;
101 
102  QgsFeatureRequest req;
103  req.setNoAttributes();
104 
105  QgsFeatureIterator it = getFeatures( req );
106  QgsFeature f;
107  while ( it.nextFeature( f ) )
108  {
109  if ( f.hasGeometry() )
111  }
112  return r;
113 }
114 
116 {
118  .setFlags( QgsFeatureRequest::NoGeometry )
119  .setNoAttributes() );
120 
121  QgsFeatureIds ids;
122 
123  QgsFeature fet;
124  while ( fit.nextFeature( fet ) )
125  {
126  ids << fet.id();
127  }
128 
129  return ids;
130 }
131 
133 {
136 
137  const QgsAttributeList requestedAttrs = request.subsetOfAttributes();
138 
139  QgsFields outFields;
141  {
142  int i = 0;
143  const QgsFields sourceFields = fields();
144  for ( const QgsField &field : sourceFields )
145  {
146  if ( requestedAttrs.contains( i ) )
147  outFields.append( field );
148  i++;
149  }
150  }
151  else
152  {
153  outFields = fields();
154  }
155 
156  std::unique_ptr< QgsVectorLayer > layer( QgsMemoryProviderUtils::createMemoryLayer(
157  sourceName(),
158  outFields,
159  outWkbType,
160  crs ) );
161  QgsFeature f;
162  QgsFeatureIterator it = getFeatures( request );
163  const int fieldCount = fields().count();
164  while ( it.nextFeature( f ) )
165  {
166  if ( feedback && feedback->isCanceled() )
167  break;
168 
170  {
171  // remove unused attributes
172  QgsAttributes attrs;
173  for ( int i = 0; i < fieldCount; ++i )
174  {
175  if ( requestedAttrs.contains( i ) )
176  {
177  attrs.append( f.attributes().at( i ) );
178  }
179  }
180 
181  f.setAttributes( attrs );
182  }
183 
184  layer->dataProvider()->addFeature( f, QgsFeatureSink::FastInsert );
185  }
186 
187  return layer.release();
188 }
189 
191 {
192  return SpatialIndexUnknown;
193 }
194 
QgsFeatureRequest::NoGeometry
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition: qgsfeaturerequest.h:115
QgsFeatureSource::minimumValue
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:52
QgsFeatureSource::hasSpatialIndex
virtual SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source,...
Definition: qgsfeaturesource.cpp:190
QgsFeatureSource::sourceCrs
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
qgsfeaturerequest.h
QgsRectangle::combineExtentWith
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Definition: qgsrectangle.h:391
qgsVariantLessThan
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:119
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgsfeatureiterator.h
QgsFields::count
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsMemoryProviderUtils::createMemoryLayer
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) SIP_FACTORY
Creates a new memory layer using the specified parameters.
Definition: qgsmemoryproviderutils.cpp:63
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsFeatureSource::uniqueValues
virtual QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const
Returns the set of unique values contained within the specified fieldIndex from this source.
Definition: qgsfeaturesource.cpp:31
QgsFeatureRequest::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
Definition: qgsfeaturerequest.cpp:291
QgsFeatureRequest::setSubsetOfAttributes
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Definition: qgsfeaturerequest.cpp:228
field
const QgsField & field
Definition: qgsfield.h:463
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeatureRequest::SubsetOfAttributes
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
Definition: qgsfeaturerequest.h:116
QgsFeatureRequest::flags
Flags flags() const
Returns the flags which affect how features are fetched.
Definition: qgsfeaturerequest.h:673
qgsVariantGreaterThan
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:187
QgsFeatureSource::allFeatureIds
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
Definition: qgsfeaturesource.cpp:115
QgsFeatureSource::hasFeatures
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
Definition: qgsfeaturesource.cpp:26
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsFeatureSource::maximumValue
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:75
QgsFeatureSource::sourceName
virtual QString sourceName() const =0
Returns a friendly display name for the source.
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsFeatureSource::SpatialIndexPresence
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition: qgsfeaturesource.h:187
QgsFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
qgsfeaturesource.h
qgsvectordataprovider.h
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:977
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
QgsFeatureSource::sourceExtent
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
Definition: qgsfeaturesource.cpp:98
QgsFeatureRequest::setNoAttributes
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
Definition: qgsfeaturerequest.cpp:235
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsFeatureSource::fields
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
qgsvectorlayer.h
QgsFeatureSource::FeatureAvailability
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Definition: qgsfeaturesource.h:50
QgsWkbTypes::NoGeometry
@ NoGeometry
Definition: qgswkbtypes.h:85
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsFeatureSource::SpatialIndexUnknown
@ SpatialIndexUnknown
Spatial index presence cannot be determined, index may or may not exist.
Definition: qgsfeaturesource.h:189
QgsFeatureSource::wkbType
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsFeatureSource::materialize
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...
Definition: qgsfeaturesource.cpp:132
QgsFeatureRequest::subsetOfAttributes
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
Definition: qgsfeaturerequest.h:723
QgsFeatureSource::FeaturesMaybeAvailable
@ FeaturesMaybeAvailable
There may be features available in this source.
Definition: qgsfeaturesource.h:54
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:1080
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgsmemoryproviderutils.h
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
qgsfeedback.h
QgsFeatureRequest::setFlags
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
Definition: qgsfeaturerequest.cpp:222
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50