QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmrepairshapefile.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrepairshapefile.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 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 <cpl_conv.h>
21
23#include "qgsvectorlayer.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QString QgsRepairShapefileAlgorithm::name() const
32{
33 return u"repairshapefile"_s;
34}
35
36QString QgsRepairShapefileAlgorithm::displayName() const
37{
38 return QObject::tr( "Repair Shapefile" );
39}
40
41QStringList QgsRepairShapefileAlgorithm::tags() const
42{
43 return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' );
44}
45
46QString QgsRepairShapefileAlgorithm::group() const
47{
48 return QObject::tr( "Vector general" );
49}
50
51QString QgsRepairShapefileAlgorithm::groupId() const
52{
53 return u"vectorgeneral"_s;
54}
55
56QString QgsRepairShapefileAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." );
59}
60
61QString QgsRepairShapefileAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." );
64}
65
66QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const
67{
68 return new QgsRepairShapefileAlgorithm();
69}
70
71void QgsRepairShapefileAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter(
74 new QgsProcessingParameterFile( u"INPUT"_s, QObject::tr( "Input Shapefile" ), Qgis::ProcessingFileParameterBehavior::File, u"shp"_s, QVariant(), false, QObject::tr( "ESRI Shapefile" ) + u" (*.shp *.SHP)"_s )
75 );
76
77 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Repaired layer" ) ) );
78}
79
80QVariantMap QgsRepairShapefileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
81{
82 const QString path = parameterAsFile( parameters, u"INPUT"_s, context );
83
84 if ( !QFile::exists( path ) )
85 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( "INPUT"_L1 ) );
86
87 CPLSetConfigOption( "SHAPE_RESTORE_SHX", "YES" );
88
89 auto layer = std::make_unique<QgsVectorLayer>( path );
90 if ( !layer->isValid() )
91 {
92 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
93 throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) );
94 }
95
96 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
97
98 feedback->pushInfo( QObject::tr( "Successfully repaired, found %n feature(s)", nullptr, layer->featureCount() ) );
99
100 QVariantMap outputs;
101 outputs.insert( u"OUTPUT"_s, path );
102 return outputs;
103}
104
@ File
Parameter is a single file.
Definition qgis.h:3906
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A vector layer output for processing algorithms.
An input file or folder parameter for processing algorithms.