QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
21 
22 QString QgsFixGeometriesAlgorithm::name() const
23 {
24  return QStringLiteral( "fixgeometries" );
25 }
26 
27 QString QgsFixGeometriesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Fix geometries" );
30 }
31 
32 QStringList QgsFixGeometriesAlgorithm::tags() const
33 {
34  return QObject::tr( "repair,invalid,geometry,make,valid" ).split( ',' );
35 }
36 
37 QString QgsFixGeometriesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsFixGeometriesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QgsProcessingFeatureSource::Flag QgsFixGeometriesAlgorithm::sourceFlags() const
48 {
50 }
51 
52 QString QgsFixGeometriesAlgorithm::outputName() const
53 {
54  return QObject::tr( "Fixed geometries" );
55 }
56 
57 QgsWkbTypes::Type QgsFixGeometriesAlgorithm::outputWkbType( QgsWkbTypes::Type type ) const
58 {
59  return QgsWkbTypes::multiType( type );
60 }
61 
62 QString QgsFixGeometriesAlgorithm::shortHelpString() const
63 {
64  return QObject::tr( "This algorithm attempts to create a valid representation of a given invalid geometry without "
65  "losing any of the input vertices. Already-valid geometries are returned without further intervention. "
66  "Always outputs multi-geometry layer.\n\n"
67  "NOTE: M values will be dropped from the output." );
68 }
69 
70 QgsFixGeometriesAlgorithm *QgsFixGeometriesAlgorithm::createInstance() const
71 {
72  return new QgsFixGeometriesAlgorithm();
73 }
74 
75 QgsFeatureList QgsFixGeometriesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
76 {
77  if ( !feature.hasGeometry() )
78  return QgsFeatureList() << feature;
79 
80  QgsFeature outputFeature = feature;
81 
82  QgsGeometry outputGeometry = outputFeature.geometry().makeValid();
83  if ( !outputGeometry )
84  {
85  feedback->pushInfo( QObject::tr( "makeValid failed for feature %1 " ).arg( feature.id() ) );
86  outputFeature.clearGeometry();
87  return QgsFeatureList() << outputFeature;
88  }
89 
90  if ( outputGeometry.wkbType() == QgsWkbTypes::Unknown ||
92  {
93  // keep only the parts of the geometry collection with correct type
94  const QVector< QgsGeometry > tmpGeometries = outputGeometry.asGeometryCollection();
95  QVector< QgsGeometry > matchingParts;
96  for ( const QgsGeometry &g : tmpGeometries )
97  {
98  if ( g.type() == feature.geometry().type() )
99  matchingParts << g;
100  }
101  if ( !matchingParts.empty() )
102  outputGeometry = QgsGeometry::collectGeometry( matchingParts );
103  else
104  outputGeometry = QgsGeometry();
105  }
106 
107  outputGeometry.convertToMultiType();
108  if ( QgsWkbTypes::geometryType( outputGeometry.wkbType() ) != QgsWkbTypes::geometryType( feature.geometry().wkbType() ) )
109  {
110  // don't keep geometries which have different types - e.g. lines converted to points
111  feedback->pushInfo( QObject::tr( "Fixing geometry for feature %1 resulted in %2, geometry has been dropped." ).arg( feature.id() ).arg( QgsWkbTypes::displayString( outputGeometry.wkbType() ) ) );
112  outputFeature.clearGeometry();
113  }
114  else
115  {
116  outputFeature.setGeometry( outputGeometry );
117  }
118  return QgsFeatureList() << outputFeature;
119 }
120 
QgsFeatureId id
Definition: qgsfeature.h:71
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:296
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
QVector< QgsGeometry > asGeometryCollection() const
Returns contents of the geometry as a list of geometries.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:663
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:144
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
QgsGeometry makeValid() const
Attempts to make an invalid geometry valid without losing vertices.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:427
Contains information about the context in which a processing algorithm is executed.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.