QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "qgsvectorlayer.h"
21#include "cpl_conv.h"
22
24
25QString QgsRepairShapefileAlgorithm::name() const
26{
27 return QStringLiteral( "repairshapefile" );
28}
29
30QString QgsRepairShapefileAlgorithm::displayName() const
31{
32 return QObject::tr( "Repair Shapefile" );
33}
34
35QStringList QgsRepairShapefileAlgorithm::tags() const
36{
37 return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' );
38}
39
40QString QgsRepairShapefileAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsRepairShapefileAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50QString QgsRepairShapefileAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." );
53}
54
55QString QgsRepairShapefileAlgorithm::shortDescription() const
56{
57 return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." );
58}
59
60QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const
61{
62 return new QgsRepairShapefileAlgorithm();
63}
64
65void QgsRepairShapefileAlgorithm::initAlgorithm( const QVariantMap & )
66{
67 addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input Shapefile" ), Qgis::ProcessingFileParameterBehavior::File,
68 QStringLiteral( "shp" ), QVariant(), false, QObject::tr( "ESRI Shapefile" ) +
69 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 std::unique_ptr< QgsVectorLayer > 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.
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.