QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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, QStringLiteral( "shp" ), QVariant(), false, QObject::tr( "ESRI Shapefile" ) + QStringLiteral( " (*.shp *.SHP)" ) ) );
68
69 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Repaired layer" ) ) );
70}
71
72QVariantMap QgsRepairShapefileAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
73{
74 const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
75
76 if ( !QFile::exists( path ) )
77 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
78
79 CPLSetConfigOption( "SHAPE_RESTORE_SHX", "YES" );
80
81 std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( path );
82 if ( !layer->isValid() )
83 {
84 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
85 throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) );
86 }
87
88 CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
89
90 feedback->pushInfo( QObject::tr( "Successfully repaired, found %n feature(s)", nullptr, layer->featureCount() ) );
91
92 QVariantMap outputs;
93 outputs.insert( QStringLiteral( "OUTPUT" ), path );
94 return outputs;
95}
96
@ 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.