Skip to content
Permalink
Browse files

Split shapefile CPG/LDID based encoding detection to separate methods

  • Loading branch information
nyalldawson committed Feb 10, 2020
1 parent 9bd81de commit 8c10e08ee5bb167c0583002ba67a7e3f8511fa69
Showing with 54 additions and 0 deletions.
  1. +27 −0 src/core/qgsogrutils.cpp
  2. +27 −0 src/core/qgsogrutils.h
@@ -750,6 +750,15 @@ QgsCoordinateReferenceSystem QgsOgrUtils::OGRSpatialReferenceToCrs( OGRSpatialRe
}

QString QgsOgrUtils::readShapefileEncoding( const QString &path )
{
const QString cpgEncoding = readShapefileEncodingFromCpg( path );
if ( !cpgEncoding.isEmpty() )
return cpgEncoding;

return readShapefileEncodingFromLdid( path );
}

QString QgsOgrUtils::readShapefileEncodingFromCpg( const QString &path )
{
// unfortunately OGR's routines for calculating the shapefile encoding aren't exposed anywhere in the GDAL c api, so
// re-implement them here...
@@ -798,6 +807,24 @@ QString QgsOgrUtils::readShapefileEncoding( const QString &path )
}
}

return QString();
}

QString QgsOgrUtils::readShapefileEncodingFromLdid( const QString &path )
{
// unfortunately OGR's routines for calculating the shapefile encoding aren't exposed anywhere in the GDAL c api, so
// re-implement them here...

// from OGRShapeLayer::ConvertCodePage
// https://github.com/OSGeo/gdal/blob/master/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp#L342

if ( !QFileInfo::exists( path ) )
return QString();

// first try to read cpg file, if present
const QFileInfo fi( path );
const QString baseName = fi.completeBaseName();

// fallback to LDID value, read from DBF file
const QString dbfPath = fi.dir().filePath( QStringLiteral( "%1.%2" ).arg( baseName, fi.suffix() == QLatin1String( "SHP" ) ? QStringLiteral( "DBF" ) : QStringLiteral( "dbf" ) ) );
if ( QFile::exists( dbfPath ) )
@@ -290,9 +290,36 @@ class CORE_EXPORT QgsOgrUtils
* Reads the encoding of the shapefile at the specified \a path (where \a path is the
* location of the ".shp" file).
*
* This method considers both the CPG specified encoding and the DBF LDID encoding
* (priority goes to CPG based encoding)
*
* \see readShapefileEncodingFromCpg()
* \see readShapefileEncodingFromLdid()
* \since QGIS 3.12
*/
static QString readShapefileEncoding( const QString &path );

/**
* Reads the encoding of the shapefile at the specified \a path (where \a path is the
* location of the ".shp" file), from the CPG specified encoding.
*
* Return an empty string if CPG based encoding was not found.
*
* \see readShapefileEncoding()
* \since QGIS 3.12
*/
static QString readShapefileEncodingFromCpg( const QString &path );

/**
* Reads the encoding of the shapefile at the specified \a path (where \a path is the
* location of the ".shp" file), from the DBF LDID encoding.
*
* Return an empty string if LDID based encoding was not found.
*
* \see readShapefileEncoding()
* \since QGIS 3.12
*/
static QString readShapefileEncodingFromLdid( const QString &path );
};

#endif // QGSOGRUTILS_H

0 comments on commit 8c10e08

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