Skip to content
Permalink
Browse files
fix attribute mapping in vector layer import
  • Loading branch information
jef-n committed Jan 30, 2013
1 parent 8579809 commit f00d0041044543d733e2617f769695f877daa33b
@@ -310,7 +310,10 @@ def accept(self):
QApplication.restoreOverrideCursor()

if ret != 0:
QMessageBox.warning(self, "Import to database", u"Error %d\n%s" % (ret, errMsg) )
output = qgis.gui.QgsMessageViewer()
output.setTitle( "Import to database" )
output.setMessageAsPlainText( u"Error %d\n%s" % (ret, errMsg) )
output.showMessage()
return

# create spatial index
@@ -85,6 +85,16 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
return;
}

mAttributeCount = -1;

foreach ( int idx, mOldToNewAttrIdx.values() )
{
if ( idx > mAttributeCount )
mAttributeCount = idx;
}

mAttributeCount++;

QgsDebugMsg( "Created empty layer" );

QgsVectorDataProvider *vectorProvider = ( QgsVectorDataProvider* ) pReg->provider( providerKey, uri );
@@ -128,17 +138,19 @@ bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
QgsFeature newFeat;
if ( feat.geometry() )
newFeat.setGeometry( *feat.geometry() );
newFeat.initAttributes( attrs.count() );

newFeat.initAttributes( mAttributeCount );

for ( int i = 0; i < attrs.count(); ++i )
{
// add only mapped attributes (un-mapped ones will not be present in the
// destination layer)
if ( mOldToNewAttrIdx.contains( i ) )
{
QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( mOldToNewAttrIdx.value( i ) ), 3 );
newFeat.setAttribute( mOldToNewAttrIdx.value( i ), attrs[i] );
}
int dstIdx = mOldToNewAttrIdx.value( i, -1 );
if ( dstIdx < 0 )
continue;

QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( dstIdx ), 3 );
newFeat.setAttribute( dstIdx, attrs[i] );
}

mFeatureBuffer.append( newFeat );
@@ -107,6 +107,7 @@ class CORE_EXPORT QgsVectorLayerImport

/** map attribute indexes to new field indexes */
QMap<int, int> mOldToNewAttrIdx;
int mAttributeCount;

QgsFeatureList mFeatureBuffer;
QProgressDialog *mProgress;
@@ -1820,7 +1820,7 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(

if ( fields.size() > 0 )
{
int offset = geometryColumn.isEmpty() ? 0 : 1;
int offset = 0;

// get the list of fields
QList<QgsField> flist;
@@ -2992,7 +2992,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(

if ( fields.size() > 0 )
{
int offset = geometryColumn.isEmpty() ? 1 : 2;
int offset = 1;

// get the list of fields
QList<QgsField> flist;

0 comments on commit f00d004

Please sign in to comment.