QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmaptoolcapturelayergeometry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolcapturelayergeometry.cpp - base class for map tools digitizing layer geometries
3  ---------------------
4  begin : January 2022
5  copyright : (C) Denis Rouzaud
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 
18 #include "qgsproject.h"
19 #include "qgscurvepolygon.h"
20 #include "qgscurve.h"
21 
22 
23 
24 
25 void QgsMapToolCaptureLayerGeometry::geometryCaptured( const QgsGeometry &geometry )
26 {
27  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
28  if ( !vlayer )
29  return;
30 
31  QgsGeometry g( geometry );
32 
33  switch ( mode() )
34  {
37  break;
40  //does compoundcurve contain circular strings?
41  //does provider support circular strings?
42  const bool hasCurvedSegments = captureCurve()->hasCurvedSegments();
43  const bool providerSupportsCurvedSegments = vlayer && ( vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::CircularGeometries );
44  if ( !hasCurvedSegments || !providerSupportsCurvedSegments )
45  g = QgsGeometry( g.constGet()->segmentize() );
46 
47  QList<QgsVectorLayer *> avoidIntersectionsLayers;
48  switch ( QgsProject::instance()->avoidIntersectionsMode() )
49  {
51  if ( vlayer )
52  avoidIntersectionsLayers.append( vlayer );
53  break;
55  avoidIntersectionsLayers = QgsProject::instance()->avoidIntersectionsLayers();
56  break;
58  break;
59  }
60  if ( avoidIntersectionsLayers.size() > 0 )
61  {
62  const int avoidIntersectionsReturn = g.avoidIntersections( avoidIntersectionsLayers );
63  if ( avoidIntersectionsReturn == 3 )
64  {
65  emit messageEmitted( tr( "The feature has been added, but at least one geometry intersected is invalid. These geometries must be manually repaired." ), Qgis::MessageLevel::Warning );
66  }
67  if ( g.isEmpty() ) //avoid intersection might have removed the whole geometry
68  {
69  emit messageEmitted( tr( "The feature cannot be added because its geometry collapsed due to intersection avoidance" ), Qgis::MessageLevel::Critical );
70  stopCapturing();
71  return;
72  }
73  }
74  break;
75  }
76 
77  layerGeometryCaptured( g );
78 
79  switch ( mode() )
80  {
82  break;
84  layerPointCaptured( *qgsgeometry_cast<const QgsPoint *>( g.constGet() ) );
85  break;
87  layerLineCaptured( qgsgeometry_cast<const QgsCurve *>( g.constGet() ) );
88  break;
90  layerPolygonCaptured( qgsgeometry_cast<const QgsCurvePolygon *>( g.constGet() ) );
91  break;
92  }
93 }
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
QgsCompoundCurve::hasCurvedSegments
bool hasCurvedSegments() const override
Returns true if the geometry contains curved segments.
Definition: qgscompoundcurve.cpp:1166
QgsProject::avoidIntersectionsLayers
QList< QgsVectorLayer * > avoidIntersectionsLayers
Definition: qgsproject.h:116
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsMapToolAdvancedDigitizing::layer
virtual QgsMapLayer * layer() const
Returns the layer associated with the map tool.
Definition: qgsmaptooladvanceddigitizing.cpp:145
QgsMapToolCapture::mode
CaptureMode mode() const
The capture mode.
Definition: qgsmaptoolcapture.h:112
QgsVectorDataProvider::capabilities
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:208
QgsMapToolCapture::captureCurve
const QgsCompoundCurve * captureCurve() const
Gets the capture curve.
Definition: qgsmaptoolcapture.h:129
Qgis::AvoidIntersectionsMode::AllowIntersections
@ AllowIntersections
Overlap with any feature allowed when digitizing new features.
QgsMapTool::messageEmitted
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
QgsMapToolCapture::CapturePolygon
@ CapturePolygon
Capture polygons.
Definition: qgsmaptoolcapture.h:61
QgsMapToolCapture::CaptureLine
@ CaptureLine
Capture lines.
Definition: qgsmaptoolcapture.h:60
qgscurvepolygon.h
Qgis::AvoidIntersectionsMode::AvoidIntersectionsLayers
@ AvoidIntersectionsLayers
Overlap with features from a specified list of layers when digitizing new features not allowed.
qgsmaptoolcapturelayergeometry.h
Qgis::AvoidIntersectionsMode::AvoidIntersectionsCurrentLayer
@ AvoidIntersectionsCurrentLayer
Overlap with features from the active layer when digitizing new features not allowed.
qgscurve.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapToolCapture::CaptureNone
@ CaptureNone
Do not capture / determine mode from layer geometry type.
Definition: qgsmaptoolcapture.h:58
QgsVectorDataProvider::CircularGeometries
@ CircularGeometries
Supports circular geometry types (circularstring, compoundcurve, curvepolygon)
Definition: qgsvectordataprovider.h:88
QgsMapToolCapture::CapturePoint
@ CapturePoint
Capture points.
Definition: qgsmaptoolcapture.h:59
QgsMapToolCapture::stopCapturing
void stopCapturing()
Stop capturing.
Definition: qgsmaptoolcapture.cpp:973
qgsproject.h