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 
18 #include "qgsalgorithmrepairshapefile.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsvectordataprovider.h"
21 #include "cpl_conv.h"
22 
23 ///@cond PRIVATE
24 
name() const25 QString QgsRepairShapefileAlgorithm::name() const
26 {
27   return QStringLiteral( "repairshapefile" );
28 }
29 
displayName() const30 QString QgsRepairShapefileAlgorithm::displayName() const
31 {
32   return QObject::tr( "Repair Shapefile" );
33 }
34 
tags() const35 QStringList QgsRepairShapefileAlgorithm::tags() const
36 {
37   return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' );
38 }
39 
group() const40 QString QgsRepairShapefileAlgorithm::group() const
41 {
42   return QObject::tr( "Vector general" );
43 }
44 
groupId() const45 QString QgsRepairShapefileAlgorithm::groupId() const
46 {
47   return QStringLiteral( "vectorgeneral" );
48 }
49 
shortHelpString() const50 QString QgsRepairShapefileAlgorithm::shortHelpString() const
51 {
52   return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." );
53 }
54 
shortDescription() const55 QString QgsRepairShapefileAlgorithm::shortDescription() const
56 {
57   return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." );
58 }
59 
createInstance() const60 QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const
61 {
62   return new QgsRepairShapefileAlgorithm();
63 }
64 
initAlgorithm(const QVariantMap &)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 
processAlgorithm(const QVariantMap & parameters,QgsProcessingContext & context,QgsProcessingFeedback * feedback)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 = qgis::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 %1 features" ).arg( layer->featureCount() ) );
90 
91   QVariantMap outputs;
92   outputs.insert( QStringLiteral( "OUTPUT" ), path );
93   return outputs;
94 }
95 
96 ///@endcond
97