QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
23QString QgsShapefileEncodingInfoAlgorithm::name() const
24{
25 return QStringLiteral( "shpencodinginfo" );
26}
27
28QString QgsShapefileEncodingInfoAlgorithm::displayName() const
29{
30 return QObject::tr( "Extract Shapefile encoding" );
31}
32
33QStringList QgsShapefileEncodingInfoAlgorithm::tags() const
34{
35 return QObject::tr( "shp,codepage,cpg,ldid,information,list,show" ).split( ',' );
36}
37
38QString QgsShapefileEncodingInfoAlgorithm::group() const
39{
40 return QObject::tr( "Vector general" );
41}
42
43QString QgsShapefileEncodingInfoAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeneral" );
46}
47
48QString 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
55QString QgsShapefileEncodingInfoAlgorithm::shortDescription() const
56{
57 return QObject::tr( "Extracts the attribute encoding information embedded in a Shapefile." );
58}
59
60QgsShapefileEncodingInfoAlgorithm *QgsShapefileEncodingInfoAlgorithm::createInstance() const
61{
62 return new QgsShapefileEncodingInfoAlgorithm();
63}
64
65void QgsShapefileEncodingInfoAlgorithm::initAlgorithm( const QVariantMap & )
66{
67 addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), false, QObject::tr( "Shapefiles (%1)" ).arg( QLatin1String( "*.shp *.SHP" ) ) ) );
68
69 addOutput( new QgsProcessingOutputString( QStringLiteral( "ENCODING" ), QObject::tr( "Shapefile Encoding" ) ) );
70 addOutput( new QgsProcessingOutputString( QStringLiteral( "CPG_ENCODING" ), QObject::tr( "CPG Encoding" ) ) );
71 addOutput( new QgsProcessingOutputString( QStringLiteral( "LDID_ENCODING" ), QObject::tr( "LDID Encoding" ) ) );
72}
73
74bool QgsShapefileEncodingInfoAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
77
78 mCpgEncoding = QgsOgrUtils::readShapefileEncodingFromCpg( path );
79 if ( mCpgEncoding.isEmpty() )
80 feedback->pushInfo( QObject::tr( "No encoding information present in CPG file" ) );
81 else
82 feedback->pushInfo( QObject::tr( "Detected encoding from CPG file: %1" ).arg( mCpgEncoding ) );
83
84 mLdidEncoding = QgsOgrUtils::readShapefileEncodingFromLdid( path );
85 if ( mLdidEncoding.isEmpty() )
86 feedback->pushInfo( QObject::tr( "No encoding information present in DBF LDID header" ) );
87 else
88 feedback->pushInfo( QObject::tr( "Detected encoding from DBF LDID header: %1" ).arg( mLdidEncoding ) );
89
90 return true;
91}
92
93
94QVariantMap QgsShapefileEncodingInfoAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
95{
96 QVariantMap outputs;
97 outputs.insert( QStringLiteral( "ENCODING" ), mCpgEncoding.isEmpty() ? mLdidEncoding : mCpgEncoding );
98 outputs.insert( QStringLiteral( "CPG_ENCODING" ), mCpgEncoding );
99 outputs.insert( QStringLiteral( "LDID_ENCODING" ), mLdidEncoding );
100 return outputs;
101}
102
@ File
Parameter is a single file.
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.