Skip to content
Permalink
Browse files

Merge pull request #492 from borysiasty/encoding_write_fix

Fix shapefile encoding in QgsVectorFileWriter
  • Loading branch information
borysiasty committed Apr 22, 2013
2 parents 1a91841 + 0564ab1 commit c0551a68c250489955c9831f5714f187df087d83
Showing with 25 additions and 10 deletions.
  1. +25 −10 src/core/qgsvectorfilewriter.cpp
@@ -73,7 +73,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
{
QString vectorFileName = theVectorFileName;
QString fileEncoding = theFileEncoding;

QStringList layOptions = layerOptions;
QStringList dsOptions = datasourceOptions;

QString ogrDriverName;
@@ -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 )
@@ -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 ) )
{
@@ -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;

0 comments on commit c0551a6

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