QGIS API Documentation 3.99.0-Master (8e76e220402)
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
20#include "qgsogrutils.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsShapefileEncodingInfoAlgorithm::name() const
29{
30 return u"shpencodinginfo"_s;
31}
32
33QString QgsShapefileEncodingInfoAlgorithm::displayName() const
34{
35 return QObject::tr( "Extract Shapefile encoding" );
36}
37
38QStringList QgsShapefileEncodingInfoAlgorithm::tags() const
39{
40 return QObject::tr( "shp,codepage,cpg,ldid,information,list,show" ).split( ',' );
41}
42
43QString QgsShapefileEncodingInfoAlgorithm::group() const
44{
45 return QObject::tr( "Vector general" );
46}
47
48QString QgsShapefileEncodingInfoAlgorithm::groupId() const
49{
50 return u"vectorgeneral"_s;
51}
52
53QString QgsShapefileEncodingInfoAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm extracts the attribute encoding information embedded in a Shapefile.\n\n"
56 "Both the encoding specified by an optional .cpg file and any encoding details present in the "
57 ".dbf LDID header block are considered." );
58}
59
60QString QgsShapefileEncodingInfoAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Extracts the attribute encoding information embedded in a Shapefile." );
63}
64
65QgsShapefileEncodingInfoAlgorithm *QgsShapefileEncodingInfoAlgorithm::createInstance() const
66{
67 return new QgsShapefileEncodingInfoAlgorithm();
68}
69
70void QgsShapefileEncodingInfoAlgorithm::initAlgorithm( const QVariantMap & )
71{
72 addParameter( new QgsProcessingParameterFile( u"INPUT"_s, QObject::tr( "Input layer" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), false, QObject::tr( "Shapefiles (%1)" ).arg( "*.shp *.SHP"_L1 ) ) );
73
74 addOutput( new QgsProcessingOutputString( u"ENCODING"_s, QObject::tr( "Shapefile Encoding" ) ) );
75 addOutput( new QgsProcessingOutputString( u"CPG_ENCODING"_s, QObject::tr( "CPG Encoding" ) ) );
76 addOutput( new QgsProcessingOutputString( u"LDID_ENCODING"_s, QObject::tr( "LDID Encoding" ) ) );
77}
78
79bool QgsShapefileEncodingInfoAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
80{
81 const QString path = parameterAsFile( parameters, u"INPUT"_s, context );
82
83 mCpgEncoding = QgsOgrUtils::readShapefileEncodingFromCpg( path );
84 if ( mCpgEncoding.isEmpty() )
85 feedback->pushInfo( QObject::tr( "No encoding information present in CPG file" ) );
86 else
87 feedback->pushInfo( QObject::tr( "Detected encoding from CPG file: %1" ).arg( mCpgEncoding ) );
88
89 mLdidEncoding = QgsOgrUtils::readShapefileEncodingFromLdid( path );
90 if ( mLdidEncoding.isEmpty() )
91 feedback->pushInfo( QObject::tr( "No encoding information present in DBF LDID header" ) );
92 else
93 feedback->pushInfo( QObject::tr( "Detected encoding from DBF LDID header: %1" ).arg( mLdidEncoding ) );
94
95 return true;
96}
97
98
99QVariantMap QgsShapefileEncodingInfoAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
100{
101 QVariantMap outputs;
102 outputs.insert( u"ENCODING"_s, mCpgEncoding.isEmpty() ? mLdidEncoding : mCpgEncoding );
103 outputs.insert( u"CPG_ENCODING"_s, mCpgEncoding );
104 outputs.insert( u"LDID_ENCODING"_s, mLdidEncoding );
105 return outputs;
106}
107
@ File
Parameter is a single file.
Definition qgis.h:3860
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.