Skip to content

Commit

Permalink
Fix shapefile encoding in QgsVectorFileWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
borysiasty committed Mar 31, 2013
1 parent b99ff61 commit 0564ab1
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
{
QString vectorFileName = theVectorFileName;
QString fileEncoding = theFileEncoding;

QStringList layOptions = layerOptions;
QStringList dsOptions = datasourceOptions;

QString ogrDriverName;
Expand All @@ -98,9 +98,6 @@ QgsVectorFileWriter::QgsVectorFileWriter(
OGRSFDriverH poDriver;
QgsApplication::registerOgrDrivers();

QSettings settings;
CPLSetConfigOption( "SHAPE_ENCODING", settings.value( "/qgis/ignoreShapeEncoding", true ).toBool() ? "" : 0 );

poDriver = OGRGetDriverByName( ogrDriverName.toLocal8Bit().data() );

if ( poDriver == NULL )
Expand All @@ -112,8 +109,26 @@ QgsVectorFileWriter::QgsVectorFileWriter(
return;
}

QSettings settings;

if ( driverName == "ESRI Shapefile" )
{
if ( layOptions.join( "" ).toUpper().indexOf( "ENCODING=" ) == -1 )
{
layOptions.append( "ENCODING=" + fileEncoding );
}

if ( settings.value( "/qgis/ignoreShapeEncoding", true ).toBool() )
{
CPLSetConfigOption( "SHAPE_ENCODING", "" );
}
else
{
CPLSetConfigOption( "SHAPE_ENCODING", fileEncoding.toLocal8Bit().data() );
// WARNING!! If SHAPE_ENCODING and -lco ENCODING are used, the fileEncoding must be set to the layer internal encoding!!
fileEncoding = "UTF-8";
}

if ( !vectorFileName.endsWith( ".shp", Qt::CaseInsensitive ) &&
!vectorFileName.endsWith( ".dbf", Qt::CaseInsensitive ) )
{
Expand Down Expand Up @@ -243,21 +258,21 @@ QgsVectorFileWriter::QgsVectorFileWriter(
QString layerName = QFileInfo( vectorFileName ).baseName();
OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>( geometryType );

if ( !layerOptions.isEmpty() )
if ( !layOptions.isEmpty() )
{
options = new char *[ layerOptions.size()+1 ];
for ( int i = 0; i < layerOptions.size(); i++ )
options = new char *[ layOptions.size()+1 ];
for ( int i = 0; i < layOptions.size(); i++ )
{
options[i] = CPLStrdup( layerOptions[i].toLocal8Bit().data() );
options[i] = CPLStrdup( layOptions[i].toLocal8Bit().data() );
}
options[ layerOptions.size()] = NULL;
options[ layOptions.size()] = NULL;
}

mLayer = OGR_DS_CreateLayer( mDS, TO8F( layerName ), ogrRef, wkbType, options );

if ( options )
{
for ( int i = 0; i < layerOptions.size(); i++ )
for ( int i = 0; i < layOptions.size(); i++ )
CPLFree( options[i] );
delete [] options;
options = NULL;
Expand Down

0 comments on commit 0564ab1

Please sign in to comment.