QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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"
20 #include "qgsvectordataprovider.h"
21 #include "cpl_conv.h"
22 
24 
25 QString QgsRepairShapefileAlgorithm::name() const
26 {
27  return QStringLiteral( "repairshapefile" );
28 }
29 
30 QString QgsRepairShapefileAlgorithm::displayName() const
31 {
32  return QObject::tr( "Repair Shapefile" );
33 }
34 
35 QStringList QgsRepairShapefileAlgorithm::tags() const
36 {
37  return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' );
38 }
39 
40 QString QgsRepairShapefileAlgorithm::group() const
41 {
42  return QObject::tr( "Vector general" );
43 }
44 
45 QString QgsRepairShapefileAlgorithm::groupId() const
46 {
47  return QStringLiteral( "vectorgeneral" );
48 }
49 
50 QString QgsRepairShapefileAlgorithm::shortHelpString() const
51 {
52  return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." );
53 }
54 
55 QString QgsRepairShapefileAlgorithm::shortDescription() const
56 {
57  return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." );
58 }
59 
60 QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const
61 {
62  return new QgsRepairShapefileAlgorithm();
63 }
64 
65 void QgsRepairShapefileAlgorithm::initAlgorithm( const QVariantMap & )
66 {
67  addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input Shapefile" ), QgsProcessingParameterFile::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 
74 QVariantMap 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  throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) );
86 
87  CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr );
88 
89  feedback->pushInfo( QObject::tr( "Successfully repaired, found %n feature(s)", nullptr, layer->featureCount() ) );
90 
91  QVariantMap outputs;
92  outputs.insert( QStringLiteral( "OUTPUT" ), path );
93  return outputs;
94 }
95 
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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.
@ File
Parameter is a single file.