QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
56 "This algorithm extracts the attribute encoding information embedded in a Shapefile.\n\n"
57 "Both the encoding specified by an optional .cpg file and any encoding details present in the "
58 ".dbf LDID header block are considered."
59 );
60}
61
62QString QgsShapefileEncodingInfoAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Extracts the attribute encoding information embedded in a Shapefile." );
65}
66
67QgsShapefileEncodingInfoAlgorithm *QgsShapefileEncodingInfoAlgorithm::createInstance() const
68{
69 return new QgsShapefileEncodingInfoAlgorithm();
70}
71
72void QgsShapefileEncodingInfoAlgorithm::initAlgorithm( const QVariantMap & )
73{
74 addParameter(
75 new QgsProcessingParameterFile( u"INPUT"_s, QObject::tr( "Input layer" ), Qgis::ProcessingFileParameterBehavior::File, QString(), QVariant(), false, QObject::tr( "Shapefiles (%1)" ).arg( "*.shp *.SHP"_L1 ) )
76 );
77
78 addOutput( new QgsProcessingOutputString( u"ENCODING"_s, QObject::tr( "Shapefile Encoding" ) ) );
79 addOutput( new QgsProcessingOutputString( u"CPG_ENCODING"_s, QObject::tr( "CPG Encoding" ) ) );
80 addOutput( new QgsProcessingOutputString( u"LDID_ENCODING"_s, QObject::tr( "LDID Encoding" ) ) );
81}
82
83bool QgsShapefileEncodingInfoAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
84{
85 const QString path = parameterAsFile( parameters, u"INPUT"_s, context );
86
87 mCpgEncoding = QgsOgrUtils::readShapefileEncodingFromCpg( path );
88 if ( mCpgEncoding.isEmpty() )
89 feedback->pushInfo( QObject::tr( "No encoding information present in CPG file" ) );
90 else
91 feedback->pushInfo( QObject::tr( "Detected encoding from CPG file: %1" ).arg( mCpgEncoding ) );
92
93 mLdidEncoding = QgsOgrUtils::readShapefileEncodingFromLdid( path );
94 if ( mLdidEncoding.isEmpty() )
95 feedback->pushInfo( QObject::tr( "No encoding information present in DBF LDID header" ) );
96 else
97 feedback->pushInfo( QObject::tr( "Detected encoding from DBF LDID header: %1" ).arg( mLdidEncoding ) );
98
99 return true;
100}
101
102
103QVariantMap QgsShapefileEncodingInfoAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
104{
105 QVariantMap outputs;
106 outputs.insert( u"ENCODING"_s, mCpgEncoding.isEmpty() ? mLdidEncoding : mCpgEncoding );
107 outputs.insert( u"CPG_ENCODING"_s, mCpgEncoding );
108 outputs.insert( u"LDID_ENCODING"_s, mLdidEncoding );
109 return outputs;
110}
111
@ File
Parameter is a single file.
Definition qgis.h:3906
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.