QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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"
22#include "qgsfeedback.h"
23#include "qgsvectorlayer.h"
25
30
31QSet<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;
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
52QVariant 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;
63 QgsFeature f;
64 while ( it.nextFeature( f ) )
65 {
66 const QVariant v = f.attribute( fieldIndex );
67 if ( !QgsVariantUtils::isNull( v ) && ( qgsVariantLessThan( v, min ) || QgsVariantUtils::isNull( min ) ) )
68 {
69 min = v;
70 }
71 }
72 return min;
73}
74
75QVariant 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;
86 QgsFeature f;
87 while ( it.nextFeature( f ) )
88 {
89 const QVariant v = f.attribute( fieldIndex );
90 if ( !QgsVariantUtils::isNull( v ) && ( qgsVariantGreaterThan( v, max ) || QgsVariantUtils::isNull( max ) ) )
91 {
92 max = v;
93 }
94 }
95 return max;
96}
97
102
104{
105 QgsBox3D r;
106
108 req.setNoAttributes();
109
111 QgsFeature f;
112 while ( it.nextFeature( f ) )
113 {
114 if ( f.hasGeometry() )
116 }
117 return r;
118}
119
121{
124 .setNoAttributes() );
125
126 QgsFeatureIds ids;
127
128 QgsFeature fet;
129 while ( fit.nextFeature( fet ) )
130 {
131 ids << fet.id();
132 }
133
134 return ids;
135}
136
138{
141 : request.destinationCrs().isValid() ? request.destinationCrs() : sourceCrs();
142
143 const QgsAttributeList requestedAttrs = request.subsetOfAttributes();
144
145 QgsFields outFields;
147 {
148 int i = 0;
149 const QgsFields sourceFields = fields();
150 for ( const QgsField &field : sourceFields )
151 {
152 if ( requestedAttrs.contains( i ) )
153 outFields.append( field );
154 i++;
155 }
156 }
157 else
158 {
159 outFields = fields();
160 }
161
162 std::unique_ptr< QgsVectorLayer > layer( QgsMemoryProviderUtils::createMemoryLayer(
163 sourceName(),
164 outFields,
165 outWkbType,
166 crs ) );
167 QgsFeature f;
168 QgsFeatureIterator it = getFeatures( request );
169 const int fieldCount = fields().count();
170 while ( it.nextFeature( f ) )
171 {
172 if ( feedback && feedback->isCanceled() )
173 break;
174
176 {
177 // remove unused attributes
178 QgsAttributes attrs;
179 for ( int i = 0; i < fieldCount; ++i )
180 {
181 if ( requestedAttrs.contains( i ) )
182 {
183 attrs.append( f.attributes().at( i ) );
184 }
185 }
186
187 f.setAttributes( attrs );
188 }
189
190 layer->dataProvider()->addFeature( f, QgsFeatureSink::FastInsert );
191 }
192
193 return layer.release();
194}
195
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:522
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:541
@ FeaturesMaybeAvailable
There may be features available in this source.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ NoGeometry
No geometry.
A vector of attributes.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:394
void combineWith(const QgsBox3D &box)
Expands the bbox so that it covers both the original rectangle and the given rectangle.
Definition qgsbox3d.cpp:208
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
Qgis::FeatureRequestFlags flags() const
Returns the flags which affect how features are fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordi...
QgsCoordinateTransform coordinateTransform() const
Returns the coordinate transform which will be used to transform the feature's geometries.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
virtual QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const
Returns the set of unique values contained within the specified fieldIndex from this source.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
virtual Qgis::WkbType wkbType() const =0
Returns the geometry type for features returned by this source.
virtual Qgis::FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
virtual Qgis::SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source,...
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
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...
virtual QString sourceName() const =0
Returns a friendly display name for the source.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
virtual QgsBox3D sourceExtent3D() const
Returns the 3D extent of all geometries from the source.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
int count
Definition qgsfields.h:50
QgsBox3D boundingBox3D() const
Returns the 3D bounding box of the geometry.
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), bool loadDefaultStyle=true) SIP_FACTORY
Creates a new memory layer using the specified parameters.
A rectangle specified with double values.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
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:121
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:189
QSet< QgsFeatureId > QgsFeatureIds
QList< int > QgsAttributeList
Definition qgsfield.h:27
const QgsCoordinateReferenceSystem & crs