Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][processing] New algorithm "Repair Shapefile"
Uses GDAL to repair shapefiles which have a broken or missing .SHX file
- Loading branch information
1 parent
b4bc7cb
commit 7746061
Showing
5 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/*************************************************************************** | ||
qgsalgorithmrepairshapefile.cpp | ||
--------------------- | ||
begin : December 2019 | ||
copyright : (C) 2019 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmrepairshapefile.h" | ||
#include "qgsvectorlayer.h" | ||
#include "qgsvectordataprovider.h" | ||
#include "cpl_conv.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsRepairShapefileAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "repairshapefile" ); | ||
} | ||
|
||
QString QgsRepairShapefileAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Repair Shapefile" ); | ||
} | ||
|
||
QStringList QgsRepairShapefileAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "fix,shp,shx,broken,missing" ).split( ',' ); | ||
} | ||
|
||
QString QgsRepairShapefileAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector general" ); | ||
} | ||
|
||
QString QgsRepairShapefileAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeneral" ); | ||
} | ||
|
||
QString QgsRepairShapefileAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "Repairs a broken Shapefile by recreating missing or broken SHX files." ); | ||
} | ||
|
||
QString QgsRepairShapefileAlgorithm::shortDescription() const | ||
{ | ||
return QObject::tr( "Repairs broken Shapefiles by recreating SHX files." ); | ||
} | ||
|
||
QgsRepairShapefileAlgorithm *QgsRepairShapefileAlgorithm::createInstance() const | ||
{ | ||
return new QgsRepairShapefileAlgorithm(); | ||
} | ||
|
||
void QgsRepairShapefileAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input Shapefile" ), QgsProcessingParameterFile::File, | ||
QStringLiteral( "shp" ), QVariant(), false, QObject::tr( "ESRI Shapefile" ) + | ||
QStringLiteral( " (*.shp *.SHP)" ) ) ); | ||
|
||
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Repaired layer" ) ) ); | ||
} | ||
|
||
QVariantMap QgsRepairShapefileAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context ); | ||
|
||
if ( !QFile::exists( path ) ) | ||
throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QStringLiteral( "INPUT" ) ) ); | ||
|
||
CPLSetConfigOption( "SHAPE_RESTORE_SHX", "YES" ); | ||
|
||
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( path ); | ||
if ( !layer->isValid() ) | ||
throw QgsProcessingException( QObject::tr( "Could not repair %1." ).arg( path ) ); | ||
|
||
CPLSetConfigOption( "SHAPE_RESTORE_SHX", nullptr ); | ||
|
||
feedback->pushInfo( QObject::tr( "Successfully repaired, found %1 features" ).arg( layer->featureCount() ) ); | ||
|
||
QVariantMap outputs; | ||
outputs.insert( QStringLiteral( "OUTPUT" ), path ); | ||
return outputs; | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*************************************************************************** | ||
qgsalgorithmrepairshapefile.h | ||
--------------------- | ||
begin : December 2019 | ||
copyright : (C) 2019 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMREPAIRSHAPEFILE_H | ||
#define QGSALGORITHMREPAIRSHAPEFILE_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native repair shapefile algorithm | ||
*/ | ||
class QgsRepairShapefileAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsRepairShapefileAlgorithm() = default; | ||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QString shortDescription() const override; | ||
QgsRepairShapefileAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMREPAIRSHAPEFILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters