QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
46 return QStringLiteral(
"Generic vector file" );
65 return QgsFeatureSource::FeatureAvailability::NoFeaturesAvailable;
67 return QgsFeatureSource::FeatureAvailability::FeaturesAvailable;
114 Q_UNUSED( attributes )
120 Q_UNUSED( attributes )
126 Q_UNUSED( renamedAttributes )
144 Q_UNUSED( fieldIndex )
151 if ( fieldIndex < 0 || fieldIndex >= f.
count() )
164 Q_UNUSED( geometry_map )
199 mEncoding = QTextCodec::codecForName( e.toLocal8Bit().constData() );
201 if ( !mEncoding && e != QLatin1String(
"System" ) )
205 mEncoding = QTextCodec::codecForName(
"System" );
209 mEncoding = QTextCodec::codecForLocale();
211 Q_ASSERT( mEncoding );
218 return mEncoding->name();
226 QStringList abilitiesList;
232 abilitiesList += tr(
"Add Features" );
237 abilitiesList += tr(
"Delete Features" );
242 abilitiesList += tr(
"Change Attribute Values" );
247 abilitiesList += tr(
"Add Attributes" );
252 abilitiesList += tr(
"Delete Attributes" );
257 abilitiesList += tr(
"Rename Attributes" );
263 abilitiesList += tr(
"Create Spatial Index" );
268 abilitiesList += tr(
"Create Attribute Indexes" );
273 abilitiesList += tr(
"Fast Access to Features at ID" );
278 abilitiesList += tr(
"Change Geometries" );
283 abilitiesList += tr(
"Presimplify Geometries" );
288 abilitiesList += tr(
"Presimplify Geometries with Validity Check" );
293 abilitiesList += tr(
"Simultaneous Geometry and Attribute Updates" );
298 abilitiesList += tr(
"Transactions" );
303 abilitiesList += tr(
"Curved Geometries" );
306 return abilitiesList.join( QStringLiteral(
", " ) );
317 QMap<QString, int> resultMap;
320 for (
int i = 0; i < fieldsCopy.
count(); ++i )
322 resultMap.insert( fieldsCopy.
at( i ).
name(), i );
350 QgsDebugMsgLevel( QStringLiteral(
"field name = %1 type = %2 length = %3 precision = %4" )
352 QVariant::typeToName( field.
type() ) )
356 const auto constMNativeTypes = mNativeTypes;
357 for (
const NativeType &nativeType : constMNativeTypes )
359 QgsDebugMsgLevel( QStringLiteral(
"native field type = %1 min length = %2 max length = %3 min precision = %4 max precision = %5" )
360 .arg( QVariant::typeToName( nativeType.mType ) )
361 .arg( nativeType.mMinLen )
362 .arg( nativeType.mMaxLen )
363 .arg( nativeType.mMinPrec )
364 .arg( nativeType.mMaxPrec ), 2 );
366 if ( field.
type() != nativeType.mType )
372 if ( ( nativeType.mMinLen > 0 && field.
length() < nativeType.mMinLen ) ||
373 ( nativeType.mMaxLen > 0 && field.
length() > nativeType.mMaxLen ) )
383 if ( ( nativeType.mMinPrec > 0 && field.
precision() < nativeType.mMinPrec ) ||
384 ( nativeType.mMaxPrec > 0 && field.
precision() > nativeType.mMaxPrec ) )
395 QgsDebugMsg( QStringLiteral(
"no sufficient native type found" ) );
401 if ( index < 0 || index >=
fields().count() )
403 QgsDebugMsg(
"Warning: access requested to invalid field index: " + QString::number( index ) );
409 if ( !mCacheMinValues.contains( index ) )
412 return mCacheMinValues[index];
417 if ( index < 0 || index >=
fields().count() )
419 QgsDebugMsg(
"Warning: access requested to invalid field index: " + QString::number( index ) );
425 if ( !mCacheMaxValues.contains( index ) )
428 return mCacheMaxValues[index];
437 if ( index < 0 || index >=
fields().count() )
442 keys.append( index );
448 request.
setFilterExpression( QStringLiteral(
"\"%1\" ILIKE '%%2%'" ).arg( fieldName, substring ) );
455 QString value = f.
attribute( index ).toString();
456 if ( !set.contains( value ) )
458 results.append( value );
462 if ( ( limit >= 0 && results.size() >= limit ) || ( feedback && feedback->
isCanceled() ) )
474 Q_UNUSED( parameters )
484 mCacheMinMaxDirty =
true;
485 mCacheMinValues.clear();
486 mCacheMaxValues.clear();
491 if ( !mCacheMinMaxDirty )
495 for (
int i = 0; i < flds.
count(); ++i )
497 if ( flds.
at( i ).
type() == QVariant::Int )
499 mCacheMinValues[i] = QVariant( std::numeric_limits<int>::max() );
500 mCacheMaxValues[i] = QVariant( std::numeric_limits<int>::lowest() );
502 else if ( flds.
at( i ).
type() == QVariant::LongLong )
504 mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
505 mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::lowest() );
507 else if ( flds.
at( i ).
type() == QVariant::Double )
509 mCacheMinValues[i] = QVariant( std::numeric_limits<double>::max() );
510 mCacheMaxValues[i] = QVariant( std::numeric_limits<double>::lowest() );
515 mCacheMinValues[i] = QVariant();
516 mCacheMaxValues[i] = QVariant();
528 for (
int attributeIndex : keys )
530 const QVariant &varValue = attrs.at( attributeIndex );
532 if ( varValue.isNull() )
535 switch ( flds.
at( attributeIndex ).
type() )
539 int value = varValue.toInt();
540 if ( value < mCacheMinValues[ attributeIndex ].toInt() )
541 mCacheMinValues[ attributeIndex ] = value;
542 if ( value > mCacheMaxValues[ attributeIndex ].toInt() )
543 mCacheMaxValues[ attributeIndex ] = value;
546 case QVariant::LongLong:
548 qlonglong value = varValue.toLongLong();
549 if ( value < mCacheMinValues[ attributeIndex ].toLongLong() )
550 mCacheMinValues[ attributeIndex ] = value;
551 if ( value > mCacheMaxValues[ attributeIndex ].toLongLong() )
552 mCacheMaxValues[ attributeIndex ] = value;
555 case QVariant::Double:
557 double value = varValue.toDouble();
558 if ( value < mCacheMinValues[ attributeIndex ].toDouble() )
559 mCacheMinValues[attributeIndex ] = value;
560 if ( value > mCacheMaxValues[ attributeIndex ].toDouble() )
561 mCacheMaxValues[ attributeIndex ] = value;
564 case QVariant::DateTime:
566 QDateTime value = varValue.toDateTime();
567 if ( value < mCacheMinValues[ attributeIndex ].toDateTime() || !mCacheMinValues[ attributeIndex ].
isValid() )
568 mCacheMinValues[attributeIndex ] = value;
569 if ( value > mCacheMaxValues[ attributeIndex ].toDateTime() || !mCacheMaxValues[ attributeIndex ].
isValid() )
570 mCacheMaxValues[ attributeIndex ] = value;
575 QDate value = varValue.toDate();
576 if ( value < mCacheMinValues[ attributeIndex ].toDate() || !mCacheMinValues[ attributeIndex ].
isValid() )
577 mCacheMinValues[attributeIndex ] = value;
578 if ( value > mCacheMaxValues[ attributeIndex ].toDate() || !mCacheMaxValues[ attributeIndex ].
isValid() )
579 mCacheMaxValues[ attributeIndex ] = value;
584 QTime value = varValue.toTime();
585 if ( value < mCacheMinValues[ attributeIndex ].toTime() || !mCacheMinValues[ attributeIndex ].
isValid() )
586 mCacheMinValues[attributeIndex ] = value;
587 if ( value > mCacheMaxValues[ attributeIndex ].toTime() || !mCacheMaxValues[ attributeIndex ].
isValid() )
588 mCacheMaxValues[ attributeIndex ] = value;
593 QString value = varValue.toString();
594 if ( mCacheMinValues[ attributeIndex ].isNull() || value < mCacheMinValues[attributeIndex ].toString() )
596 mCacheMinValues[attributeIndex] = value;
598 if ( mCacheMaxValues[attributeIndex].isNull() || value > mCacheMaxValues[attributeIndex].toString() )
600 mCacheMaxValues[attributeIndex] = value;
608 mCacheMinMaxDirty =
false;
615 if ( !v.convert( type ) || value.isNull() )
616 v = QVariant( type );
626 static bool _compareEncodings(
const QString &s1,
const QString &s2 )
628 return s1.toLower() < s2.toLower();
633 static std::once_flag initialized;
634 std::call_once( initialized, [ = ]
636 const auto codecs { QTextCodec::availableCodecs() };
637 for (
const QString &codec : codecs )
642 smEncodings <<
"BIG5";
643 smEncodings <<
"BIG5-HKSCS";
644 smEncodings <<
"EUCJP";
645 smEncodings <<
"EUCKR";
646 smEncodings <<
"GB2312";
647 smEncodings <<
"GBK";
648 smEncodings <<
"GB18030";
649 smEncodings <<
"JIS7";
650 smEncodings <<
"SHIFT-JIS";
651 smEncodings <<
"TSCII";
652 smEncodings <<
"UTF-8";
653 smEncodings <<
"UTF-16";
654 smEncodings <<
"KOI8-R";
655 smEncodings <<
"KOI8-U";
656 smEncodings <<
"ISO8859-1";
657 smEncodings <<
"ISO8859-2";
658 smEncodings <<
"ISO8859-3";
659 smEncodings <<
"ISO8859-4";
660 smEncodings <<
"ISO8859-5";
661 smEncodings <<
"ISO8859-6";
662 smEncodings <<
"ISO8859-7";
663 smEncodings <<
"ISO8859-8";
664 smEncodings <<
"ISO8859-8-I";
665 smEncodings <<
"ISO8859-9";
666 smEncodings <<
"ISO8859-10";
667 smEncodings <<
"ISO8859-11";
668 smEncodings <<
"ISO8859-12";
669 smEncodings <<
"ISO8859-13";
670 smEncodings <<
"ISO8859-14";
671 smEncodings <<
"ISO8859-15";
672 smEncodings <<
"IBM 850";
673 smEncodings <<
"IBM 866";
674 smEncodings <<
"CP874";
675 smEncodings <<
"CP1250";
676 smEncodings <<
"CP1251";
677 smEncodings <<
"CP1252";
678 smEncodings <<
"CP1253";
679 smEncodings <<
"CP1254";
680 smEncodings <<
"CP1255";
681 smEncodings <<
"CP1256";
682 smEncodings <<
"CP1257";
683 smEncodings <<
"CP1258";
684 smEncodings <<
"Apple Roman";
685 smEncodings <<
"TIS-620";
686 smEncodings <<
"System";
690 std::sort( sEncodings.begin(), sEncodings.end(), _compareEncodings );
704 return !mErrors.isEmpty();
741 return QSet<QgsMapLayerDependency>();
760 if ( geometry->
wkbType() == providerGeomType )
765 std::unique_ptr< QgsAbstractGeometry > outputGeom;
770 QgsCompoundCurve *compoundCurve = qgsgeometry_cast<QgsCompoundCurve *>( geometry );
773 if ( compoundCurve->
nCurves() == 1 )
775 const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( compoundCurve->
curveAt( 0 ) );
776 if ( circularString )
778 outputGeom.reset( circularString->
clone() );
790 outputGeom.reset( curveGeom );
798 if ( segmentizedGeom )
800 outputGeom.reset( segmentizedGeom );
809 if ( geomCollection )
811 if ( geomCollection->
addGeometry( outputGeom ? outputGeom->clone() : geometry->
clone() ) )
813 outputGeom.reset( collGeom.release() );
823 outputGeom.reset( geometry->
clone() );
825 outputGeom->addZValue();
832 outputGeom.reset( geometry->
clone() );
834 outputGeom->addMValue();
860 QStringList QgsVectorDataProvider::sEncodings;
864 return QList<QgsRelation>();
874 return mTemporalCapabilities.get();
879 return mTemporalCapabilities.get();
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
virtual bool createAttributeIndex(int field)
Create an attribute index on the datasource.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QSet< int > QgsAttributeIds
@ NoCapabilities
Provider has no capabilities.
@ DeleteFeatures
Allows deletion of features.
void setNativeTypes(const QList< QgsVectorDataProvider::NativeType > &nativeTypes)
Set the list of native types supported by this provider.
virtual bool cancelReload()
Cancels the current reloading of data.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
bool hasErrors() const
Provider has errors to report.
virtual QString dataComment() const override
Returns a short comment for the data that this provider is providing access to (e....
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
Setting options for creating vector data providers.
@ TransactionSupport
Supports transactions.
@ DeleteAttributes
Allows deletion of attributes (fields)
QgsRectangle sourceExtent() const override
Returns the extent of all geometries from the source.
void raiseError(const QString &msg) const
Signals an error in this provider.
void fillMinMaxCache() const
Populates the cache of minimum and maximum attribute values.
#define QgsDebugMsgLevel(str, level)
virtual void handlePostCloneOperations(QgsVectorDataProvider *source)
Handles any post-clone operations required after this vector data provider was cloned from the source...
A bundle of parameters controlling aggregate calculation.
QString encoding() const
Returns the encoding which is used for accessing data.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
@ CreateSpatialIndex
Allows creation of spatial index.
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
int count() const
Returns number of items.
QgsVectorDataProvider(const QString &uri=QString(), const QgsDataProvider::ProviderOptions &providerOptions=QgsDataProvider::ProviderOptions())
Constructor for a vector data provider.
@ CreateAttributeIndex
Can create indexes on provider's fields.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
virtual bool empty() const
Returns true if the layer does not contain any feature.
bool supportedType(const QgsField &field) const
check if provider supports type of field
virtual bool isDeleteStyleFromDatabaseSupported() const
It returns false by default.
@ AddAttributes
Allows addition of new attributes (fields)
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
virtual bool skipConstraintCheck(int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value=QVariant()) const
Returns true if a constraint check should be skipped for a specified field (e.g., if the value return...
Type
The WKB type describes the number of dimensions a geometry has.
virtual bool isSaveAndLoadStyleToDatabaseSupported() const
It returns false by default.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Constraint
Constraints which may be present on a field.
QgsFieldConstraints::Constraints fieldConstraints(int fieldIndex) const
Returns any constraints which are present at the provider for a specified field index.
QgsVectorDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
QList< int > QgsAttributeList
virtual void setEncoding(const QString &e)
Set encoding used for accessing data from layer.
virtual QVariant defaultValue(int fieldIndex) const
Returns any literal default values which are present at the provider for a specified field index.
@ ChangeGeometries
Allows modifications of geometries.
void clearMinMaxCache()
Invalidates the min/max cache.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
virtual bool changeFeatures(const QgsChangedAttributesMap &attr_map, const QgsGeometryMap &geometry_map)
Changes attribute values and geometries of existing features.
@ SelectAtId
Fast access to features using their ID.
int nCurves() const
Returns the number of curves in the geometry.
QStringList errors() const
Gets recorded errors.
QList< QgsVectorDataProvider::NativeType > nativeTypes() const
Returns the names of the supported types.
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
QMap< QString, int > fieldNameMap() const
Returns a map where the key is the name of the field and the value is its index.
virtual QVariant aggregate(QgsAggregateCalculator::Aggregate aggregate, int index, const QgsAggregateCalculator::AggregateParameters ¶meters, QgsExpressionContext *context, bool &ok, QgsFeatureIds *fids=nullptr) const
Calculates an aggregated value from the layer's features.
QVariant minimumValue(int index) const override
Returns the minimum value of an attribute.
Circular string geometry type.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
virtual bool truncate()
Removes all features from the layer.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
QgsFeatureSource::FeatureAvailability hasFeatures() const override
Will always return FeatureAvailability::FeaturesAvailable or FeatureAvailability::NoFeaturesAvailable...
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of layer ids on which this layer depends.
@ SimplifyGeometriesWithTopologicalValidation
Supports topological simplification of geometries on provider side according to a distance tolerance.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
virtual bool renameAttributes(const QgsFieldNameMap &renamedAttributes)
Renames existing attributes.
QList< QgsFeature > QgsFeatureList
virtual QgsFeatureRenderer * createRenderer(const QVariantMap &configuration=QVariantMap()) const
Creates a new vector layer feature renderer, using provider backend specific information.
@ AddFeatures
Allows adding features.
virtual QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const
Returns list of indexes to names for QgsPalLabeling fix.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override=0
Query the provider for features specified in request.
QSet< QgsFeatureId > QgsFeatureIds
Abstract base class for all geometries.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
void clearErrors()
Clear recorded errors.
static QStringList availableEncodings()
Returns a list of available encodings.
QgsGeometry convertToProviderType(const QgsGeometry &geom) const
Converts the geometry to the provider type if possible / necessary.
const QgsCurve * curveAt(int i) const
Returns the curve at the specified index.
bool isCanceled() const
Tells whether the operation has been canceled already.
virtual QList< QgsRelation > discoverRelations(const QgsVectorLayer *self, const QList< QgsVectorLayer * > &layers) const
Discover the available relations with the given layers.
virtual QgsAttributeList attributeIndexes() const
Returns list of indexes to fetch all attributes in nextFeature()
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
virtual bool deleteAttributes(const QgsAttributeIds &attributes)
Deletes existing attributes from the provider.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Aggregate
Available aggregates to calculate.
bool nextFeature(QgsFeature &f)
@ ChangeAttributeValues
Allows modification of attribute values.
virtual QgsAbstractVectorLayerLabeling * createLabeling(const QVariantMap &configuration=QVariantMap()) const
Creates labeling settings, using provider backend specific information.
virtual QgsAbstractGeometry * toCurveType() const =0
Returns the geometry converted to the more generic curve type.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
QVariant maximumValue(int index) const override
Returns the maximum value of an attribute.
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
QMap< int, QString > QgsFieldNameMap
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
@ CircularGeometries
Supports circular geometry types (circularstring, compoundcurve, curvepolygon)
QString capabilitiesString() const
Returns the above in friendly format.
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
QTextCodec * textEncoding() const
Gets this providers encoding.
QgsFieldConstraints constraints
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
QHash< int, QString > QgsAttrPalIndexNameHash
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index.
@ RenameAttributes
Supports joint updates for attributes and geometry Providers supporting this should still define Chan...
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
virtual QStringList uniqueStringsMatching(int index, const QString &substring, int limit=-1, QgsFeedback *feedback=nullptr) const
Returns unique string values of an attribute which contain a specified subset string.
void pushError(const QString &msg) const
Push a notification about errors that happened in this providers scope.
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType(QgsWkbTypes::Type t)
Returns empty geometry from wkb type.
QgsCircularString * clone() const override
Clones the geometry by performing a deep copy.
static QVariant convertValue(QVariant::Type type, const QString &value)
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
@ SimplifyGeometries
Supports simplification of geometries on provider side according to a distance tolerance.
QgsWkbTypes::Type wkbType() const override=0
Returns the geometry type which is returned by this layer.
static Type flatType(Type type)
Returns the flat type for a WKB type.
virtual bool isValid() const =0
Returns true if this is a valid layer.
Compound curve geometry type.
virtual QgsCoordinateReferenceSystem crs() const =0
Returns the coordinate system for the data source.