42 setEncoding( settings.
value( QStringLiteral(
"UI/encoding" ),
"System" ).toString() );
47 return QStringLiteral(
"Generic vector file" );
66 return QgsFeatureSource::FeatureAvailability::NoFeaturesAvailable;
68 return QgsFeatureSource::FeatureAvailability::FeaturesAvailable;
115 Q_UNUSED( attributes )
121 Q_UNUSED( attributes )
127 Q_UNUSED( renamedAttributes )
145 Q_UNUSED( fieldIndex )
152 if ( fieldIndex < 0 || fieldIndex >= f.
count() )
165 Q_UNUSED( geometry_map )
200 mEncoding = QTextCodec::codecForName( e.toLocal8Bit().constData() );
202 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 ) )
391 QgsDebugMsg( QStringLiteral(
"native type matches" ) );
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() ) )
472 Q_UNUSED( aggregate )
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;
566 QString value = varValue.toString();
567 if ( mCacheMinValues[ attributeIndex ].isNull() || value < mCacheMinValues[attributeIndex ].toString() )
569 mCacheMinValues[attributeIndex] = value;
571 if ( mCacheMaxValues[attributeIndex].isNull() || value > mCacheMaxValues[attributeIndex].toString() )
573 mCacheMaxValues[attributeIndex] = value;
581 mCacheMinMaxDirty =
false;
588 if ( !v.convert( type ) || value.isNull() )
589 v = QVariant( type );
604 static bool _compareEncodings(
const QString &s1,
const QString &s2 )
606 return s1.toLower() < s2.toLower();
611 static std::once_flag initialized;
612 std::call_once( initialized, [ = ]
614 const auto codecs { QTextCodec::availableCodecs() };
615 for (
const QString &codec : codecs )
620 smEncodings <<
"BIG5";
621 smEncodings <<
"BIG5-HKSCS";
622 smEncodings <<
"EUCJP";
623 smEncodings <<
"EUCKR";
624 smEncodings <<
"GB2312";
625 smEncodings <<
"GBK";
626 smEncodings <<
"GB18030";
627 smEncodings <<
"JIS7";
628 smEncodings <<
"SHIFT-JIS";
629 smEncodings <<
"TSCII";
630 smEncodings <<
"UTF-8";
631 smEncodings <<
"UTF-16";
632 smEncodings <<
"KOI8-R";
633 smEncodings <<
"KOI8-U";
634 smEncodings <<
"ISO8859-1";
635 smEncodings <<
"ISO8859-2";
636 smEncodings <<
"ISO8859-3";
637 smEncodings <<
"ISO8859-4";
638 smEncodings <<
"ISO8859-5";
639 smEncodings <<
"ISO8859-6";
640 smEncodings <<
"ISO8859-7";
641 smEncodings <<
"ISO8859-8";
642 smEncodings <<
"ISO8859-8-I";
643 smEncodings <<
"ISO8859-9";
644 smEncodings <<
"ISO8859-10";
645 smEncodings <<
"ISO8859-11";
646 smEncodings <<
"ISO8859-12";
647 smEncodings <<
"ISO8859-13";
648 smEncodings <<
"ISO8859-14";
649 smEncodings <<
"ISO8859-15";
650 smEncodings <<
"IBM 850";
651 smEncodings <<
"IBM 866";
652 smEncodings <<
"CP874";
653 smEncodings <<
"CP1250";
654 smEncodings <<
"CP1251";
655 smEncodings <<
"CP1252";
656 smEncodings <<
"CP1253";
657 smEncodings <<
"CP1254";
658 smEncodings <<
"CP1255";
659 smEncodings <<
"CP1256";
660 smEncodings <<
"CP1257";
661 smEncodings <<
"CP1258";
662 smEncodings <<
"Apple Roman";
663 smEncodings <<
"TIS-620";
664 smEncodings <<
"System";
668 std::sort( sEncodings.begin(), sEncodings.end(), _compareEncodings );
682 return !mErrors.isEmpty();
719 return QSet<QgsMapLayerDependency>();
738 if ( geometry->
wkbType() == providerGeomType )
743 std::unique_ptr< QgsAbstractGeometry > outputGeom;
751 if ( compoundCurve->
nCurves() == 1 )
754 if ( circularString )
756 outputGeom.reset( circularString->
clone() );
768 outputGeom.reset( curveGeom );
776 if ( segmentizedGeom )
778 outputGeom.reset( segmentizedGeom );
787 if ( geomCollection )
789 if ( geomCollection->
addGeometry( outputGeom ? outputGeom->clone() : geometry->
clone() ) )
791 outputGeom.reset( collGeom.release() );
801 outputGeom.reset( geometry->
clone() );
810 outputGeom.reset( geometry->
clone() );
812 outputGeom->addMValue();
838 QStringList QgsVectorDataProvider::sEncodings;
842 return QList<QgsRelation>();
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QString encoding() const
Gets encoding which is used for accessing data.
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
static QVariant convertValue(QVariant::Type type, const QString &value)
A rectangle specified with double values.
QVariant maximumValue(int index) const override
Returns the maximum value of an attribute.
QSet< QgsFeatureId > QgsFeatureIds
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType(QgsWkbTypes::Type t)
Returns empty geometry from wkb type.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
QgsWkbTypes::Type wkbType() const override=0
Returns the geometry type which is returned by this layer.
QMap< QString, int > fieldNameMap() const
Returns a map where the key is the name of the field and the value is its index.
virtual QgsAbstractGeometry * toCurveType() const =0
Returns the geometry converted to the more generic curve type.
QVariant minimumValue(int index) const override
Returns the minimum value of an attribute.
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
This class is a composition of two QSettings instances:
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Constraint
Constraints which may be present on a field.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsFieldConstraints::Constraints fieldConstraints(int fieldIndex) const
Returns any constraints which are present at the provider for a specified field index.
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=nullptr) override
Adds a list of features to the sink.
QList< QgsFeature > QgsFeatureList
Supports simplification of geometries on provider side according to a distance tolerance.
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of layer ids on which this layer depends.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
virtual void setEncoding(const QString &e)
Set encoding used for accessing data from layer.
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
virtual bool renameAttributes(const QgsFieldNameMap &renamedAttributes)
Renames existing attributes.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
void pushError(const QString &msg) const
Push a notification about errors that happened in this providers scope.
void raiseError(const QString &msg) const
Signals an error in this provider.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
virtual QgsFeatureRenderer * createRenderer(const QVariantMap &configuration=QVariantMap()) const
Creates a new vector layer feature renderer, using provider backend specific information.
Abstract base class for spatial data provider implementations.
Allows deletion of attributes (fields)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QList< QgsVectorDataProvider::NativeType > nativeTypes() const
Returns the names of the supported types.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
int count() const
Returns number of items.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
QgsVectorDataProvider(const QString &uri=QString(), const QgsDataProvider::ProviderOptions &providerOptions=QgsDataProvider::ProviderOptions())
Constructor for a vector data provider.
virtual bool createAttributeIndex(int field)
Create an attribute index on the datasource.
bool supportedType(const QgsField &field) const
check if provider supports type of field
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
Base class for feedback objects to be used for cancellation of something running in a worker thread...
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...
void setNativeTypes(const QList< QgsVectorDataProvider::NativeType > &nativeTypes)
Set the list of native types supported by this provider.
Allows creation of spatial index.
QSet< int > QgsAttributeIds
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Type
The WKB type describes the number of dimensions a geometry has.
Can create indexes on provider's fields.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
void fillMinMaxCache() const
Populates the cache of minimum and maximum attribute values.
Allows addition of new attributes (fields)
#define QgsDebugMsgLevel(str, level)
void clearMinMaxCache()
Invalidates the min/max cache.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
virtual QgsCoordinateReferenceSystem crs() const =0
Returns the coordinate system for the data source.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual bool changeFeatures(const QgsChangedAttributesMap &attr_map, const QgsGeometryMap &geometry_map)
Changes attribute values and geometries of existing features.
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).
QStringList errors() const
Gets recorded errors.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
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.
Allows modifications of geometries.
void clearErrors()
Clear recorded errors.
QgsGeometry convertToProviderType(const QgsGeometry &geom) const
Converts the geometry to the provider type if possible / necessary.
virtual bool isDeleteStyleFromDatabaseSupported() const
It returns false by default.
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
Encapsulate a field in an attribute table or data source.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
virtual bool isSaveAndLoadStyleToDatabaseSupported() const
It returns false by default.
Abstract base class for all geometries.
Fast access to features using their ID.
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
QMap< int, QString > QgsFieldNameMap
virtual QgsAbstractVectorLayerLabeling * createLabeling(const QVariantMap &configuration=QVariantMap()) const
Creates labeling settings, using provider backend specific information.
Supports circular geometry types (circularstring, compoundcurve, curvepolygon)
virtual void forceReload()
Forces a reload of the underlying datasource if the provider implements this method.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
int nCurves() const
Returns the number of curves in the geometry.
Supports topological simplification of geometries on provider side according to a distance tolerance...
QString capabilitiesString() const
Returns the above in friendly format.
Setting options for creating vector data providers.
QgsFieldConstraints constraints
bool isCanceled() const
Tells whether the operation has been canceled already.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override=0
Query the provider for features specified in request.
virtual bool cancelReload()
Cancels the current reloading of data.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
QgsCircularString * clone() const override
Clones the geometry by performing a deep copy.
bool hasErrors() const
Provider has errors to report.
virtual bool truncate()
Removes all features from the layer.
virtual QString dataComment() const
Returns a short comment for the data that this provider is providing access to (e.g.
This class represents a coordinate reference system (CRS).
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.
const QgsCurve * curveAt(int i) const
Returns the curve at the specified index.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QgsAttributeList attributeIndexes() const
Returns list of indexes to fetch all attributes in nextFeature()
QgsFeatureSource::FeatureAvailability hasFeatures() const override
Will always return FeatureAvailability::FeaturesAvailable or FeatureAvailability::NoFeaturesAvailable...
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
virtual bool deleteAttributes(const QgsAttributeIds &attributes)
Deletes existing attributes from the provider.
virtual void handlePostCloneOperations(QgsVectorDataProvider *source)
Handles any post-clone operations required after this vector data provider was cloned from the source...
Allows deletion of features.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Compound curve geometry type.
Circular string geometry type.
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
virtual QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Provider has no capabilities.
This is the base class for vector data providers.
virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const
Returns list of indexes to names for QgsPalLabeling fix.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
virtual bool empty() const
Returns true if the layer does not contain any feature.
Represents a vector layer which manages a vector based data sets.
QTextCodec * textEncoding() const
Gets this providers encoding.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
static Type flatType(Type type)
Returns the flat type for a WKB type.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
QHash< int, QString > QgsAttrPalIndexNameHash
Allows modification of attribute values.
static QStringList availableEncodings()
Returns a list of available encodings.
Aggregate
Available aggregates to calculate.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Supports joint updates for attributes and geometry Providers supporting this should still define Chan...
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsRectangle sourceExtent() const override
Returns the extent of all geometries from the source.
A bundle of parameters controlling aggregate calculation.
virtual QVariant defaultValue(int fieldIndex) const
Returns any literal default values which are present at the provider for a specified field index...
virtual QList< QgsRelation > discoverRelations(const QgsVectorLayer *self, const QList< QgsVectorLayer *> &layers) const
Discover the available relations with the given layers.