Skip to content

Commit

Permalink
Split shapefile CPG/LDID based encoding detection to separate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 12, 2020
1 parent 9bd81de commit 8c10e08
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/core/qgsogrutils.cpp
Expand Up @@ -750,6 +750,15 @@ QgsCoordinateReferenceSystem QgsOgrUtils::OGRSpatialReferenceToCrs( OGRSpatialRe
} }


QString QgsOgrUtils::readShapefileEncoding( const QString &path ) 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 // unfortunately OGR's routines for calculating the shapefile encoding aren't exposed anywhere in the GDAL c api, so
// re-implement them here... // re-implement them here...
Expand Down Expand Up @@ -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 // 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" ) ) ); const QString dbfPath = fi.dir().filePath( QStringLiteral( "%1.%2" ).arg( baseName, fi.suffix() == QLatin1String( "SHP" ) ? QStringLiteral( "DBF" ) : QStringLiteral( "dbf" ) ) );
if ( QFile::exists( dbfPath ) ) if ( QFile::exists( dbfPath ) )
Expand Down
27 changes: 27 additions & 0 deletions src/core/qgsogrutils.h
Expand Up @@ -290,9 +290,36 @@ class CORE_EXPORT QgsOgrUtils
* Reads the encoding of the shapefile at the specified \a path (where \a path is the * Reads the encoding of the shapefile at the specified \a path (where \a path is the
* location of the ".shp" file). * 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 * \since QGIS 3.12
*/ */
static QString readShapefileEncoding( const QString &path ); 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 #endif // QGSOGRUTILS_H

0 comments on commit 8c10e08

Please sign in to comment.