QGIS API Documentation 3.99.0-Master (357b655ed83)
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( 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 ) );
74
75 addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Repaired layer" ) ) );
76}
77
78QVariantMap QgsRepairShapefileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
79{
80 const QString path = parameterAsFile( parameters, u"INPUT"_s, context );
81
82 if ( !QFile::exists( path ) )
83 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( "INPUT"_L1 ) );
84
85 CPLSetConfigOption( "SHAPE_RESTORE_SHX", "YES" );
86
87 auto layer = std::make_unique<QgsVectorLayer>( path );
88 if ( !layer->isValid() )
89 {
90 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
91 throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) );
92 }
93
94 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
95
96 feedback->pushInfo( QObject::tr( "Successfully repaired, found %n feature(s)", nullptr, layer->featureCount() ) );
97
98 QVariantMap outputs;
99 outputs.insert( u"OUTPUT"_s, path );
100 return outputs;
101}
102
@ File
Parameter is a single file.
Definition qgis.h:3860
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.