QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
26
27QString QgsRepairShapefileAlgorithm::name() const
28{
29 return QStringLiteral( "repairshapefile" );
30}
31
32QString QgsRepairShapefileAlgorithm::displayName() const
33{
34 return QObject::tr( "Repair Shapefile" );
35}
36
37QStringList QgsRepairShapefileAlgorithm::tags() const
38{
39 return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' );
40}
41
42QString QgsRepairShapefileAlgorithm::group() const
43{
44 return QObject::tr( "Vector general" );
45}
46
47QString QgsRepairShapefileAlgorithm::groupId() const
48{
49 return QStringLiteral( "vectorgeneral" );
50}
51
52QString QgsRepairShapefileAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." );
55}
56
57QString QgsRepairShapefileAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." );
60}
61
62QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const
63{
64 return new QgsRepairShapefileAlgorithm();
65}
66
67void QgsRepairShapefileAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input Shapefile" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "shp" ), QVariant(), false, QObject::tr( "ESRI Shapefile" ) + QStringLiteral( " (*.shp *.SHP)" ) ) );
70
71 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Repaired layer" ) ) );
72}
73
74QVariantMap QgsRepairShapefileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
77
78 if ( !QFile::exists( path ) )
79 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
80
81 CPLSetConfigOption( "SHAPE_RESTORE_SHX", "YES" );
82
83 auto layer = std::make_unique<QgsVectorLayer>( path );
84 if ( !layer->isValid() )
85 {
86 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
87 throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) );
88 }
89
90 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
91
92 feedback->pushInfo( QObject::tr( "Successfully repaired, found %n feature(s)", nullptr, layer->featureCount() ) );
93
94 QVariantMap outputs;
95 outputs.insert( QStringLiteral( "OUTPUT" ), path );
96 return outputs;
97}
98
@ File
Parameter is a single file.
Definition qgis.h:3789
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.