QGIS API Documentation  2.99.0-Master (7d4f81d)
qgsvectorlayerfeatureiterator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerfeatureiterator.cpp
3  ---------------------
4  begin : Dezember 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
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  ***************************************************************************/
16 
18 #include "qgsgeometrysimplifier.h"
19 #include "qgssimplifymethod.h"
20 #include "qgsvectordataprovider.h"
22 #include "qgsvectorlayer.h"
24 #include "qgsexpressioncontext.h"
25 #include "qgsdistancearea.h"
26 #include "qgsproject.h"
27 #include "qgsmessagelog.h"
28 #include "qgsexception.h"
29 
31 {
32  QMutexLocker locker( &layer->mFeatureSourceConstructorMutex );
34  mFields = layer->fields();
35 
36  // update layer's join caches if necessary
37  if ( layer->mJoinBuffer->containsJoins() )
38  layer->mJoinBuffer->createJoinCaches();
39 
40  mJoinBuffer = layer->mJoinBuffer->clone();
41 
42  mExpressionFieldBuffer = new QgsExpressionFieldBuffer( *layer->mExpressionFieldBuffer );
43  mCrs = layer->crs();
44 
45  mHasEditBuffer = layer->editBuffer();
46  if ( mHasEditBuffer )
47  {
48 #if 0
49  // TODO[MD]: after merge
50  if ( request.filterType() == QgsFeatureRequest::FilterFid )
51  {
52 
53  // only copy relevant parts
54  if ( L->editBuffer()->addedFeatures().contains( request.filterFid() ) )
55  mAddedFeatures.insert( request.filterFid(), L->editBuffer()->addedFeatures()[ request.filterFid()] );
56 
57  if ( L->editBuffer()->changedGeometries().contains( request.filterFid() ) )
58  mChangedGeometries.insert( request.filterFid(), L->editBuffer()->changedGeometries()[ request.filterFid()] );
59 
60  if ( L->editBuffer()->deletedFeatureIds().contains( request.filterFid() ) )
61  mDeletedFeatureIds.insert( request.filterFid() );
62 
63  if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
64  mChangedAttributeValues.insert( request.filterFid(), L->editBuffer()->changedAttributeValues()[ request.filterFid()] );
65 
66  if ( L->editBuffer()->changedAttributeValues().contains( request.filterFid() ) )
67  mChangedFeaturesRequest.setFilterFids( QgsFeatureIds() << request.filterFid() );
68  }
69  else
70  {
71 #endif
76  mAddedAttributes = QList<QgsField>( layer->editBuffer()->addedAttributes() );
78 #if 0
79  }
80 #endif
81  }
82 
83  std::unique_ptr< QgsExpressionContextScope > layerScope( QgsExpressionContextUtils::layerScope( layer ) );
84  mLayerScope = *layerScope;
85 }
86 
88 {
89  delete mJoinBuffer;
92 }
93 
95 {
96  // return feature iterator that does not own this source
97  return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( this, false, request ) );
98 }
99 
101 {
102  return mFields;
103 }
104 
106 {
107  return mCrs;
108 }
109 
110 
113  , mFetchedFid( false )
114  , mInterruptionChecker( nullptr )
115 {
117  {
119  }
120  try
121  {
123  }
124  catch ( QgsCsException & )
125  {
126  // can't reproject mFilterRect
127  mClosed = true;
128  return;
129  }
130  if ( !mFilterRect.isNull() )
131  {
132  // update request to be the unprojected filter rect
134  }
135 
137  {
140 
142  {
143  //ensure that all fields required for filter expressions are prepared
144  QSet<int> attributeIndexes = mRequest.filterExpression()->referencedAttributeIndexes( mSource->mFields );
145  attributeIndexes += mRequest.subsetOfAttributes().toSet();
146  mRequest.setSubsetOfAttributes( attributeIndexes.toList() );
147  }
148  }
149 
150  prepareFields();
151 
152  mHasVirtualAttributes = !mFetchJoinInfo.isEmpty() || !mExpressionFieldInfo.isEmpty();
153 
154  // by default provider's request is the same
156  // but we remove any destination CRS parameter - that is handled in QgsVectorLayerFeatureIterator,
157  // not at the provider level. Otherwise virtual fields depending on geometry would have incorrect
158  // values
159  if ( mRequest.destinationCrs().isValid() )
160  {
162  }
163 
165  {
166  // prepare list of attributes to match provider fields
167  QSet<int> providerSubset;
169  int nPendingFields = mSource->mFields.count();
170  Q_FOREACH ( int attrIndex, subset )
171  {
172  if ( attrIndex < 0 || attrIndex >= nPendingFields )
173  continue;
174  if ( mSource->mFields.fieldOrigin( attrIndex ) == QgsFields::OriginProvider )
175  providerSubset << mSource->mFields.fieldOriginIndex( attrIndex );
176  }
177 
178  // This is done in order to be prepared to do fallback order bys
179  // and be sure we have the required columns.
180  // TODO:
181  // It would be nicer to first check if we can compile the order by
182  // and only modify the subset if we cannot.
183  if ( !mProviderRequest.orderBy().isEmpty() )
184  {
185  Q_FOREACH ( const QString &attr, mProviderRequest.orderBy().usedAttributes() )
186  {
187  providerSubset << mSource->mFields.lookupField( attr );
188  }
189  }
190 
191  mProviderRequest.setSubsetOfAttributes( providerSubset.toList() );
192  }
193 
195  {
196  Q_FOREACH ( const QString &field, mProviderRequest.filterExpression()->referencedColumns() )
197  {
198  int idx = source->mFields.lookupField( field );
199 
200  // If there are fields in the expression which are not of origin provider, the provider will not be able to filter based on them.
201  // In this case we disable the expression filter.
202  if ( source->mFields.fieldOrigin( idx ) != QgsFields::OriginProvider )
203  {
205  // can't limit at provider side
207  }
208  }
209  }
210 
211  if ( mSource->mHasEditBuffer )
212  {
214  QgsFeatureIds changedIds;
215  QgsChangedAttributesMap::const_iterator attIt = mSource->mChangedAttributeValues.constBegin();
216  for ( ; attIt != mSource->mChangedAttributeValues.constEnd(); ++attIt )
217  {
218  changedIds << attIt.key();
219  }
221 
222  if ( mChangedFeaturesRequest.limit() > 0 )
223  {
224  int providerLimit = mProviderRequest.limit();
225 
226  // features may be deleted in buffer, so increase limit sent to provider
227  providerLimit += mSource->mDeletedFeatureIds.size();
228 
230  {
231  // attribute changes may mean some features no longer match expression, so increase limit sent to provider
232  providerLimit += mSource->mChangedAttributeValues.size();
233  }
234 
236  {
237  // geometry changes may mean some features no longer match expression or rect, so increase limit sent to provider
238  providerLimit += mSource->mChangedGeometries.size();
239  }
240 
241  mProviderRequest.setLimit( providerLimit );
242  mChangedFeaturesRequest.setLimit( providerLimit );
243  }
244  }
245 
246  if ( request.filterType() == QgsFeatureRequest::FilterFid )
247  {
248  mFetchedFid = false;
249  }
250  else // no filter or filter by rect
251  {
252  if ( mSource->mHasEditBuffer )
253  {
255  }
256  else
257  {
259  }
260 
262  }
263 }
264 
265 
267 {
268  qDeleteAll( mExpressionFieldInfo );
269 
270  close();
271 }
272 
273 
274 
276 {
277  f.setValid( false );
278 
279  if ( mClosed )
280  return false;
281 
283  {
284  if ( mFetchedFid )
285  return false;
286  bool res = nextFeatureFid( f );
287  if ( res && postProcessFeature( f ) )
288  {
289  mFetchedFid = true;
290  return res;
291  }
292  else
293  {
294  return false;
295  }
296  }
297 
298  if ( !mFilterRect.isNull() )
299  {
300  if ( fetchNextChangedGeomFeature( f ) )
301  return true;
302 
303  // no more changed geometries
304  }
305 
307  {
309  return true;
310 
311  if ( fetchNextChangedGeomFeature( f ) )
312  return true;
313 
314  // no more changed features
315  }
316 
317  while ( fetchNextAddedFeature( f ) )
318  {
319  return true;
320  }
321  // no more added features
322 
323  if ( mProviderIterator.isClosed() )
324  {
327  mProviderIterator.setInterruptionChecker( mInterruptionChecker );
328  }
329 
330  while ( mProviderIterator.nextFeature( f ) )
331  {
332  if ( mFetchConsidered.contains( f.id() ) )
333  continue;
334 
335  // TODO[MD]: just one resize of attributes
336  f.setFields( mSource->mFields );
337 
338  // update attributes
339  if ( mSource->mHasEditBuffer )
341 
342  if ( mHasVirtualAttributes )
344 
346  {
347  //filtering by expression, and couldn't do it on the provider side
349  if ( !mRequest.filterExpression()->evaluate( mRequest.expressionContext() ).toBool() )
350  {
351  //feature did not match filter
352  continue;
353  }
354  }
355 
356  // update geometry
357  // TODO[MK]: FilterRect check after updating the geometry
360 
361  if ( !postProcessFeature( f ) )
362  continue;
363 
364  return true;
365  }
366  // no more provider features
367 
368  close();
369  return false;
370 }
371 
372 
373 
375 {
376  if ( mClosed )
377  return false;
378 
380  {
381  mFetchedFid = false;
382  }
383  else
384  {
387  }
388 
389  return true;
390 }
391 
393 {
394  if ( mClosed )
395  return false;
396 
398 
399  iteratorClosed();
400 
401  mClosed = true;
402  return true;
403 }
404 
406 {
407  mProviderIterator.setInterruptionChecker( interruptionChecker );
408  mInterruptionChecker = interruptionChecker;
409 }
410 
412 {
413  while ( mFetchAddedFeaturesIt-- != mSource->mAddedFeatures.constBegin() )
414  {
416 
417  if ( mFetchConsidered.contains( fid ) )
418  // must have changed geometry outside rectangle
419  continue;
420 
422 
423  // can't test for feature acceptance until after calling useAddedFeature
424  // since acceptFeature may rely on virtual fields
425  if ( !mRequest.acceptFeature( f ) )
426  // skip features which are not accepted by the filter
427  continue;
428 
429  if ( !postProcessFeature( f ) )
430  continue;
431 
432  return true;
433  }
434 
436  return false; // no more added features
437 }
438 
439 
441 {
442  // since QgsFeature is implicitly shared, it's more efficient to just copy the
443  // whole feature, even if flags like NoGeometry or a subset of attributes is set at the request.
444  // This helps potentially avoid an unnecessary detach of the feature
445  f = src;
446  f.setValid( true );
447  f.setFields( mSource->mFields );
448 
449  if ( mHasVirtualAttributes )
451 }
452 
453 
454 
456 {
457  // check if changed geometries are in rectangle
459  {
460  QgsFeatureId fid = mFetchChangedGeomIt.key();
461 
462  if ( mFetchConsidered.contains( fid ) )
463  // skip deleted features
464  continue;
465 
466  mFetchConsidered << fid;
467 
468  if ( !mFilterRect.isNull() && !mFetchChangedGeomIt->intersects( mFilterRect ) )
469  // skip changed geometries not in rectangle and don't check again
470  continue;
471 
473 
475  {
477  if ( !mRequest.filterExpression()->evaluate( mRequest.expressionContext() ).toBool() )
478  {
479  continue;
480  }
481  }
482 
483  if ( postProcessFeature( f ) )
484  {
485  // return complete feature
487  return true;
488  }
489  }
490 
491  return false; // no more changed geometries
492 }
493 
495 {
497  {
498  if ( mFetchConsidered.contains( f.id() ) )
499  // skip deleted features and those already handled by the geometry
500  continue;
501 
502  mFetchConsidered << f.id();
503 
505 
506  if ( mHasVirtualAttributes )
508 
510  if ( mRequest.filterExpression()->evaluate( mRequest.expressionContext() ).toBool() && postProcessFeature( f ) )
511  {
512  return true;
513  }
514  }
515 
516  return false;
517 }
518 
519 
521 {
522  f.setId( fid );
523  f.setValid( true );
524  f.setFields( mSource->mFields );
525 
528  {
529  f.setGeometry( geom );
530  }
531 
532  bool subsetAttrs = ( mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes );
533  if ( !subsetAttrs || !mRequest.subsetOfAttributes().isEmpty() )
534  {
535  // retrieve attributes from provider
536  QgsFeature tmp;
537  //mDataProvider->featureAtId( fid, tmp, false, mFetchProvAttributes );
538  QgsFeatureRequest request;
540  if ( subsetAttrs )
541  {
543  }
545  if ( fi.nextFeature( tmp ) )
546  {
549  f.setAttributes( tmp.attributes() );
550  }
551  }
552 
554 }
555 
556 
557 
559 {
561 
564 }
565 
567 {
568  if ( !mSource->mFields.exists( fieldIdx ) )
569  return;
570 
571  if ( mSource->mFields.fieldOrigin( fieldIdx ) != QgsFields::OriginJoin )
572  return;
573 
574  int sourceLayerIndex;
575  const QgsVectorLayerJoinInfo *joinInfo = mSource->mJoinBuffer->joinForFieldIndex( fieldIdx, mSource->mFields, sourceLayerIndex );
576  Q_ASSERT( joinInfo );
577 
578  QgsVectorLayer *joinLayer = joinInfo->joinLayer();
579  if ( !joinLayer )
580  return; // invalid join (unresolved reference to layer)
581 
582  if ( !mFetchJoinInfo.contains( joinInfo ) )
583  {
584  FetchJoinInfo info;
585  info.joinInfo = joinInfo;
586  info.joinLayer = joinLayer;
589  info.joinField = joinLayer->fields().indexFromName( joinInfo->joinFieldName() );
590 
591  // for joined fields, we always need to request the targetField from the provider too
592  if ( !mPreparedFields.contains( info.targetField ) && !mFieldsToPrepare.contains( info.targetField ) )
593  mFieldsToPrepare << info.targetField;
594 
597 
598  mFetchJoinInfo.insert( joinInfo, info );
599  }
600 
601  // store field source index - we'll need it when fetching from provider
602  mFetchJoinInfo[ joinInfo ].attributes.push_back( sourceLayerIndex );
603 }
604 
606 {
607  const QList<QgsExpressionFieldBuffer::ExpressionField> &exps = mSource->mExpressionFieldBuffer->expressions();
608 
609  int oi = mSource->mFields.fieldOriginIndex( fieldIdx );
610  QgsExpression *exp = new QgsExpression( exps[oi].cachedExpression );
611 
612  QgsDistanceArea da;
613  da.setSourceCrs( mSource->mCrs );
614  da.setEllipsoid( QgsProject::instance()->ellipsoid() );
615  exp->setGeomCalculator( &da );
616  exp->setDistanceUnits( QgsProject::instance()->distanceUnits() );
617  exp->setAreaUnits( QgsProject::instance()->areaUnits() );
618 
619  exp->prepare( mExpressionContext.get() );
620  Q_FOREACH ( const QString &col, exp->referencedColumns() )
621  {
622  if ( mSource->fields().lookupField( col ) == fieldIdx )
623  {
624  // circular reference - expression depends on column itself
625  delete exp;
626  return;
627  }
628  }
629  mExpressionFieldInfo.insert( fieldIdx, exp );
630 
631  Q_FOREACH ( const QString &col, exp->referencedColumns() )
632  {
633  int dependentFieldIdx = mSource->mFields.lookupField( col );
635  {
636  mRequest.setSubsetOfAttributes( mRequest.subsetOfAttributes() << dependentFieldIdx );
637  }
638  // also need to fetch this dependent field
639  if ( !mPreparedFields.contains( dependentFieldIdx ) && !mFieldsToPrepare.contains( dependentFieldIdx ) )
640  mFieldsToPrepare << dependentFieldIdx;
641  }
642 
643  if ( exp->needsGeometry() )
644  {
645  mRequest.setFlags( mRequest.flags() & ~QgsFeatureRequest::NoGeometry );
646  }
647 }
648 
650 {
651  mPreparedFields.clear();
652  mFieldsToPrepare.clear();
653  mFetchJoinInfo.clear();
654  mOrderedJoinInfoList.clear();
655 
656  mExpressionContext.reset( new QgsExpressionContext() );
657  mExpressionContext->appendScope( QgsExpressionContextUtils::globalScope() );
658  mExpressionContext->appendScope( QgsExpressionContextUtils::projectScope( QgsProject::instance() ) );
659  mExpressionContext->appendScope( new QgsExpressionContextScope( mSource->mLayerScope ) );
660 
662 
663  while ( !mFieldsToPrepare.isEmpty() )
664  {
665  int fieldIdx = mFieldsToPrepare.takeFirst();
666  if ( mPreparedFields.contains( fieldIdx ) )
667  continue;
668 
669  mPreparedFields << fieldIdx;
670  prepareField( fieldIdx );
671  }
672 
673  //sort joins by dependency
674  if ( mFetchJoinInfo.size() > 0 )
675  {
676  createOrderedJoinList();
677  }
678 }
679 
680 void QgsVectorLayerFeatureIterator::createOrderedJoinList()
681 {
682  mOrderedJoinInfoList = mFetchJoinInfo.values();
683  if ( mOrderedJoinInfoList.size() < 2 )
684  {
685  return;
686  }
687 
688  QSet<int> resolvedFields; //todo: get provider / virtual fields without joins
689 
690  //add all provider fields without joins as resolved fields
691  QList< int >::const_iterator prepFieldIt = mPreparedFields.constBegin();
692  for ( ; prepFieldIt != mPreparedFields.constEnd(); ++prepFieldIt )
693  {
694  if ( mSource->mFields.fieldOrigin( *prepFieldIt ) != QgsFields::OriginJoin )
695  {
696  resolvedFields.insert( *prepFieldIt );
697  }
698  }
699 
700  //iterate through the joins. If target field is not yet covered, move the entry to the end of the list
701 
702  //some join combinations might not have a resolution at all
703  int maxIterations = ( mOrderedJoinInfoList.size() + 1 ) * mOrderedJoinInfoList.size() / 2.0;
704  int currentIteration = 0;
705 
706  for ( int i = 0; i < mOrderedJoinInfoList.size() - 1; ++i )
707  {
708  if ( !resolvedFields.contains( mOrderedJoinInfoList.at( i ).targetField ) )
709  {
710  mOrderedJoinInfoList.append( mOrderedJoinInfoList.at( i ) );
711  mOrderedJoinInfoList.removeAt( i );
712  --i;
713  }
714  else
715  {
716  int offset = mOrderedJoinInfoList.at( i ).indexOffset;
717  int joinField = mOrderedJoinInfoList.at( i ).joinField;
718 
719  QgsAttributeList attributes = mOrderedJoinInfoList.at( i ).attributes;
720  QgsAttributeList::const_iterator attIt = attributes.constBegin();
721  for ( ; attIt != attributes.constEnd(); ++attIt )
722  {
723  if ( *attIt != joinField )
724  {
725  resolvedFields.insert( joinField < *attIt ? *attIt + offset - 1 : *attIt + offset );
726  }
727  }
728  }
729 
730  ++currentIteration;
731  if ( currentIteration >= maxIterations )
732  {
733  break;
734  }
735  }
736 }
737 
738 bool QgsVectorLayerFeatureIterator::postProcessFeature( QgsFeature &feature )
739 {
740  bool result = checkGeometryValidity( feature );
741  if ( result )
743  return result;
744 }
745 
746 bool QgsVectorLayerFeatureIterator::checkGeometryValidity( const QgsFeature &feature )
747 {
748  if ( !feature.hasGeometry() )
749  return true;
750 
751  switch ( mRequest.invalidGeometryCheck() )
752  {
754  return true;
755 
757  {
758  if ( !feature.geometry().isGeosValid() )
759  {
760  QgsMessageLog::logMessage( QObject::tr( "Geometry error: One or more input features have invalid geometry." ), QString(), QgsMessageLog::CRITICAL );
762  {
763  mRequest.invalidGeometryCallback()( feature );
764  }
765  return false;
766  }
767  break;
768  }
769 
771  if ( !feature.geometry().isGeosValid() )
772  {
773  QgsMessageLog::logMessage( QObject::tr( "Geometry error: One or more input features have invalid geometry." ), QString(), QgsMessageLog::CRITICAL );
774  close();
776  {
777  mRequest.invalidGeometryCallback()( feature );
778  }
779  return false;
780  }
781  break;
782  }
783 
784  return true;
785 }
786 
788 {
789  switch ( mSource->mFields.fieldOrigin( fieldIdx ) )
790  {
792  prepareExpression( fieldIdx );
793  break;
794 
797  {
798  prepareJoin( fieldIdx );
799  }
800  break;
801 
805  break;
806  }
807 }
808 
810 {
811  QList< FetchJoinInfo >::const_iterator joinIt = mOrderedJoinInfoList.constBegin();
812  for ( ; joinIt != mOrderedJoinInfoList.constEnd(); ++joinIt )
813  {
814  QVariant targetFieldValue = f.attribute( joinIt->targetField );
815  if ( !targetFieldValue.isValid() )
816  continue;
817 
818  const QHash< QString, QgsAttributes> &memoryCache = joinIt->joinInfo->cachedAttributes;
819  if ( memoryCache.isEmpty() )
820  joinIt->addJoinedAttributesDirect( f, targetFieldValue );
821  else
822  joinIt->addJoinedAttributesCached( f, targetFieldValue );
823  }
824 }
825 
827 {
828  // make sure we have space for newly added attributes
829  QgsAttributes attr = f.attributes();
830  attr.resize( mSource->mFields.count() ); // Provider attrs count + joined attrs count + expression attrs count
831  f.setAttributes( attr );
832 
833  // possible TODO - handle combinations of expression -> join -> expression -> join?
834  // but for now, write that off as too complex and an unlikely rare, unsupported use case
835 
836  QList< int > fetchedVirtualAttributes;
837  //first, check through joins for any virtual fields we need
838  QMap<const QgsVectorLayerJoinInfo *, FetchJoinInfo>::const_iterator joinIt = mFetchJoinInfo.constBegin();
839  for ( ; joinIt != mFetchJoinInfo.constEnd(); ++joinIt )
840  {
841  if ( mExpressionFieldInfo.contains( joinIt->targetField ) )
842  {
843  // have to calculate expression field before we can handle this join
844  addExpressionAttribute( f, joinIt->targetField );
845  fetchedVirtualAttributes << joinIt->targetField;
846  }
847  }
848 
849  if ( !mFetchJoinInfo.isEmpty() )
850  addJoinedAttributes( f );
851 
852  // add remaining expression fields
853  if ( !mExpressionFieldInfo.isEmpty() )
854  {
855  QMap<int, QgsExpression *>::ConstIterator it = mExpressionFieldInfo.constBegin();
856  for ( ; it != mExpressionFieldInfo.constEnd(); ++it )
857  {
858  if ( fetchedVirtualAttributes.contains( it.key() ) )
859  continue;
860 
861  addExpressionAttribute( f, it.key() );
862  }
863  }
864 }
865 
867 {
868  QgsExpression *exp = mExpressionFieldInfo.value( attrIndex );
869  if ( exp )
870  {
871  mExpressionContext->setFeature( f );
872  QVariant val = exp->evaluate( mExpressionContext.get() );
873  mSource->mFields.at( attrIndex ).convertCompatible( val );
874  f.setAttribute( attrIndex, val );
875  }
876  else
877  {
878  f.setAttribute( attrIndex, QVariant() );
879  }
880 }
881 
883 {
884  Q_UNUSED( simplifyMethod );
885  return false;
886 }
887 
888 bool QgsVectorLayerFeatureIterator::providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const
889 {
890  Q_UNUSED( methodType );
891  return false;
892 }
893 
894 
896 {
897  const QHash<QString, QgsAttributes> &memoryCache = joinInfo->cachedAttributes;
898  QHash<QString, QgsAttributes>::const_iterator it = memoryCache.find( joinValue.toString() );
899  if ( it == memoryCache.constEnd() )
900  return; // joined value not found -> leaving the attributes empty (null)
901 
902  int index = indexOffset;
903 
904  const QgsAttributes &featureAttributes = it.value();
905  for ( int i = 0; i < featureAttributes.count(); ++i )
906  {
907  f.setAttribute( index++, featureAttributes.at( i ) );
908  }
909 }
910 
911 
912 
914 {
915  // no memory cache, query the joined values by setting substring
916  QString subsetString;
917 
918  QString joinFieldName = joinInfo->joinFieldName();
919 
920  subsetString.append( QStringLiteral( "\"%1\"" ).arg( joinFieldName ) );
921 
922  if ( joinValue.isNull() )
923  {
924  subsetString += QLatin1String( " IS NULL" );
925  }
926  else
927  {
928  QString v = joinValue.toString();
929  switch ( joinValue.type() )
930  {
931  case QVariant::Int:
932  case QVariant::LongLong:
933  case QVariant::Double:
934  break;
935 
936  default:
937  case QVariant::String:
938  v.replace( '\'', QLatin1String( "''" ) );
939  v.prepend( '\'' ).append( '\'' );
940  break;
941  }
942  subsetString += '=' + v;
943  }
944 
945  // maybe user requested just a subset of layer's attributes
946  // so we do not have to cache everything
947  bool hasSubset = joinInfo->joinFieldNamesSubset();
948  QVector<int> subsetIndices;
949  if ( hasSubset )
950  subsetIndices = QgsVectorLayerJoinBuffer::joinSubsetIndices( joinLayer, *joinInfo->joinFieldNamesSubset() );
951 
952  // select (no geometry)
953  QgsFeatureRequest request;
955  request.setSubsetOfAttributes( attributes );
956  request.setFilterExpression( subsetString );
957  request.setLimit( 1 );
958  QgsFeatureIterator fi = joinLayer->getFeatures( request );
959 
960  // get first feature
961  QgsFeature fet;
962  if ( fi.nextFeature( fet ) )
963  {
964  int index = indexOffset;
965  QgsAttributes attr = fet.attributes();
966  if ( hasSubset )
967  {
968  for ( int i = 0; i < subsetIndices.count(); ++i )
969  f.setAttribute( index++, attr.at( subsetIndices.at( i ) ) );
970  }
971  else
972  {
973  // use all fields except for the one used for join (has same value as exiting field in target layer)
974  for ( int i = 0; i < attr.count(); ++i )
975  {
976  if ( i == joinField )
977  continue;
978 
979  f.setAttribute( index++, attr.at( i ) );
980  }
981  }
982  }
983  else
984  {
985  // no suitable join feature found, keeping empty (null) attributes
986  }
987 }
988 
989 
990 
991 
993 {
994  QgsFeatureId featureId = mRequest.filterFid();
995 
996  // deleted already?
997  if ( mSource->mDeletedFeatureIds.contains( featureId ) )
998  return false;
999 
1000  // has changed geometry?
1001  if ( !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) && mSource->mChangedGeometries.contains( featureId ) )
1002  {
1003  useChangedAttributeFeature( featureId, mSource->mChangedGeometries[featureId], f );
1004  return true;
1005  }
1006 
1007  // added features
1008  for ( QgsFeatureMap::ConstIterator iter = mSource->mAddedFeatures.constBegin(); iter != mSource->mAddedFeatures.constEnd(); ++iter )
1009  {
1010  if ( iter->id() == featureId )
1011  {
1012  useAddedFeature( *iter, f );
1013  return true;
1014  }
1015  }
1016 
1017  // regular features
1019  if ( fi.nextFeature( f ) )
1020  {
1021  f.setFields( mSource->mFields );
1022 
1023  if ( mSource->mHasEditBuffer )
1025 
1026  if ( mHasVirtualAttributes )
1027  addVirtualAttributes( f );
1028 
1029  return true;
1030  }
1031 
1032  return false;
1033 }
1034 
1036 {
1037  QgsAttributes attrs = f.attributes();
1038 
1039  // remove all attributes that will disappear - from higher indices to lower
1040  for ( int idx = mSource->mDeletedAttributeIds.count() - 1; idx >= 0; --idx )
1041  {
1042  attrs.remove( mSource->mDeletedAttributeIds[idx] );
1043  }
1044 
1045  // adjust size to accommodate added attributes
1046  attrs.resize( attrs.count() + mSource->mAddedAttributes.count() );
1047 
1048  // update changed attributes
1049  if ( mSource->mChangedAttributeValues.contains( f.id() ) )
1050  {
1052  for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
1053  attrs[it.key()] = it.value();
1054  }
1055  f.setAttributes( attrs );
1056 }
1057 
1059 {
1060  if ( mSource->mChangedGeometries.contains( f.id() ) )
1062 }
1063 
1064 bool QgsVectorLayerFeatureIterator::prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys )
1065 {
1066  Q_UNUSED( orderBys );
1067  return true;
1068 }
1069 
1070 
1071 //
1072 // QgsVectorLayerSelectedFeatureSource
1073 //
1074 
1076  : mSource( layer )
1077  , mSelectedFeatureIds( layer->selectedFeatureIds() )
1078  , mWkbType( layer->wkbType() )
1079  , mName( layer->name() )
1080 {}
1081 
1083 {
1084  QgsFeatureRequest req( request );
1085 
1086  if ( req.filterFids().isEmpty() && req.filterType() != QgsFeatureRequest::FilterFid )
1087  {
1088  req.setFilterFids( mSelectedFeatureIds );
1089  }
1090  else if ( !req.filterFids().isEmpty() )
1091  {
1092  QgsFeatureIds reqIds = mSelectedFeatureIds;
1093  reqIds.intersect( req.filterFids() );
1094  req.setFilterFids( reqIds );
1095  }
1096 
1097  return mSource.getFeatures( req );
1098 }
1099 
1101 {
1102  return mSource.crs();
1103 }
1104 
1106 {
1107  return mSource.fields();
1108 }
1109 
1111 {
1112  return mWkbType;
1113 }
1114 
1116 {
1117  return mSelectedFeatureIds.count();
1118 }
1119 
1121 {
1122  return mName;
1123 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:289
QList< QgsExpressionFieldBuffer::ExpressionField > expressions() const
QgsAbstractFeatureSource * mProviderFeatureSource
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Returns an iterator for the features in the source.
QgsFeatureId id
Definition: qgsfeature.h:70
void addJoinedAttributesDirect(QgsFeature &f, const QVariant &joinValue) const
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:512
const QgsVectorLayerJoinInfo * joinInfo
Canonical source of information about the join.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QSet< QString > CORE_EXPORT usedAttributes() const
Returns a set of used attributes.
void geometryToDestinationCrs(QgsFeature &feature, const QgsCoordinateTransform &transform) const
Transforms feature&#39;s geometry according to the specified coordinate transform.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
Filter using feature ID.
QgsVectorLayerJoinBuffer * mJoinBuffer
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system for features retrieved from this source.
bool containsJoins() const
Quick way to test if there is any join at all.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:49
virtual void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker) override
Attach an object that can be queried regularly by the iterator to check if it must stopped...
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfields.cpp:161
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
Definition: qgsfeature.cpp:155
QString sourceName() const override
Returns a friendly display name for the source.
const Flags & flags() const
void createJoinCaches()
Calls cacheJoinLayer() for all vector joins.
QMap< int, QgsExpression * > mExpressionFieldInfo
void addExpressionAttribute(QgsFeature &f, int attrIndex)
Adds an expression based attribute to a feature.
QgsFeatureMap::ConstIterator mFetchAddedFeaturesIt
QgsGeometryMap::ConstIterator mFetchChangedGeomIt
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:50
bool exists(int i) const
Return if a field index is valid.
Definition: qgsfields.cpp:125
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
QgsVectorLayerFeatureIterator(QgsVectorLayerFeatureSource *source, bool ownSource, const QgsFeatureRequest &request)
QgsVectorLayerSelectedFeatureSource(QgsVectorLayer *layer)
Constructor for QgsVectorLayerSelectedFeatureSource, for selected features from the specified layer...
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) override
Get an iterator for features matching the specified request.
QgsFeatureId filterFid() const
Get the feature ID that should be fetched.
QgsRectangle filterRectToSourceCrs(const QgsCoordinateTransform &transform) const
Returns a rectangle representing the original request&#39;s QgsFeatureRequest::filterRect().
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature&#39;s geometries, or an invalid QgsCoordi...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Get an iterator for features matching the specified request.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:225
FilterType filterType() const
Return the filter type which is currently set on this request.
Container of fields for a vector layer.
Definition: qgsfields.h:41
const QgsFeatureIds & filterFids() const
Get feature IDs that should be fetched.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
bool setAttribute(int field, const QVariant &attr)
Set an attribute&#39;s value by field index.
Definition: qgsfeature.cpp:204
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:48
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
bool mClosed
Set to true, as soon as the iterator is closed.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed. ...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
QgsVectorLayer * joinLayer
Resolved pointer to the joined layer.
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
int count() const
Return number of items.
Definition: qgsfields.cpp:115
QgsFeatureIds deletedFeatureIds() const
Returns a list of deleted feature IDs which are not committed.
bool isGeosValid() const
Checks validity of the geometry using GEOS.
It has not been specified where the field comes from.
Definition: qgsfields.h:47
QgsFields fields() const override
Returns the fields associated with features in the source.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:135
int joinField
Index of field (of the joined layer) must have equal value.
QgsExpression * filterExpression() const
Returns the filter expression if set.
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:169
virtual QgsAbstractFeatureSource * featureSource() const =0
Return feature source object that can be used for querying provider&#39;s data.
Interface that can be optionally attached to an iterator so its nextFeature() implementaton can check...
void updateChangedAttributes(QgsFeature &f)
Update feature with uncommitted attribute updates.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Set feature ID that should be fetched.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
QgsGeometryMap changedGeometries() const
Returns a map of features with changed geometries which are not committed.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:66
void iteratorClosed()
to be called by from subclass in close()
int indexFromName(const QString &fieldName) const
Get the field index from the field name.
Definition: qgsfields.cpp:174
void useAddedFeature(const QgsFeature &src, QgsFeature &f)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null if the reference was set by layer ID and not resolved yet) ...
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfields.cpp:316
QgsFields fields() const override
Returns the list of fields of this layer.
QgsFields fields() const
Returns the fields that will be available for features that are retrieved from this source...
QgsFeatureRequest & disableFilter()
Disables filter conditions.
void updateFeatureGeometry(QgsFeature &f)
Update feature with uncommitted geometry updates.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual bool prepareSimplification(const QgsSimplifyMethod &simplifyMethod) override
Setup the simplification of geometries to fetch using the specified simplify method.
void addJoinedAttributesCached(QgsFeature &f, const QVariant &joinValue) const
Defines left outer join from our vector layer to some other vector layer.
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:39
virtual bool fetchFeature(QgsFeature &feature) override
fetch next feature, return true on success
QgsAttributeList deletedAttributeIds() const
Returns a list of deleted attributes fields which are not committed.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
QgsWkbTypes::Type wkbType() const override
Returns the geometry type for features returned by this source.
No invalid geometry checking.
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:112
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsExpressionFieldBuffer * mExpressionFieldBuffer
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QgsAttributeList subsetOfAttributes() const
Return the subset of attributes which at least need to be fetched.
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
int indexOffset
At what position the joined fields start.
void useChangedAttributeFeature(QgsFeatureId fid, const QgsGeometry &geom, QgsFeature &f)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:181
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
long featureCount() const override
Returns the number of features contained in the source, or -1 if the feature count is unknown...
QgsFeatureMap addedFeatures() const
Returns a map of new features which are not committed.
QgsFeatureRequest mRequest
A copy of the feature request.
static void logMessage(const QString &message, const QString &tag=QString(), MessageLevel level=QgsMessageLog::WARNING)
add a message to the instance (and create it if necessary)
int targetField
Index of field (of this layer) that drives the join.
Buffers information about expression fields for a vector layer.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Set feature IDs that should be fetched.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:503
virtual bool rewind() override
reset the iterator to the starting position
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:379
QMap< const QgsVectorLayerJoinInfo *, QgsVectorLayerFeatureIterator::FetchJoinInfo > mFetchJoinInfo
Information about joins used in the current select() statement.
This class represents a coordinate reference system (CRS).
virtual bool close() override
end of iterating: free the resources / lock
static QVector< int > joinSubsetIndices(QgsVectorLayer *joinLayer, const QStringList &joinFieldsSubset)
Return a vector of indices for use in join based on field names from the layer.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
QgsVectorDataProvider * dataProvider() override
Returns the layer&#39;s data provider.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
Class for doing transforms between two map coordinate systems.
Join information prepared for fast attribute id mapping in QgsVectorLayerJoinBuffer::updateFeatureAtt...
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs for feature&#39;s geometries.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:64
void setSourceCrs(const QgsCoordinateReferenceSystem &srcCRS)
Sets source spatial reference system.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
bool nextFeature(QgsFeature &f)
QgsChangedAttributesMap mChangedAttributeValues
OrderBy orderBy() const
Return a list of order by clauses specified for this feature request.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QList< QgsField > addedAttributes() const
Returns a list of added attributes fields which are not committed.
A vector of attributes.
Definition: qgsattributes.h:57
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
Field is calculated from an expression.
Definition: qgsfields.h:51
QString joinFieldName() const
Returns name of the field of joined layer that will be used for join.
bool isClosed() const
find out whether the iterator is still valid or closed already
QgsVectorLayerFeatureSource(const QgsVectorLayer *layer)
Constructor for QgsVectorLayerFeatureSource.
QgsAttributes attributes
Definition: qgsfeature.h:71
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
void setInterruptionChecker(QgsInterruptionChecker *interruptionChecker)
Attach an object that can be queried regularly by the iterator to check if it must stopped...
int joinedFieldsOffset(const QgsVectorLayerJoinInfo *info, const QgsFields &fields)
Find out what is the first index of the join within fields.
QgsVectorLayerJoinBuffer * clone() const
Create a copy of the join buffer.
void addVirtualAttributes(QgsFeature &f)
Adds attributes that don&#39;t source from the provider but are added inside QGIS Includes.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Set flags that affect how features will be fetched.
Helper template that cares of two things: 1.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.