mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-04 00:06:46 -05:00
fix attribute mapping in vector layer import
This commit is contained in:
parent
85798098ea
commit
f00d004104
@ -310,7 +310,10 @@ class DlgImportVector(QDialog, Ui_Dialog):
|
|||||||
QApplication.restoreOverrideCursor()
|
QApplication.restoreOverrideCursor()
|
||||||
|
|
||||||
if ret != 0:
|
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
|
return
|
||||||
|
|
||||||
# create spatial index
|
# create spatial index
|
||||||
|
|||||||
@ -85,6 +85,16 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mAttributeCount = -1;
|
||||||
|
|
||||||
|
foreach ( int idx, mOldToNewAttrIdx.values() )
|
||||||
|
{
|
||||||
|
if ( idx > mAttributeCount )
|
||||||
|
mAttributeCount = idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
mAttributeCount++;
|
||||||
|
|
||||||
QgsDebugMsg( "Created empty layer" );
|
QgsDebugMsg( "Created empty layer" );
|
||||||
|
|
||||||
QgsVectorDataProvider *vectorProvider = ( QgsVectorDataProvider* ) pReg->provider( providerKey, uri );
|
QgsVectorDataProvider *vectorProvider = ( QgsVectorDataProvider* ) pReg->provider( providerKey, uri );
|
||||||
@ -128,17 +138,19 @@ bool QgsVectorLayerImport::addFeature( QgsFeature& feat )
|
|||||||
QgsFeature newFeat;
|
QgsFeature newFeat;
|
||||||
if ( feat.geometry() )
|
if ( feat.geometry() )
|
||||||
newFeat.setGeometry( *feat.geometry() );
|
newFeat.setGeometry( *feat.geometry() );
|
||||||
newFeat.initAttributes( attrs.count() );
|
|
||||||
|
newFeat.initAttributes( mAttributeCount );
|
||||||
|
|
||||||
for ( int i = 0; i < attrs.count(); ++i )
|
for ( int i = 0; i < attrs.count(); ++i )
|
||||||
{
|
{
|
||||||
// add only mapped attributes (un-mapped ones will not be present in the
|
// add only mapped attributes (un-mapped ones will not be present in the
|
||||||
// destination layer)
|
// destination layer)
|
||||||
if ( mOldToNewAttrIdx.contains( i ) )
|
int dstIdx = mOldToNewAttrIdx.value( i, -1 );
|
||||||
{
|
if ( dstIdx < 0 )
|
||||||
QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( mOldToNewAttrIdx.value( i ) ), 3 );
|
continue;
|
||||||
newFeat.setAttribute( mOldToNewAttrIdx.value( i ), attrs[i] );
|
|
||||||
}
|
QgsDebugMsgLevel( QString( "moving field from pos %1 to %2" ).arg( i ).arg( dstIdx ), 3 );
|
||||||
|
newFeat.setAttribute( dstIdx, attrs[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
mFeatureBuffer.append( newFeat );
|
mFeatureBuffer.append( newFeat );
|
||||||
|
|||||||
@ -107,6 +107,7 @@ class CORE_EXPORT QgsVectorLayerImport
|
|||||||
|
|
||||||
/** map attribute indexes to new field indexes */
|
/** map attribute indexes to new field indexes */
|
||||||
QMap<int, int> mOldToNewAttrIdx;
|
QMap<int, int> mOldToNewAttrIdx;
|
||||||
|
int mAttributeCount;
|
||||||
|
|
||||||
QgsFeatureList mFeatureBuffer;
|
QgsFeatureList mFeatureBuffer;
|
||||||
QProgressDialog *mProgress;
|
QProgressDialog *mProgress;
|
||||||
|
|||||||
@ -1820,7 +1820,7 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
|
|||||||
|
|
||||||
if ( fields.size() > 0 )
|
if ( fields.size() > 0 )
|
||||||
{
|
{
|
||||||
int offset = geometryColumn.isEmpty() ? 0 : 1;
|
int offset = 0;
|
||||||
|
|
||||||
// get the list of fields
|
// get the list of fields
|
||||||
QList<QgsField> flist;
|
QList<QgsField> flist;
|
||||||
|
|||||||
@ -2992,7 +2992,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
|
|||||||
|
|
||||||
if ( fields.size() > 0 )
|
if ( fields.size() > 0 )
|
||||||
{
|
{
|
||||||
int offset = geometryColumn.isEmpty() ? 1 : 2;
|
int offset = 1;
|
||||||
|
|
||||||
// get the list of fields
|
// get the list of fields
|
||||||
QList<QgsField> flist;
|
QList<QgsField> flist;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user