QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgsvectorlayerjoinbuffer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerjoinbuffer.cpp
3  ----------------------------
4  begin : Feb 09, 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
20 #include "qgsmaplayerregistry.h"
21 #include "qgsvectordataprovider.h"
22 
23 #include <QDomElement>
24 
26  : mLayer( layer )
27 {
28 }
29 
31 {
32 }
33 
34 static QList<QgsVectorLayer*> _outEdges( QgsVectorLayer* vl )
35 {
36  QList<QgsVectorLayer*> lst;
37  foreach ( const QgsVectorJoinInfo& info, vl->vectorJoins() )
38  {
39  if ( QgsVectorLayer* joinVl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( info.joinLayerId ) ) )
40  lst << joinVl;
41  }
42  return lst;
43 }
44 
45 static bool _hasCycleDFS( QgsVectorLayer* n, QHash<QgsVectorLayer*, int>& mark )
46 {
47  if ( mark.value( n ) == 1 ) // temporary
48  return true;
49  if ( mark.value( n ) == 0 ) // not visited
50  {
51  mark[n] = 1; // temporary
52  foreach ( QgsVectorLayer* m, _outEdges( n ) )
53  {
54  if ( _hasCycleDFS( m, mark ) )
55  return true;
56  }
57  mark[n] = 2; // permanent
58  }
59  return false;
60 }
61 
62 
64 {
65  mVectorJoins.push_back( joinInfo );
66 
67  // run depth-first search to detect cycles in the graph of joins between layers.
68  // any cycle would cause infinite recursion when updating fields
69  QHash<QgsVectorLayer*, int> markDFS;
70  if ( mLayer && _hasCycleDFS( mLayer, markDFS ) )
71  {
72  // we have to reject this one
73  mVectorJoins.pop_back();
74  return false;
75  }
76 
77  //cache joined layer to virtual memory if specified by user
78  if ( joinInfo.memoryCache )
79  {
80  cacheJoinLayer( mVectorJoins.last() );
81  }
82 
83  // Wait for notifications about changed fields in joined layer to propagate them.
84  // During project load the joined layers possibly do not exist yet so the connection will not be created,
85  // but then QgsProject makes sure to call createJoinCaches() which will do the connection.
86  // Unique connection makes sure we do not respond to one layer's update more times (in case of multiple join)
87  if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) ) )
88  connect( vl, SIGNAL( updatedFields() ), this, SLOT( joinedLayerUpdatedFields() ), Qt::UniqueConnection );
89 
90  emit joinedFieldsChanged();
91  return true;
92 }
93 
94 
95 void QgsVectorLayerJoinBuffer::removeJoin( const QString& joinLayerId )
96 {
97  for ( int i = 0; i < mVectorJoins.size(); ++i )
98  {
99  if ( mVectorJoins.at( i ).joinLayerId == joinLayerId )
100  {
101  mVectorJoins.removeAt( i );
102  }
103  }
104 
105  if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinLayerId ) ) )
106  disconnect( vl, SIGNAL( updatedFields() ), this, SLOT( joinedLayerUpdatedFields() ) );
107 
108  emit joinedFieldsChanged();
109 }
110 
111 void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
112 {
113  //memory cache not required or already done
114  if ( !joinInfo.memoryCache || joinInfo.cachedAttributes.size() > 0 )
115  {
116  return;
117  }
118 
119  QgsVectorLayer* cacheLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) );
120  if ( cacheLayer )
121  {
122  int joinFieldIndex;
123  if ( joinInfo.joinFieldName.isEmpty() )
124  joinFieldIndex = joinInfo.joinFieldIndex; //for compatibility with 1.x
125  else
126  joinFieldIndex = cacheLayer->pendingFields().indexFromName( joinInfo.joinFieldName );
127 
128  if ( joinFieldIndex < 0 || joinFieldIndex >= cacheLayer->pendingFields().count() )
129  return;
130 
131  joinInfo.cachedAttributes.clear();
132 
133  QgsFeatureRequest request;
135 
136  // maybe user requested just a subset of layer's attributes
137  // so we do not have to cache everything
138  bool hasSubset = joinInfo.joinFieldNamesSubset();
139  QVector<int> subsetIndices;
140  if ( hasSubset )
141  {
142  subsetIndices = joinSubsetIndices( cacheLayer, *joinInfo.joinFieldNamesSubset() );
143 
144  // we need just subset of attributes - but make sure to include join field name
145  QgsAttributeList cacheLayerAttrs = subsetIndices.toList();
146  if ( !cacheLayerAttrs.contains( joinFieldIndex ) )
147  cacheLayerAttrs.append( joinFieldIndex );
148  request.setSubsetOfAttributes( cacheLayerAttrs );
149  }
150 
151  QgsFeatureIterator fit = cacheLayer->getFeatures( request );
152  QgsFeature f;
153  while ( fit.nextFeature( f ) )
154  {
155  const QgsAttributes& attrs = f.attributes();
156  QString key = attrs[joinFieldIndex].toString();
157  if ( hasSubset )
158  {
159  QgsAttributes subsetAttrs( subsetIndices.count() );
160  for ( int i = 0; i < subsetIndices.count(); ++i )
161  subsetAttrs[i] = attrs[ subsetIndices[i] ];
162  joinInfo.cachedAttributes.insert( key, subsetAttrs );
163  }
164  else
165  {
166  QgsAttributes attrs2 = attrs;
167  attrs2.remove( joinFieldIndex ); // skip the join field to avoid double field names (fields often have the same name)
168  joinInfo.cachedAttributes.insert( key, attrs2 );
169  }
170  }
171  }
172 }
173 
174 
175 QVector<int> QgsVectorLayerJoinBuffer::joinSubsetIndices( QgsVectorLayer* joinLayer, const QStringList& joinFieldsSubset )
176 {
177  QVector<int> subsetIndices;
178  const QgsFields& fields = joinLayer->pendingFields();
179  for ( int i = 0; i < joinFieldsSubset.count(); ++i )
180  {
181  QString joinedFieldName = joinFieldsSubset.at( i );
182  int index = fields.fieldNameIndex( joinedFieldName );
183  if ( index != -1 )
184  {
185  subsetIndices.append( index );
186  }
187  else
188  {
189  QgsDebugMsg( "Join layer subset field not found: " + joinedFieldName );
190  }
191  }
192 
193  return subsetIndices;
194 }
195 
197 {
198  QList< QgsVectorJoinInfo>::const_iterator joinIt = mVectorJoins.constBegin();
199  for ( int joinIdx = 0 ; joinIt != mVectorJoins.constEnd(); ++joinIt, ++joinIdx )
200  {
201  QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinIt->joinLayerId ) );
202  if ( !joinLayer )
203  {
204  continue;
205  }
206 
207  const QgsFields& joinFields = joinLayer->pendingFields();
208  QString joinFieldName;
209  if ( joinIt->joinFieldName.isEmpty() && joinIt->joinFieldIndex >= 0 && joinIt->joinFieldIndex < joinFields.count() )
210  joinFieldName = joinFields.field( joinIt->joinFieldIndex ).name(); //for compatibility with 1.x
211  else
212  joinFieldName = joinIt->joinFieldName;
213 
214  QSet<QString> subset;
215  bool hasSubset = false;
216  if ( joinIt->joinFieldNamesSubset() )
217  {
218  hasSubset = true;
219  subset = QSet<QString>::fromList( *joinIt->joinFieldNamesSubset() );
220  }
221 
222  for ( int idx = 0; idx < joinFields.count(); ++idx )
223  {
224  // if using just a subset of fields, filter some of them out
225  if ( hasSubset && !subset.contains( joinFields[idx].name() ) )
226  continue;
227 
228  //skip the join field to avoid double field names (fields often have the same name)
229  if ( joinFields[idx].name() != joinFieldName )
230  {
231  QgsField f = joinFields[idx];
232  f.setName( joinLayer->name() + "_" + f.name() );
233  fields.append( f, QgsFields::OriginJoin, idx + ( joinIdx*1000 ) );
234  }
235  }
236  }
237 }
238 
240 {
241  QList< QgsVectorJoinInfo >::iterator joinIt = mVectorJoins.begin();
242  for ( ; joinIt != mVectorJoins.end(); ++joinIt )
243  {
244  cacheJoinLayer( *joinIt );
245 
246  // make sure we are connected to the joined layer
247  if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinIt->joinLayerId ) ) )
248  connect( vl, SIGNAL( updatedFields() ), this, SLOT( joinedLayerUpdatedFields() ), Qt::UniqueConnection );
249  }
250 }
251 
252 
253 void QgsVectorLayerJoinBuffer::writeXml( QDomNode& layer_node, QDomDocument& document ) const
254 {
255  QDomElement vectorJoinsElem = document.createElement( "vectorjoins" );
256  layer_node.appendChild( vectorJoinsElem );
257  QList< QgsVectorJoinInfo >::const_iterator joinIt = mVectorJoins.constBegin();
258  for ( ; joinIt != mVectorJoins.constEnd(); ++joinIt )
259  {
260  QDomElement joinElem = document.createElement( "join" );
261 
262  if ( joinIt->targetFieldName.isEmpty() )
263  joinElem.setAttribute( "targetField", joinIt->targetFieldIndex ); //for compatibility with 1.x
264  else
265  joinElem.setAttribute( "targetFieldName", joinIt->targetFieldName );
266 
267  joinElem.setAttribute( "joinLayerId", joinIt->joinLayerId );
268  if ( joinIt->joinFieldName.isEmpty() )
269  joinElem.setAttribute( "joinField", joinIt->joinFieldIndex ); //for compatibility with 1.x
270  else
271  joinElem.setAttribute( "joinFieldName", joinIt->joinFieldName );
272 
273  joinElem.setAttribute( "memoryCache", !joinIt->cachedAttributes.isEmpty() );
274 
275  if ( joinIt->joinFieldNamesSubset() )
276  {
277  QDomElement subsetElem = document.createElement( "joinFieldsSubset" );
278  foreach ( QString fieldName, *joinIt->joinFieldNamesSubset() )
279  {
280  QDomElement fieldElem = document.createElement( "field" );
281  fieldElem.setAttribute( "name", fieldName );
282  subsetElem.appendChild( fieldElem );
283  }
284 
285  joinElem.appendChild( subsetElem );
286  }
287 
288  vectorJoinsElem.appendChild( joinElem );
289  }
290 }
291 
292 void QgsVectorLayerJoinBuffer::readXml( const QDomNode& layer_node )
293 {
294  mVectorJoins.clear();
295  QDomElement vectorJoinsElem = layer_node.firstChildElement( "vectorjoins" );
296  if ( !vectorJoinsElem.isNull() )
297  {
298  QDomNodeList joinList = vectorJoinsElem.elementsByTagName( "join" );
299  for ( int i = 0; i < joinList.size(); ++i )
300  {
301  QDomElement infoElem = joinList.at( i ).toElement();
302  QgsVectorJoinInfo info;
303  info.joinFieldName = infoElem.attribute( "joinFieldName" );
304  info.joinLayerId = infoElem.attribute( "joinLayerId" );
305  info.targetFieldName = infoElem.attribute( "targetFieldName" );
306  info.memoryCache = infoElem.attribute( "memoryCache" ).toInt();
307 
308  info.joinFieldIndex = infoElem.attribute( "joinField" ).toInt(); //for compatibility with 1.x
309  info.targetFieldIndex = infoElem.attribute( "targetField" ).toInt(); //for compatibility with 1.x
310 
311  QDomElement subsetElem = infoElem.firstChildElement( "joinFieldsSubset" );
312  if ( !subsetElem.isNull() )
313  {
314  QStringList* fieldNames = new QStringList;
315  QDomNodeList fieldNodes = infoElem.elementsByTagName( "field" );
316  for ( int i = 0; i < fieldNodes.count(); ++i )
317  *fieldNames << fieldNodes.at( i ).toElement().attribute( "name" );
318  info.setJoinFieldNamesSubset( fieldNames );
319  }
320 
321  addJoin( info );
322  }
323  }
324 }
325 
327 {
328  if ( !info )
329  return -1;
330 
331  int joinIndex = mVectorJoins.indexOf( *info );
332  if ( joinIndex == -1 )
333  return -1;
334 
335  for ( int i = 0; i < fields.count(); ++i )
336  {
337  if ( fields.fieldOrigin( i ) != QgsFields::OriginJoin )
338  continue;
339 
340  if ( fields.fieldOriginIndex( i ) / 1000 == joinIndex )
341  return i;
342  }
343  return -1;
344 }
345 
346 const QgsVectorJoinInfo* QgsVectorLayerJoinBuffer::joinForFieldIndex( int index, const QgsFields& fields, int& sourceFieldIndex ) const
347 {
348  if ( fields.fieldOrigin( index ) != QgsFields::OriginJoin )
349  return 0;
350 
351  int originIndex = fields.fieldOriginIndex( index );
352  int sourceJoinIndex = originIndex / 1000;
353  sourceFieldIndex = originIndex % 1000;
354 
355  if ( sourceJoinIndex < 0 || sourceJoinIndex >= mVectorJoins.count() )
356  return 0;
357 
358  return &( mVectorJoins[sourceJoinIndex] );
359 }
360 
362 {
363  QgsVectorLayerJoinBuffer* cloned = new QgsVectorLayerJoinBuffer( mLayer );
364  cloned->mVectorJoins = mVectorJoins;
365  return cloned;
366 }
367 
368 void QgsVectorLayerJoinBuffer::joinedLayerUpdatedFields()
369 {
370  QgsVectorLayer* joinedLayer = qobject_cast<QgsVectorLayer*>( sender() );
371  Q_ASSERT( joinedLayer );
372 
373  // recache the joined layer
374  for ( QgsVectorJoinList::iterator it = mVectorJoins.begin(); it != mVectorJoins.end(); ++it )
375  {
376  if ( joinedLayer->id() == it->joinLayerId )
377  {
378  it->cachedAttributes.clear();
379  cacheJoinLayer( *it );
380  }
381  }
382 
383  emit joinedFieldsChanged();
384 }
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:59
const QList< QgsVectorJoinInfo > & vectorJoins() const
Wrapper for iterator of features from vector data provider or vector layer.
static unsigned index
QString joinFieldName
Join field in the source layer.
QString targetFieldName
Join field in the target layer.
void createJoinCaches()
Calls cacheJoinLayer() for all vector joins.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:229
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name - case insensitive TODO: sort out case sensitive (indexFromName()) vs...
Definition: qgsfield.cpp:208
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
int joinFieldIndex
Join field index in the source layer.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
void readXml(const QDomNode &layer_node)
Reads joins from project file.
Container of fields for a vector layer.
Definition: qgsfield.h:172
QgsVectorLayerJoinBuffer(QgsVectorLayer *layer=0)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:227
bool memoryCache
True if the join is cached in virtual memory.
int targetFieldIndex
Join field index in the target layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
Manages joined fields for a vector layer.
const QgsVectorJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
int joinedFieldsOffset(const QgsVectorJoinInfo *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.
const QString & name() const
Get the display name of the layer.
void setJoinFieldNamesSubset(QStringList *fieldNamesSubset)
Set subset of fields to be used from joined layer.
bool addJoin(const QgsVectorJoinInfo &joinInfo)
Joins another vector layer to this layer.
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfield.h:236
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
const QgsAttributes & attributes() const
Definition: qgsfeature.h:142
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfield.cpp:162
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
int count() const
Return number of items.
Definition: qgsfield.h:214
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:33
void removeJoin(const QString &joinLayerId)
Removes a vector layer join.
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
Definition: qgsfield.h:241
void setName(const QString &nam)
Set the field name.
Definition: qgsfield.cpp:89
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QHash< QString, QgsAttributes > cachedAttributes
Cache for joined attributes to provide fast lookup (size is 0 if no memory caching) ...
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:100
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves mVectorJoins to xml under the layer node.
static bool _hasCycleDFS(QgsVectorLayer *n, QHash< QgsVectorLayer *, int > &mark)
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfield.h:234
void updateFields(QgsFields &fields)
Updates field map with joined attributes.
static QList< QgsVectorLayer * > _outEdges(QgsVectorLayer *vl)
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 joinedFieldsChanged()
Emitted whenever the list of joined fields changes (e.g.
QStringList * joinFieldNamesSubset() const
Get subset of fields to be used from joined layer.
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfield.h:180
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
QgsFeatureRequest & setFlags(Flags flags)
Set flags that affect how features will be fetched.
QString joinLayerId
Source layer.