QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsalgorithmshpencodinginfo.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmshpencodinginfo.cpp
3  -----------------------------
4  begin : February 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsogrutils.h"
20 
22 
23 QString QgsShapefileEncodingInfoAlgorithm::name() const
24 {
25  return QStringLiteral( "shpencodinginfo" );
26 }
27 
28 QString QgsShapefileEncodingInfoAlgorithm::displayName() const
29 {
30  return QObject::tr( "Extract Shapefile encoding" );
31 }
32 
33 QStringList QgsShapefileEncodingInfoAlgorithm::tags() const
34 {
35  return QObject::tr( "shp,codepage,cpg,ldid,information,list,show" ).split( ',' );
36 }
37 
38 QString QgsShapefileEncodingInfoAlgorithm::group() const
39 {
40  return QObject::tr( "Vector general" );
41 }
42 
43 QString QgsShapefileEncodingInfoAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeneral" );
46 }
47 
48 QString QgsShapefileEncodingInfoAlgorithm::shortHelpString() const
49 {
50  return QObject::tr( "This algorithm extracts the attribute encoding information embedded in a Shapefile.\n\n"
51  "Both the encoding specified by an optional .cpg file and any encoding details present in the "
52  ".dbf LDID header block are considered." );
53 }
54 
55 QString QgsShapefileEncodingInfoAlgorithm::shortDescription() const
56 {
57  return QObject::tr( "Extracts the attribute encoding information embedded in a Shapefile." );
58 }
59 
60 QgsShapefileEncodingInfoAlgorithm *QgsShapefileEncodingInfoAlgorithm::createInstance() const
61 {
62  return new QgsShapefileEncodingInfoAlgorithm();
63 }
64 
65 void QgsShapefileEncodingInfoAlgorithm::initAlgorithm( const QVariantMap & )
66 {
67  addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QgsProcessingParameterFile::File,
68  QString(), QVariant(), false, QObject::tr( "Shapefiles (%1)" ).arg( QLatin1String( "*.shp *.SHP" ) ) ) );
69 
70  addOutput( new QgsProcessingOutputString( QStringLiteral( "ENCODING" ), QObject::tr( "Shapefile Encoding" ) ) );
71  addOutput( new QgsProcessingOutputString( QStringLiteral( "CPG_ENCODING" ), QObject::tr( "CPG Encoding" ) ) );
72  addOutput( new QgsProcessingOutputString( QStringLiteral( "LDID_ENCODING" ), QObject::tr( "LDID Encoding" ) ) );
73 }
74 
75 bool QgsShapefileEncodingInfoAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76 {
77  const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
78 
79  mCpgEncoding = QgsOgrUtils::readShapefileEncodingFromCpg( path );
80  if ( mCpgEncoding.isEmpty() )
81  feedback->pushInfo( QObject::tr( "No encoding information present in CPG file" ) );
82  else
83  feedback->pushInfo( QObject::tr( "Detected encoding from CPG file: %1" ).arg( mCpgEncoding ) );
84 
85  mLdidEncoding = QgsOgrUtils::readShapefileEncodingFromLdid( path );
86  if ( mLdidEncoding.isEmpty() )
87  feedback->pushInfo( QObject::tr( "No encoding information present in DBF LDID header" ) );
88  else
89  feedback->pushInfo( QObject::tr( "Detected encoding from DBF LDID header: %1" ).arg( mLdidEncoding ) );
90 
91  return true;
92 }
93 
94 
95 QVariantMap QgsShapefileEncodingInfoAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
96 {
97  QVariantMap outputs;
98  outputs.insert( QStringLiteral( "ENCODING" ), mCpgEncoding.isEmpty() ? mLdidEncoding : mCpgEncoding );
99  outputs.insert( QStringLiteral( "CPG_ENCODING" ), mCpgEncoding );
100  outputs.insert( QStringLiteral( "LDID_ENCODING" ), mLdidEncoding );
101  return outputs;
102 }
103 
static QString readShapefileEncodingFromCpg(const QString &path)
Reads the encoding of the shapefile at the specified path (where path is the location of the "....
static QString readShapefileEncodingFromLdid(const QString &path)
Reads the encoding of the shapefile at the specified path (where path is the location of the "....
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A string output for processing algorithms.
An input file or folder parameter for processing algorithms.
@ File
Parameter is a single file.