Skip to content

Commit f00d004

Browse files
committedJan 30, 2013
fix attribute mapping in vector layer import
1 parent 8579809 commit f00d004

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed
 

‎python/plugins/db_manager/dlg_import_vector.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ def accept(self):
310310
QApplication.restoreOverrideCursor()
311311

312312
if ret != 0:
313-
QMessageBox.warning(self, "Import to database", u"Error %d\n%s" % (ret, errMsg) )
313+
output = qgis.gui.QgsMessageViewer()
314+
output.setTitle( "Import to database" )
315+
output.setMessageAsPlainText( u"Error %d\n%s" % (ret, errMsg) )
316+
output.showMessage()
314317
return
315318

316319
# create spatial index

‎src/core/qgsvectorlayerimport.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
8585
return;
8686
}
8787

88+
mAttributeCount = -1;
89+
90+
foreach ( int idx, mOldToNewAttrIdx.values() )
91+
{
92+
if ( idx > mAttributeCount )
93+
mAttributeCount = idx;
94+
}
95+
96+
mAttributeCount++;
97+
8898
QgsDebugMsg( "Created empty layer" );
8999

90100
QgsVectorDataProvider *vectorProvider = ( QgsVectorDataProvider* ) pReg->provider( providerKey, uri );
@@ -128,17 +138,19 @@ bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
128138
QgsFeature newFeat;
129139
if ( feat.geometry() )
130140
newFeat.setGeometry( *feat.geometry() );
131-
newFeat.initAttributes( attrs.count() );
141+
142+
newFeat.initAttributes( mAttributeCount );
132143

133144
for ( int i = 0; i < attrs.count(); ++i )
134145
{
135146
// add only mapped attributes (un-mapped ones will not be present in the
136147
// destination layer)
137-
if ( mOldToNewAttrIdx.contains( i ) )
138-
{
139-
QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( mOldToNewAttrIdx.value( i ) ), 3 );
140-
newFeat.setAttribute( mOldToNewAttrIdx.value( i ), attrs[i] );
141-
}
148+
int dstIdx = mOldToNewAttrIdx.value( i, -1 );
149+
if ( dstIdx < 0 )
150+
continue;
151+
152+
QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( dstIdx ), 3 );
153+
newFeat.setAttribute( dstIdx, attrs[i] );
142154
}
143155

144156
mFeatureBuffer.append( newFeat );

‎src/core/qgsvectorlayerimport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class CORE_EXPORT QgsVectorLayerImport
107107

108108
/** map attribute indexes to new field indexes */
109109
QMap<int, int> mOldToNewAttrIdx;
110+
int mAttributeCount;
110111

111112
QgsFeatureList mFeatureBuffer;
112113
QProgressDialog *mProgress;

‎src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
18201820

18211821
if ( fields.size() > 0 )
18221822
{
1823-
int offset = geometryColumn.isEmpty() ? 0 : 1;
1823+
int offset = 0;
18241824

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

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2992,7 +2992,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29922992

29932993
if ( fields.size() > 0 )
29942994
{
2995-
int offset = geometryColumn.isEmpty() ? 1 : 2;
2995+
int offset = 1;
29962996

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

0 commit comments

Comments
 (0)
Please sign in to comment.