Skip to content

Commit

Permalink
Merge pull request #566 from minorua/fix_cpg
Browse files Browse the repository at this point in the history
Encoding: produce more compatible .cpg files.
  • Loading branch information
borysiasty committed Apr 29, 2013
2 parents c5177fc + 1113c60 commit 62c5e63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -113,7 +113,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
{
if ( layOptions.join( "" ).toUpper().indexOf( "ENCODING=" ) == -1 )
{
layOptions.append( "ENCODING=" + fileEncoding );
layOptions.append( "ENCODING=" + convertCodecNameForEncodingOption( fileEncoding ) );
}

CPLSetConfigOption( "SHAPE_ENCODING", "" );
Expand Down Expand Up @@ -1023,6 +1023,23 @@ QString QgsVectorFileWriter::filterForDriver( const QString& driverName )
return trLongName + " [OGR] (" + glob.toLower() + " " + glob.toUpper() + ")";
}

QString QgsVectorFileWriter::convertCodecNameForEncodingOption( const QString &codecName )
{
if ( codecName == "System" )
return QString( "LDID/0" );

QRegExp re = QRegExp( QString( "(CP|windows-|ISO[ -])(.+)" ), Qt::CaseInsensitive );
if ( re.exactMatch( codecName ) )
{
QString c = re.cap( 2 ).replace( "-" , "" );
bool isNumber;
c.toInt( &isNumber );
if ( isNumber )
return c;
}
return codecName;
}

bool QgsVectorFileWriter::driverMetadata( QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext )
{
if ( driverName.startsWith( "AVCE00" ) )
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -123,6 +123,9 @@ class CORE_EXPORT QgsVectorFileWriter
/**Creates a filter for an OGR driver key*/
static QString filterForDriver( const QString& driverName );

/**Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile*/
static QString convertCodecNameForEncodingOption( const QString &codecName );

/** checks whether there were any errors in constructor */
WriterError hasError();

Expand Down
2 changes: 1 addition & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1850,7 +1850,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,
char **papszOptions = NULL;
if ( driverName == "ESRI Shapefile" )
{
papszOptions = CSLSetNameValue( papszOptions, "ENCODING", encoding.toLocal8Bit().data() );
papszOptions = CSLSetNameValue( papszOptions, "ENCODING", QgsVectorFileWriter::convertCodecNameForEncodingOption( encoding ).toLocal8Bit().data() );
// OGR Shapefile fails to create fields if given encoding is not supported by its side
// so disable encoding conversion of OGR Shapefile layer
CPLSetConfigOption( "SHAPE_ENCODING", "" );
Expand Down

0 comments on commit 62c5e63

Please sign in to comment.