1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/***************************************************************************
qgsvectorlayerjoinbuffer.cpp
----------------------------
begin : Feb 09, 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectordataprovider.h"
#include <QDomElement>
QgsVectorLayerJoinBuffer::QgsVectorLayerJoinBuffer()
{
}
QgsVectorLayerJoinBuffer::~QgsVectorLayerJoinBuffer()
{
}
void QgsVectorLayerJoinBuffer::addJoin( const QgsVectorJoinInfo& joinInfo )
{
mVectorJoins.push_back( joinInfo );
//cache joined layer to virtual memory if specified by user
if ( joinInfo.memoryCache )
{
cacheJoinLayer( mVectorJoins.last() );
}
}
void QgsVectorLayerJoinBuffer::removeJoin( const QString& joinLayerId )
{
for ( int i = 0; i < mVectorJoins.size(); ++i )
{
if ( mVectorJoins.at( i ).joinLayerId == joinLayerId )
{
mVectorJoins.removeAt( i );
}
}
}
void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorJoinInfo& joinInfo )
{
//memory cache not required or already done
if ( !joinInfo.memoryCache || joinInfo.cachedAttributes.size() > 0 )
{
return;
}
QgsVectorLayer* cacheLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) );
if ( cacheLayer )
{
int joinFieldIndex;
if ( joinInfo.joinFieldName.isEmpty() )
joinFieldIndex = joinInfo.joinFieldIndex; //for compatibility with 1.x
else
joinFieldIndex = cacheLayer->pendingFields().indexFromName( joinInfo.joinFieldName );
if ( joinFieldIndex < 0 || joinFieldIndex >= cacheLayer->pendingFields().count() )
return;
joinInfo.cachedAttributes.clear();
QgsFeatureIterator fit = cacheLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
const QgsAttributes& attrs = f.attributes();
joinInfo.cachedAttributes.insert( attrs[joinFieldIndex].toString(), attrs );
}
}
}
void QgsVectorLayerJoinBuffer::updateFields( QgsFields& fields )
{
QList< QgsVectorJoinInfo>::const_iterator joinIt = mVectorJoins.constBegin();
for ( int joinIdx = 0 ; joinIt != mVectorJoins.constEnd(); ++joinIt, ++joinIdx )
{
QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( joinIt->joinLayerId ) );
if ( !joinLayer )
{
continue;
}
const QgsFields& joinFields = joinLayer->pendingFields();
QString joinFieldName;
if ( joinIt->joinFieldName.isEmpty() && joinIt->joinFieldIndex >= 0 && joinIt->joinFieldIndex < joinFields.count() )
joinFieldName = joinFields.field( joinIt->joinFieldIndex ).name(); //for compatibility with 1.x
else
joinFieldName = joinIt->joinFieldName;
for ( int idx = 0; idx < joinFields.count(); ++idx )
{
//skip the join field to avoid double field names (fields often have the same name)
if ( joinFields[idx].name() != joinFieldName )
{
QgsField f = joinFields[idx];
f.setName( joinLayer->name() + "_" + f.name() );
fields.append( f, QgsFields::OriginJoin, idx + ( joinIdx*1000 ) );
}
}
}
}
void QgsVectorLayerJoinBuffer::createJoinCaches()
{
QList< QgsVectorJoinInfo >::iterator joinIt = mVectorJoins.begin();
for ( ; joinIt != mVectorJoins.end(); ++joinIt )
{
cacheJoinLayer( *joinIt );
}
}
void QgsVectorLayerJoinBuffer::writeXml( QDomNode& layer_node, QDomDocument& document ) const
{
QDomElement vectorJoinsElem = document.createElement( "vectorjoins" );
layer_node.appendChild( vectorJoinsElem );
QList< QgsVectorJoinInfo >::const_iterator joinIt = mVectorJoins.constBegin();
for ( ; joinIt != mVectorJoins.constEnd(); ++joinIt )
{
QDomElement joinElem = document.createElement( "join" );
if ( joinIt->targetFieldName.isEmpty() )
joinElem.setAttribute( "targetField", joinIt->targetFieldIndex ); //for compatibility with 1.x
else
joinElem.setAttribute( "targetFieldName", joinIt->targetFieldName );
joinElem.setAttribute( "joinLayerId", joinIt->joinLayerId );
if ( joinIt->joinFieldName.isEmpty() )
joinElem.setAttribute( "joinField", joinIt->joinFieldIndex ); //for compatibility with 1.x
else
joinElem.setAttribute( "joinFieldName", joinIt->joinFieldName );
joinElem.setAttribute( "memoryCache", !joinIt->cachedAttributes.isEmpty() );
vectorJoinsElem.appendChild( joinElem );
}
}
void QgsVectorLayerJoinBuffer::readXml( const QDomNode& layer_node )
{
mVectorJoins.clear();
QDomElement vectorJoinsElem = layer_node.firstChildElement( "vectorjoins" );
if ( !vectorJoinsElem.isNull() )
{
QDomNodeList joinList = vectorJoinsElem.elementsByTagName( "join" );
for ( int i = 0; i < joinList.size(); ++i )
{
QDomElement infoElem = joinList.at( i ).toElement();
QgsVectorJoinInfo info;
info.joinFieldName = infoElem.attribute( "joinFieldName" );
info.joinLayerId = infoElem.attribute( "joinLayerId" );
info.targetFieldName = infoElem.attribute( "targetFieldName" );
info.memoryCache = infoElem.attribute( "memoryCache" ).toInt();
info.joinFieldIndex = infoElem.attribute( "joinField" ).toInt(); //for compatibility with 1.x
info.targetFieldIndex = infoElem.attribute( "targetField" ).toInt(); //for compatibility with 1.x
addJoin( info );
}
}
}
const QgsVectorJoinInfo* QgsVectorLayerJoinBuffer::joinForFieldIndex( int index, const QgsFields& fields, int& sourceFieldIndex ) const
{
if ( fields.fieldOrigin( index ) != QgsFields::OriginJoin )
return 0;
int originIndex = fields.fieldOriginIndex( index );
int sourceJoinIndex = originIndex / 1000;
sourceFieldIndex = originIndex % 1000;
if ( sourceJoinIndex < 0 || sourceJoinIndex >= mVectorJoins.count() )
return 0;
return &( mVectorJoins[sourceJoinIndex] );
}
|