QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsvectordataprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectordataprovider.cpp - DataProvider Interface for vector layers
3  --------------------------------------
4  Date : 26-Oct-2004
5  Copyright : (C) 2004 by Marco Hugentobler
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QTextCodec>
17 
18 #include <cfloat>
19 #include <climits>
20 #include <limits>
21 
22 #include "qgsvectordataprovider.h"
23 #include "qgscircularstring.h"
24 #include "qgscompoundcurve.h"
25 #include "qgsfeature.h"
26 #include "qgsfeatureiterator.h"
27 #include "qgsfeaturerequest.h"
28 #include "qgsfeedback.h"
29 #include "qgsfields.h"
30 #include "qgsgeometry.h"
31 #include "qgsgeometrycollection.h"
32 #include "qgsgeometryfactory.h"
33 #include "qgslogger.h"
34 #include "qgsmessagelog.h"
35 #include "qgssettings.h"
36 #include <mutex>
37 
39  : QgsDataProvider( uri, options )
40 {
41  QgsSettings settings;
42  setEncoding( settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString() );
43 }
44 
46 {
47  return QStringLiteral( "Generic vector file" );
48 }
49 
51 {
52  QgsFeature f;
53  QgsFeatureRequest request;
54  request.setNoAttributes();
56  request.setLimit( 1 );
57  if ( getFeatures( request ).nextFeature( f ) )
58  return false;
59  else
60  return true;
61 }
62 
64 {
65  if ( empty() )
66  return QgsFeatureSource::FeatureAvailability::NoFeaturesAvailable;
67  else
68  return QgsFeatureSource::FeatureAvailability::FeaturesAvailable;
69 }
70 
72 {
73  return crs();
74 }
75 
77 {
78  return extent();
79 }
80 
82 {
83  return QString();
84 }
85 
87 {
88  Q_UNUSED( flist )
89  Q_UNUSED( flags )
90  return false;
91 }
92 
94 {
95  Q_UNUSED( ids )
96  return false;
97 }
98 
100 {
101  if ( !( capabilities() & DeleteFeatures ) )
102  return false;
103 
104  QgsFeatureIds toDelete;
105  QgsFeatureIterator it = getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setNoAttributes() );
106  QgsFeature f;
107  while ( it.nextFeature( f ) )
108  toDelete << f.id();
109 
110  return deleteFeatures( toDelete );
111 }
112 
113 bool QgsVectorDataProvider::addAttributes( const QList<QgsField> &attributes )
114 {
115  Q_UNUSED( attributes )
116  return false;
117 }
118 
120 {
121  Q_UNUSED( attributes )
122  return false;
123 }
124 
126 {
127  Q_UNUSED( renamedAttributes )
128  return false;
129 }
130 
132 {
133  Q_UNUSED( attr_map )
134  return false;
135 }
136 
137 QVariant QgsVectorDataProvider::defaultValue( int fieldId ) const
138 {
139  Q_UNUSED( fieldId )
140  return QVariant();
141 }
142 
143 QString QgsVectorDataProvider::defaultValueClause( int fieldIndex ) const
144 {
145  Q_UNUSED( fieldIndex )
146  return QString();
147 }
148 
149 QgsFieldConstraints::Constraints QgsVectorDataProvider::fieldConstraints( int fieldIndex ) const
150 {
151  QgsFields f = fields();
152  if ( fieldIndex < 0 || fieldIndex >= f.count() )
153  return nullptr;
154 
155  return f.at( fieldIndex ).constraints().constraints();
156 }
157 
159 {
160  return false;
161 }
162 
164 {
165  Q_UNUSED( geometry_map )
166  return false;
167 }
168 
170  const QgsGeometryMap &geometry_map )
171 {
173  return false;
174 
175  bool result = true;
176  result = result && changeAttributeValues( attr_map );
177  result = result && changeGeometryValues( geometry_map );
178  return result;
179 }
180 
182 {
183  return false;
184 }
185 
187 {
188  Q_UNUSED( field )
189  return true;
190 }
191 
192 QgsVectorDataProvider::Capabilities QgsVectorDataProvider::capabilities() const
193 {
195 }
196 
197 
198 void QgsVectorDataProvider::setEncoding( const QString &e )
199 {
200  mEncoding = QTextCodec::codecForName( e.toLocal8Bit().constData() );
201 
202  if ( !mEncoding && e != QLatin1String( "System" ) )
203  {
204  QgsMessageLog::logMessage( tr( "Codec %1 not found. Falling back to system locale" ).arg( e ) );
205  mEncoding = QTextCodec::codecForName( "System" );
206  }
207 
208  if ( !mEncoding )
209  mEncoding = QTextCodec::codecForLocale();
210 
211  Q_ASSERT( mEncoding );
212 }
213 
215 {
216  if ( mEncoding )
217  {
218  return mEncoding->name();
219  }
220 
221  return QString();
222 }
223 
225 {
226  QStringList abilitiesList;
227 
228  int abilities = capabilities();
229 
230  if ( abilities & QgsVectorDataProvider::AddFeatures )
231  {
232  abilitiesList += tr( "Add Features" );
233  }
234 
235  if ( abilities & QgsVectorDataProvider::DeleteFeatures )
236  {
237  abilitiesList += tr( "Delete Features" );
238  }
239 
241  {
242  abilitiesList += tr( "Change Attribute Values" );
243  }
244 
245  if ( abilities & QgsVectorDataProvider::AddAttributes )
246  {
247  abilitiesList += tr( "Add Attributes" );
248  }
249 
250  if ( abilities & QgsVectorDataProvider::DeleteAttributes )
251  {
252  abilitiesList += tr( "Delete Attributes" );
253  }
254 
255  if ( abilities & QgsVectorDataProvider::RenameAttributes )
256  {
257  abilitiesList += tr( "Rename Attributes" );
258  }
259 
261  {
262  // TODO: Tighten up this test. See QgsOgrProvider for details.
263  abilitiesList += tr( "Create Spatial Index" );
264  }
265 
267  {
268  abilitiesList += tr( "Create Attribute Indexes" );
269  }
270 
271  if ( abilities & QgsVectorDataProvider::SelectAtId )
272  {
273  abilitiesList += tr( "Fast Access to Features at ID" );
274  }
275 
276  if ( abilities & QgsVectorDataProvider::ChangeGeometries )
277  {
278  abilitiesList += tr( "Change Geometries" );
279  }
280 
282  {
283  abilitiesList += tr( "Presimplify Geometries" );
284  }
285 
287  {
288  abilitiesList += tr( "Presimplify Geometries with Validity Check" );
289  }
290 
291  if ( abilities & QgsVectorDataProvider::ChangeFeatures )
292  {
293  abilitiesList += tr( "Simultaneous Geometry and Attribute Updates" );
294  }
295 
297  {
298  abilitiesList += tr( "Transactions" );
299  }
300 
302  {
303  abilitiesList += tr( "Curved Geometries" );
304  }
305 
306  return abilitiesList.join( QStringLiteral( ", " ) );
307 }
308 
309 
310 int QgsVectorDataProvider::fieldNameIndex( const QString &fieldName ) const
311 {
312  return fields().lookupField( fieldName );
313 }
314 
315 QMap<QString, int> QgsVectorDataProvider::fieldNameMap() const
316 {
317  QMap<QString, int> resultMap;
318 
319  QgsFields fieldsCopy = fields();
320  for ( int i = 0; i < fieldsCopy.count(); ++i )
321  {
322  resultMap.insert( fieldsCopy.at( i ).name(), i );
323  }
324 
325  return resultMap;
326 }
327 
329 {
330  return fields().allAttributesList();
331 }
332 
334 {
335  return QgsAttributeList();
336 }
337 
338 QList<QgsVectorDataProvider::NativeType> QgsVectorDataProvider::nativeTypes() const
339 {
340  return mNativeTypes;
341 }
342 
344 {
345  return QgsAttrPalIndexNameHash();
346 }
347 
349 {
350  QgsDebugMsgLevel( QStringLiteral( "field name = %1 type = %2 length = %3 precision = %4" )
351  .arg( field.name(),
352  QVariant::typeToName( field.type() ) )
353  .arg( field.length() )
354  .arg( field.precision() ), 2 );
355 
356  const auto constMNativeTypes = mNativeTypes;
357  for ( const NativeType &nativeType : constMNativeTypes )
358  {
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 );
365 
366  if ( field.type() != nativeType.mType )
367  continue;
368 
369  if ( field.length() > 0 )
370  {
371  // source length limited
372  if ( ( nativeType.mMinLen > 0 && field.length() < nativeType.mMinLen ) ||
373  ( nativeType.mMaxLen > 0 && field.length() > nativeType.mMaxLen ) )
374  {
375  // source length exceeds destination limits
376  continue;
377  }
378  }
379 
380  if ( field.precision() > 0 )
381  {
382  // source precision limited
383  if ( ( nativeType.mMinPrec > 0 && field.precision() < nativeType.mMinPrec ) ||
384  ( nativeType.mMaxPrec > 0 && field.precision() > nativeType.mMaxPrec ) )
385  {
386  // source precision exceeds destination limits
387  continue;
388  }
389  }
390 
391  QgsDebugMsg( QStringLiteral( "native type matches" ) );
392  return true;
393  }
394 
395  QgsDebugMsg( QStringLiteral( "no sufficient native type found" ) );
396  return false;
397 }
398 
399 QVariant QgsVectorDataProvider::minimumValue( int index ) const
400 {
401  if ( index < 0 || index >= fields().count() )
402  {
403  QgsDebugMsg( "Warning: access requested to invalid field index: " + QString::number( index ) );
404  return QVariant();
405  }
406 
407  fillMinMaxCache();
408 
409  if ( !mCacheMinValues.contains( index ) )
410  return QVariant();
411 
412  return mCacheMinValues[index];
413 }
414 
415 QVariant QgsVectorDataProvider::maximumValue( int index ) const
416 {
417  if ( index < 0 || index >= fields().count() )
418  {
419  QgsDebugMsg( "Warning: access requested to invalid field index: " + QString::number( index ) );
420  return QVariant();
421  }
422 
423  fillMinMaxCache();
424 
425  if ( !mCacheMaxValues.contains( index ) )
426  return QVariant();
427 
428  return mCacheMaxValues[index];
429 }
430 
431 
432 QStringList QgsVectorDataProvider::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
433 {
434  QStringList results;
435 
436  // Safety belt
437  if ( index < 0 || index >= fields().count() )
438  return results;
439 
440  QgsFeature f;
441  QgsAttributeList keys;
442  keys.append( index );
443 
444  QgsFeatureRequest request;
445  request.setSubsetOfAttributes( keys );
447  QString fieldName = fields().at( index ).name();
448  request.setFilterExpression( QStringLiteral( "\"%1\" ILIKE '%%2%'" ).arg( fieldName, substring ) );
449  QgsFeatureIterator fi = getFeatures( request );
450 
451  QSet<QString> set;
452 
453  while ( fi.nextFeature( f ) )
454  {
455  QString value = f.attribute( index ).toString();
456  if ( !set.contains( value ) )
457  {
458  results.append( value );
459  set.insert( value );
460  }
461 
462  if ( ( limit >= 0 && results.size() >= limit ) || ( feedback && feedback->isCanceled() ) )
463  break;
464  }
465  return results;
466 }
467 
469  const QgsAggregateCalculator::AggregateParameters &parameters, QgsExpressionContext *context, bool &ok, QgsFeatureIds *fids ) const
470 {
471  //base implementation does nothing
472  Q_UNUSED( aggregate )
473  Q_UNUSED( index )
474  Q_UNUSED( parameters )
475  Q_UNUSED( context )
476  Q_UNUSED( fids )
477 
478  ok = false;
479  return QVariant();
480 }
481 
483 {
484  mCacheMinMaxDirty = true;
485  mCacheMinValues.clear();
486  mCacheMaxValues.clear();
487 }
488 
490 {
491  if ( !mCacheMinMaxDirty )
492  return;
493 
494  QgsFields flds = fields();
495  for ( int i = 0; i < flds.count(); ++i )
496  {
497  if ( flds.at( i ).type() == QVariant::Int )
498  {
499  mCacheMinValues[i] = QVariant( std::numeric_limits<int>::max() );
500  mCacheMaxValues[i] = QVariant( std::numeric_limits<int>::lowest() );
501  }
502  else if ( flds.at( i ).type() == QVariant::LongLong )
503  {
504  mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
505  mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::lowest() );
506  }
507  else if ( flds.at( i ).type() == QVariant::Double )
508  {
509  mCacheMinValues[i] = QVariant( std::numeric_limits<double>::max() );
510  mCacheMaxValues[i] = QVariant( std::numeric_limits<double>::lowest() );
511 
512  }
513  else
514  {
515  mCacheMinValues[i] = QVariant();
516  mCacheMaxValues[i] = QVariant();
517  }
518  }
519 
520  QgsFeature f;
521  const QgsAttributeList keys = mCacheMinValues.keys();
522  QgsFeatureIterator fi = getFeatures( QgsFeatureRequest().setSubsetOfAttributes( keys )
523  .setFlags( QgsFeatureRequest::NoGeometry ) );
524 
525  while ( fi.nextFeature( f ) )
526  {
527  QgsAttributes attrs = f.attributes();
528  for ( int attributeIndex : keys )
529  {
530  const QVariant &varValue = attrs.at( attributeIndex );
531 
532  if ( varValue.isNull() )
533  continue;
534 
535  switch ( flds.at( attributeIndex ).type() )
536  {
537  case QVariant::Int:
538  {
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;
544  break;
545  }
546  case QVariant::LongLong:
547  {
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;
553  break;
554  }
555  case QVariant::Double:
556  {
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;
562  break;
563  }
564  default:
565  {
566  QString value = varValue.toString();
567  if ( mCacheMinValues[ attributeIndex ].isNull() || value < mCacheMinValues[attributeIndex ].toString() )
568  {
569  mCacheMinValues[attributeIndex] = value;
570  }
571  if ( mCacheMaxValues[attributeIndex].isNull() || value > mCacheMaxValues[attributeIndex].toString() )
572  {
573  mCacheMaxValues[attributeIndex] = value;
574  }
575  break;
576  }
577  }
578  }
579  }
580 
581  mCacheMinMaxDirty = false;
582 }
583 
584 QVariant QgsVectorDataProvider::convertValue( QVariant::Type type, const QString &value )
585 {
586  QVariant v( value );
587 
588  if ( !v.convert( type ) || value.isNull() )
589  v = QVariant( type );
590 
591  return v;
592 }
593 
595 {
596  return nullptr;
597 }
598 
600 {
601  emit dataChanged();
602 }
603 
604 static bool _compareEncodings( const QString &s1, const QString &s2 )
605 {
606  return s1.toLower() < s2.toLower();
607 }
608 
610 {
611  static std::once_flag initialized;
612  std::call_once( initialized, [ = ]
613  {
614  const auto codecs { QTextCodec::availableCodecs() };
615  for ( const QString &codec : codecs )
616  {
617  sEncodings << codec;
618  }
619 #if 0
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";
665 #endif
666 
667  // Do case-insensitive sorting of encodings
668  std::sort( sEncodings.begin(), sEncodings.end(), _compareEncodings );
669 
670  } );
671 
672  return sEncodings;
673 }
674 
676 {
677  mErrors.clear();
678 }
679 
681 {
682  return !mErrors.isEmpty();
683 }
684 
685 QStringList QgsVectorDataProvider::errors() const
686 {
687  return mErrors;
688 }
689 
691 {
692  return false;
693 }
694 
696 {
697  return false;
698 }
699 
701 {
702  return nullptr;
703 }
704 
706 {
707  return nullptr;
708 }
709 
710 void QgsVectorDataProvider::pushError( const QString &msg ) const
711 {
712  QgsDebugMsg( msg );
713  mErrors << msg;
714  emit raiseError( msg );
715 }
716 
717 QSet<QgsMapLayerDependency> QgsVectorDataProvider::dependencies() const
718 {
719  return QSet<QgsMapLayerDependency>();
720 }
721 
723 {
724  if ( geom.isNull() )
725  {
726  return QgsGeometry();
727  }
728 
729  const QgsAbstractGeometry *geometry = geom.constGet();
730  if ( !geometry )
731  {
732  return QgsGeometry();
733  }
734 
735  QgsWkbTypes::Type providerGeomType = wkbType();
736 
737  //geom is already in the provider geometry type
738  if ( geometry->wkbType() == providerGeomType )
739  {
740  return QgsGeometry();
741  }
742 
743  std::unique_ptr< QgsAbstractGeometry > outputGeom;
744 
745  //convert compoundcurve to circularstring (possible if compoundcurve consists of one circular string)
746  if ( QgsWkbTypes::flatType( providerGeomType ) == QgsWkbTypes::CircularString )
747  {
748  QgsCompoundCurve *compoundCurve = qgsgeometry_cast<QgsCompoundCurve *>( geometry );
749  if ( compoundCurve )
750  {
751  if ( compoundCurve->nCurves() == 1 )
752  {
753  const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( compoundCurve->curveAt( 0 ) );
754  if ( circularString )
755  {
756  outputGeom.reset( circularString->clone() );
757  }
758  }
759  }
760  }
761 
762  //convert to curved type if necessary
763  if ( !QgsWkbTypes::isCurvedType( geometry->wkbType() ) && QgsWkbTypes::isCurvedType( providerGeomType ) )
764  {
765  QgsAbstractGeometry *curveGeom = outputGeom ? outputGeom->toCurveType() : geometry->toCurveType();
766  if ( curveGeom )
767  {
768  outputGeom.reset( curveGeom );
769  }
770  }
771 
772  //convert to linear type from curved type
773  if ( QgsWkbTypes::isCurvedType( geometry->wkbType() ) && !QgsWkbTypes::isCurvedType( providerGeomType ) )
774  {
775  QgsAbstractGeometry *segmentizedGeom = outputGeom ? outputGeom->segmentize() : geometry->segmentize();
776  if ( segmentizedGeom )
777  {
778  outputGeom.reset( segmentizedGeom );
779  }
780  }
781 
782  //convert to multitype if necessary
783  if ( QgsWkbTypes::isMultiType( providerGeomType ) && !QgsWkbTypes::isMultiType( geometry->wkbType() ) )
784  {
785  std::unique_ptr< QgsAbstractGeometry > collGeom( QgsGeometryFactory::geomFromWkbType( providerGeomType ) );
786  QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( collGeom.get() );
787  if ( geomCollection )
788  {
789  if ( geomCollection->addGeometry( outputGeom ? outputGeom->clone() : geometry->clone() ) )
790  {
791  outputGeom.reset( collGeom.release() );
792  }
793  }
794  }
795 
796  //set z/m types
797  if ( QgsWkbTypes::hasZ( providerGeomType ) )
798  {
799  if ( !outputGeom )
800  {
801  outputGeom.reset( geometry->clone() );
802  }
803  outputGeom->addZValue();
804  }
805 
806  if ( QgsWkbTypes::hasM( providerGeomType ) )
807  {
808  if ( !outputGeom )
809  {
810  outputGeom.reset( geometry->clone() );
811  }
812  outputGeom->addMValue();
813  }
814 
815  if ( outputGeom )
816  {
817  return QgsGeometry( outputGeom.release() );
818  }
819 
820  return QgsGeometry();
821 }
822 
823 void QgsVectorDataProvider::setNativeTypes( const QList<NativeType> &nativeTypes )
824 {
825  mNativeTypes = nativeTypes;
826 }
827 
829 {
830  return mEncoding;
831 }
832 
834 {
835  return false;
836 }
837 
838 QStringList QgsVectorDataProvider::sEncodings;
839 
840 QList<QgsRelation> QgsVectorDataProvider::discoverRelations( const QgsVectorLayer *, const QList<QgsVectorLayer *> & ) const
841 {
842  return QList<QgsRelation>();
843 }
844 
846 {
847 
848 }
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:324
QgsFeatureId id
Definition: qgsfeature.h:64
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
Definition: qgsfeature.h:566
static QVariant convertValue(QVariant::Type type, const QString &value)
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QVariant maximumValue(int index) const override
Returns the maximum value of an attribute.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
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.
QString name
Definition: qgsfield.h:58
QVariant minimumValue(int index) const override
Returns the minimum value of an attribute.
int precision
Definition: qgsfield.h:55
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:706
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.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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
Definition: qgsfeature.h:571
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.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
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...
Definition: qgsfeature.h:55
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.
Definition: qgsfields.cpp:133
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:917
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)
Definition: qgsfields.cpp:163
int length
Definition: qgsfield.h:54
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider&#39;...
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
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.
Definition: qgswkbtypes.h:68
Can create indexes on provider&#39;s fields.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfields.cpp:351
void fillMinMaxCache() const
Populates the cache of minimum and maximum attribute values.
Allows addition of new attributes (fields)
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
void clearMinMaxCache()
Invalidates the min/max cache.
Geometry collection.
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 &parameters, QgsExpressionContext *context, bool &ok, QgsFeatureIds *fids=nullptr) const
Calculates an aggregated value from the layer&#39;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.
Definition: qgsfield.h:48
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
Definition: qgsattributes.h:44
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
Definition: qgsfield.h:61
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:557
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.
Definition: qgswkbtypes.h:755
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...
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:967
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
Definition: qgsfield.h:27
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.
A vector of attributes.
Definition: qgsattributes.h:57
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.
Definition: qgsfeature.cpp:262
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:576
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.
QVariant::Type type
Definition: qgsfield.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
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.