QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmfixgeometries.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmfixgeometries.cpp
3 -----------------------------
4 begin : April 2017
5 copyright : (C) 2017 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
21#include <geos_c.h>
22
24
25QString QgsFixGeometriesAlgorithm::name() const
26{
27 return QStringLiteral( "fixgeometries" );
28}
29
30QString QgsFixGeometriesAlgorithm::displayName() const
31{
32 return QObject::tr( "Fix geometries" );
33}
34
35QStringList QgsFixGeometriesAlgorithm::tags() const
36{
37 return QObject::tr( "repair,invalid,geometry,make,valid,error" ).split( ',' );
38}
39
40QString QgsFixGeometriesAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsFixGeometriesAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50Qgis::ProcessingFeatureSourceFlags QgsFixGeometriesAlgorithm::sourceFlags() const
51{
53}
54
55QString QgsFixGeometriesAlgorithm::outputName() const
56{
57 return QObject::tr( "Fixed geometries" );
58}
59
60Qgis::WkbType QgsFixGeometriesAlgorithm::outputWkbType( Qgis::WkbType type ) const
61{
63}
64
65QString QgsFixGeometriesAlgorithm::shortHelpString() const
66{
67 return QObject::tr( "This algorithm attempts to create a valid representation of a given invalid geometry without "
68 "losing any of the input vertices. Already-valid geometries are returned without further intervention. "
69 "Always outputs multi-geometry layer.\n\n"
70 "NOTE: M values will be dropped from the output." );
71}
72
73QString QgsFixGeometriesAlgorithm::shortDescription() const
74{
75 return QObject::tr( "Attempts to create a valid representation of a given invalid geometry without losing any of the input vertices." );
76}
77
78QgsFixGeometriesAlgorithm *QgsFixGeometriesAlgorithm::createInstance() const
79{
80 return new QgsFixGeometriesAlgorithm();
81}
82
83bool QgsFixGeometriesAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
84{
85 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
86 if ( !layer )
87 return false;
88
90 return false;
91 // The algorithm would drop M, so disable it if the layer has M
92 return !QgsWkbTypes::hasM( layer->wkbType() );
93}
94
95void QgsFixGeometriesAlgorithm::initParameters( const QVariantMap & )
96{
97 auto methodParameter = std::make_unique<QgsProcessingParameterEnum>(
98 QStringLiteral( "METHOD" ),
99 QObject::tr( "Repair method" ),
100 QStringList { QObject::tr( "Linework" ), QObject::tr( "Structure" ) },
101 0,
102 false
103 );
104#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 10
105 methodParameter->setDefaultValue( 0 );
106#else
107 methodParameter->setDefaultValue( 1 );
108#endif
109 addParameter( methodParameter.release() );
110}
111
112bool QgsFixGeometriesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
113{
114 mMethod = static_cast<Qgis::MakeValidMethod>( parameterAsInt( parameters, QStringLiteral( "METHOD" ), context ) );
115#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 10
116 if ( mMethod == Qgis::MakeValidMethod::Structure )
117 {
118 throw QgsProcessingException( QObject::tr( "The structured method to make geometries valid requires a QGIS build based on GEOS 3.10 or later" ) );
119 }
120#endif
121 return true;
122}
123
124QgsFeatureList QgsFixGeometriesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
125{
126 if ( !feature.hasGeometry() )
127 return QgsFeatureList() << feature;
128
129 QgsFeature outputFeature = feature;
130
131 QgsGeometry outputGeometry = outputFeature.geometry().makeValid( mMethod );
132 if ( outputGeometry.isNull() )
133 {
134 feedback->pushInfo( QObject::tr( "makeValid failed for feature %1 " ).arg( feature.id() ) );
135 outputFeature.clearGeometry();
136 return QgsFeatureList() << outputFeature;
137 }
138
139 if ( outputGeometry.wkbType() == Qgis::WkbType::Unknown || QgsWkbTypes::flatType( outputGeometry.wkbType() ) == Qgis::WkbType::GeometryCollection )
140 {
141 // keep only the parts of the geometry collection with correct type
142 const QVector<QgsGeometry> tmpGeometries = outputGeometry.asGeometryCollection();
143 QVector<QgsGeometry> matchingParts;
144 for ( const QgsGeometry &g : tmpGeometries )
145 {
146 if ( g.type() == feature.geometry().type() )
147 matchingParts << g;
148 }
149 if ( !matchingParts.empty() )
150 outputGeometry = QgsGeometry::collectGeometry( matchingParts );
151 else
152 outputGeometry = QgsGeometry();
153 }
154
155 if ( outputGeometry.type() != Qgis::GeometryType::Point )
156 {
157 // some data providers are picky about the geometries we pass to them: we can't add single-part geometries
158 // when we promised multi-part geometries, so ensure we have the right type
159 outputGeometry.convertToMultiType();
160 }
161
162 if ( QgsWkbTypes::geometryType( outputGeometry.wkbType() ) != QgsWkbTypes::geometryType( feature.geometry().wkbType() ) )
163 {
164 // don't keep geometries which have different types - e.g. lines converted to points
165 feedback->pushInfo( QObject::tr( "Fixing geometry for feature %1 resulted in %2, geometry has been dropped." ).arg( feature.id() ).arg( QgsWkbTypes::displayString( outputGeometry.wkbType() ) ) );
166 outputFeature.clearGeometry();
167 }
168 else
169 {
170 outputFeature.setGeometry( outputGeometry );
171 }
172 return QgsFeatureList() << outputFeature;
173}
174
MakeValidMethod
Algorithms to use when repairing invalid geometries.
Definition qgis.h:2130
@ Structure
Structured method, first makes all rings valid and then merges shells and subtracts holes from shells...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Unknown
Unknown.
@ GeometryCollection
GeometryCollection.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
void clearGeometry()
Removes any geometry associated with the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
QgsGeometry makeValid(Qgis::MakeValidMethod method=Qgis::MakeValidMethod::Linework, bool keepCollapsed=false) const
Attempts to make an invalid geometry valid without losing vertices.
QVector< QgsGeometry > asGeometryCollection() const
Returns contents of the geometry as a list of geometries.
Qgis::GeometryType type
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Base class for all map layer types.
Definition qgsmaplayer.h:77
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Represents a vector layer which manages a vector based dataset.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType promoteNonPointTypesToMulti(Qgis::WkbType type)
Promotes a WKB geometry type to its multi-type equivalent, with the exception of point geometry types...
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
QList< QgsFeature > QgsFeatureList