Skip to content
Permalink
Browse files

case-sensitivity of vector layer field (fixes #13032)

* fieldNameIndex: resort to case-insensitive lookup only if
  case-sensitive lookup has no match
* file writer: fix handling of fields that only differ by case
  • Loading branch information
jef-n committed Oct 11, 2015
1 parent d277dd6 commit 8e2b791092ebc2be415c24c76bdc9d1b5a149961
Showing with 19 additions and 11 deletions.
  1. +4 −4 python/core/qgsfield.sip
  2. +7 −2 src/core/qgsfield.cpp
  3. +2 −2 src/core/qgsfield.h
  4. +6 −3 src/core/qgsvectorfilewriter.cpp
@@ -13,7 +13,7 @@ class QgsField
#include <qgsfield.h>
%End

public:
public:
/** Constructor. Constructs a new QgsField object.
* @param name Field name
* @param type Field variant type, currently supported: String / Int / Double
@@ -177,7 +177,7 @@ public:
*
* In addition to storing a list of QgsField instances, it also:
* - allows quick lookups of field names to index in the list
*- keeps track of where the field definition comes from (vector data provider, joined layer or newly added from an editing operation)
* - keeps track of where the field definition comes from (vector data provider, joined layer or newly added from an editing operation)
* \note QgsFields objects are implicitly shared.
*/

@@ -332,8 +332,8 @@ class QgsFields
//! Look up field's index from name. Returns -1 on error
int indexFromName( const QString& name ) const;

//! Look up field's index from name - case insensitive
//! TODO: sort out case sensitive (indexFromName()) vs insensitive (fieldNameIndex()) calls
//! Look up field's index from name
//! also looks up case-insensitive if there is no match otherwise
//! @note added in 2.4
int fieldNameIndex( const QString& fieldName ) const;

@@ -356,13 +356,18 @@ bool QgsFields::operator==( const QgsFields &other ) const

int QgsFields::fieldNameIndex( const QString& fieldName ) const
{
for ( int idx = 0; idx < count(); ++idx )
{
if ( d->fields[idx].field.name() == fieldName )
return idx;
}

for ( int idx = 0; idx < count(); ++idx )
{
if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
{
return idx;
}
}

return -1;
}

@@ -257,8 +257,8 @@ class CORE_EXPORT QgsFields
//! Look up field's index from name. Returns -1 on error
int indexFromName( const QString& name ) const;

//! Look up field's index from name - case insensitive
//! TODO: sort out case sensitive (indexFromName()) vs insensitive (fieldNameIndex()) calls
//! Look up field's index from name
//! also looks up case-insensitive if there is no match otherwise
//! @note added in 2.4
int fieldNameIndex( const QString& fieldName ) const;

@@ -339,6 +339,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(

mFields = fields;
mAttrIdxToOgrIdx.clear();
QSet<int> existingIdxs;

for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
{
@@ -447,7 +448,8 @@ QgsVectorFileWriter::QgsVectorFileWriter(
OGR_Fld_Destroy( fld );

int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
if ( ogrIdx < 0 )
QgsDebugMsg( QString( "returned field index for %1: %2" ).arg( name ).arg( ogrIdx ) );
if ( ogrIdx < 0 || existingIdxs.contains( ogrIdx ) )
{
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700
// if we didn't find our new column, assume it's name was truncated and
@@ -480,6 +482,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
}
}

existingIdxs.insert( ogrIdx );
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
}

@@ -1388,7 +1391,7 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()

layerOptions.insert( "LAUNDER", new BoolOption(
QObject::tr( "Controls whether layer and field names will be laundered for easier use "
"in SQLite. Laundered names will be convered to lower case and some special "
"in SQLite. Laundered names will be converted to lower case and some special "
"characters(' - #) will be changed to underscores." ),
true // Default value
) );
@@ -2633,7 +2636,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::exportFeaturesSymbolLevels
if ( !styleString.isEmpty() )
{
OGR_F_SetStyleString( ogrFeature, styleString.toLocal8Bit().data() );
if ( ! writeFeature( mLayer, ogrFeature ) )
if ( !writeFeature( mLayer, ogrFeature ) )
{
++nErrors;
}

0 comments on commit 8e2b791

Please sign in to comment.
You can’t perform that action at this time.