Skip to content

Commit

Permalink
fix attribute mapping in vector layer import
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jan 30, 2013
1 parent 8579809 commit f00d004
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
5 changes: 4 additions & 1 deletion python/plugins/db_manager/dlg_import_vector.py
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/core/qgsvectorlayerimport.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerimport.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit f00d004

Please sign in to comment.