QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmaptooldigitizefeature.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptooldigitizefeature.cpp
3 
4  ---------------------
5  begin : 7.12.2017
6  copyright : (C) 2017 by David Signer
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 
20 #include "qgsapplication.h"
21 #include "qgsattributedialog.h"
22 #include "qgscurvepolygon.h"
23 #include "qgsexception.h"
24 #include "qgsfields.h"
25 #include "qgsgeometry.h"
26 #include "qgslinestring.h"
27 #include "qgslogger.h"
28 #include "qgsmapcanvas.h"
29 #include "qgsmapmouseevent.h"
30 #include "qgsmultipoint.h"
31 #include "qgspolygon.h"
32 #include "qgsproject.h"
34 #include "qgsvectordataprovider.h"
35 #include "qgsvectorlayer.h"
36 
37 #include <QSettings>
38 
40  : QgsMapToolCaptureLayerGeometry( canvas, cadDockWidget, mode )
41  , mCheckGeometryType( true )
42 {
43  mToolName = tr( "Digitize feature" );
46 }
47 
48 QgsMapToolCapture::Capabilities QgsMapToolDigitizeFeature::capabilities() const
49 {
51 }
52 
54 {
55  switch ( technique )
56  {
58  return true;
63  }
64  return false;
65 }
66 
67 void QgsMapToolDigitizeFeature::layerGeometryCaptured( const QgsGeometry &geometry )
68 {
69  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
70  if ( !vlayer )
71  vlayer = currentVectorLayer();
72 
73  if ( !vlayer )
74  return;
75 
76  QgsFeature f( vlayer->fields(), 0 );
77 
78  if ( vlayer->isSpatial() )
79  {
80  const QgsWkbTypes::Type layerWKBType = vlayer->wkbType();
81 
82  QgsGeometry layerGeometry;
83 
84  if ( mCheckGeometryType )
85  {
88  QVector<QgsGeometry> layerGeometries = geometry.coerceToType( layerWKBType, defaultZ, defaultM );
89  if ( layerGeometries.count() > 0 )
90  layerGeometry = layerGeometries.at( 0 );
91 
92  if ( layerGeometry.wkbType() != layerWKBType && layerGeometry.wkbType() != QgsWkbTypes::linearType( layerWKBType ) )
93  {
94  emit messageEmitted( tr( "The digitized geometry type (%1) does not correspond to the layer geometry type (%2)." ).arg( QgsWkbTypes::displayString( layerGeometry.wkbType() ), QgsWkbTypes::displayString( layerWKBType ) ), Qgis::MessageLevel::Warning );
95  return;
96  }
97  }
98  else
99  {
100  layerGeometry = geometry;
101  }
102  f.setGeometry( layerGeometry );
103  }
104  f.setValid( true );
105  emit digitizingCompleted( f );
106  featureDigitized( f );
107 }
108 
110 {
111  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
112  if ( !vlayer )
113  vlayer = currentVectorLayer();
114 
115  if ( vlayer && vlayer->geometryType() == QgsWkbTypes::NullGeometry )
116  {
117  layerGeometryCaptured( QgsGeometry() );
118  return;
119  }
120 
121  if ( mLayer )
122  {
123  //remember current layer
124  mCurrentLayer = mCanvas->currentLayer();
125  //set the layer with the given
126  mCanvas->setCurrentLayer( mLayer );
127  }
128 
130 }
131 
133 {
135 
136  if ( mCurrentLayer )
137  //set the layer back to the one remembered
138  mCanvas->setCurrentLayer( mCurrentLayer );
139  emit digitizingFinished();
140 }
141 
143 {
144  return mCheckGeometryType;
145 }
146 
148 {
149  mCheckGeometryType = checkGeometryType;
150 }
151 
153 {
154  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
155 
156  if ( !vlayer )
157  //if no given layer take the current from canvas
158  vlayer = currentVectorLayer();
159 
160  if ( !vlayer )
161  {
163  return;
164  }
165 
166  QgsVectorDataProvider *provider = vlayer->dataProvider();
167 
168  if ( !( provider->capabilities() & QgsVectorDataProvider::AddFeatures ) )
169  {
170  emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
171  return;
172  }
173 
174  if ( !vlayer->isEditable() )
175  {
177  return;
178  }
179 
180  //check we only use this tool for point/multipoint layers
181  if ( mode() == CapturePoint && vlayer->geometryType() != QgsWkbTypes::PointGeometry && mCheckGeometryType )
182  {
183  emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
184  return;
185  }
186 
187  //check we only use the line tool for line/multiline layers
188  if ( mode() == CaptureLine && vlayer->geometryType() != QgsWkbTypes::LineGeometry && mCheckGeometryType )
189  {
190  emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
191  return;
192  }
193 
194  //check we only use the polygon tool for polygon/multipolygon layers
195  if ( mode() == CapturePolygon && vlayer->geometryType() != QgsWkbTypes::PolygonGeometry && mCheckGeometryType )
196  {
197  emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
198  return;
199  }
200 
202 }
203 
205 {
206  mLayer = vl;
207 }
208 
qgspolygon.h
qgsfields.h
QgsMapToolDigitizeFeature::supportsTechnique
bool supportsTechnique(Qgis::CaptureTechnique technique) const override
Returns true if the tool supports the specified capture technique.
Definition: qgsmaptooldigitizefeature.cpp:53
qgssettingsregistrycore.h
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsWkbTypes::displayString
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
Definition: qgswkbtypes.cpp:145
QgsMapToolDigitizeFeature::cadCanvasReleaseEvent
void cadCanvasReleaseEvent(QgsMapMouseEvent *e) override
Override this method when subclassing this class.
Definition: qgsmaptooldigitizefeature.cpp:152
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
qgslinestring.h
qgsmapcanvas.h
QgsWkbTypes::NullGeometry
@ NullGeometry
Definition: qgswkbtypes.h:146
QgsMapToolDigitizeFeature::checkGeometryType
bool checkGeometryType() const
Check if CaptureMode matches layer type.
Definition: qgsmaptooldigitizefeature.cpp:142
QgsMapToolCapture::CaptureMode
CaptureMode
Different capture modes.
Definition: qgsmaptoolcapture.h:56
QgsMapToolEdit::notifyNotVectorLayer
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
Definition: qgsmaptooledit.cpp:166
QgsProject::cleared
void cleared()
Emitted when the project is cleared (and additionally when an open project is cleared just before a n...
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
qgsmultipoint.h
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsProject::readProject
void readProject(const QDomDocument &)
Emitted when a project is being read.
QgsVectorLayer::isSpatial
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Definition: qgsvectorlayer.cpp:3733
QgsMapToolCapture::mode
CaptureMode mode() const
The capture mode.
Definition: qgsmaptoolcapture.h:112
QgsMapToolDigitizeFeature::activate
void activate() override
Registers this maptool with the cad dock widget.
Definition: qgsmaptooldigitizefeature.cpp:109
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsMapToolDigitizeFeature::capabilities
QgsMapToolCapture::Capabilities capabilities() const override
Returns flags containing the supported capabilities.
Definition: qgsmaptooldigitizefeature.cpp:48
QgsMapToolDigitizeFeature::setCheckGeometryType
void setCheckGeometryType(bool checkGeometryType)
Check if CaptureMode matches layer type.
Definition: qgsmaptooldigitizefeature.cpp:147
QgsMapTool::mCanvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
QgsMapToolEdit::notifyNotEditableLayer
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
Definition: qgsmaptooledit.cpp:171
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsMapToolEdit::currentVectorLayer
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
Definition: qgsmaptooledit.cpp:91
QgsSettingsRegistryCore::settingsDigitizingDefaultMValue
static const QgsSettingsEntryDouble settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
Definition: qgssettingsregistrycore.h:94
QgsVectorLayer::isEditable
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Definition: qgsvectorlayer.cpp:3728
qgsapplication.h
qgsmaptooldigitizefeature.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsVectorDataProvider::capabilities
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:208
QgsMapToolDigitizeFeature::digitizingCompleted
void digitizingCompleted(const QgsFeature &feature)
Emitted whenever the digitizing has been successfully completed.
QgsMapToolCapture::activate
void activate() override
Registers this maptool with the cad dock widget.
Definition: qgsmaptoolcapture.cpp:116
Qgis::CaptureTechnique::CircularString
@ CircularString
Capture in circular strings.
QgsWkbTypes::linearType
static Type linearType(Type type) SIP_HOLDGIL
Returns the linear type for a WKB type.
Definition: qgswkbtypes.h:622
QgsMapToolDigitizeFeature::setLayer
void setLayer(QgsMapLayer *vl)
Change the layer edited by the map tool.
Definition: qgsmaptooldigitizefeature.cpp:204
qgsvectordataprovider.h
QgsMapToolCapture::SupportsCurves
@ SupportsCurves
Supports curved geometries input.
Definition: qgsmaptoolcapture.h:68
qgsattributedialog.h
QgsVectorDataProvider::AddFeatures
@ AddFeatures
Allows adding features.
Definition: qgsvectordataprovider.h:75
QgsMapTool::messageEmitted
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
QgsMapToolCapture::CapturePolygon
@ CapturePolygon
Capture polygons.
Definition: qgsmaptoolcapture.h:61
QgsMapToolCaptureLayerGeometry
QgsMapToolCaptureLayerGeometry is a base class for map tools digitizing layer geometries This map too...
Definition: qgsmaptoolcapturelayergeometry.h:30
QgsMapToolCapture::CaptureLine
@ CaptureLine
Capture lines.
Definition: qgsmaptoolcapture.h:60
QgsAdvancedDigitizingDockWidget
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Definition: qgsadvanceddigitizingdockwidget.h:50
qgscurvepolygon.h
Qgis::CaptureTechnique::Shape
@ Shape
Digitize shapes.
qgsvectorlayer.h
QgsMapToolDigitizeFeature::QgsMapToolDigitizeFeature
QgsMapToolDigitizeFeature(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode=QgsMapToolCapture::CaptureNone)
QgsMapToolDigitizeFeature is a map tool to digitize a feature geometry.
Definition: qgsmaptooldigitizefeature.cpp:39
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
qgsadvanceddigitizingdockwidget.h
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
QgsGeometry::coerceToType
QVector< QgsGeometry > coerceToType(QgsWkbTypes::Type type, double defaultZ=0, double defaultM=0) const
Attempts to coerce this geometry into the specified destination type.
Definition: qgsgeometry.cpp:1431
qgsgeometry.h
QgsMapToolCapture::cadCanvasReleaseEvent
void cadCanvasReleaseEvent(QgsMapMouseEvent *e) override
Override this method when subclassing this class.
Definition: qgsmaptoolcapture.cpp:1229
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Qgis::CaptureTechnique::StraightSegments
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapToolDigitizeFeature::deactivate
void deactivate() override
Unregisters this maptool from the cad dock widget.
Definition: qgsmaptooldigitizefeature.cpp:132
QgsSettingsRegistryCore::settingsDigitizingDefaultZValue
static const QgsSettingsEntryDouble settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
Definition: qgssettingsregistrycore.h:91
QgsMapToolCapture::CapturePoint
@ CapturePoint
Capture points.
Definition: qgsmaptoolcapture.h:59
QgsMapToolCapture::stopCapturing
void stopCapturing()
Stop capturing.
Definition: qgsmaptoolcapture.cpp:973
QgsMapToolCapture::ValidateGeometries
@ ValidateGeometries
Tool supports geometry validation (since QGIS 3.22)
Definition: qgsmaptoolcapture.h:69
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:58
qgsexception.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsMapToolCapture::deactivate
void deactivate() override
Unregisters this maptool from the cad dock widget.
Definition: qgsmaptoolcapture.cpp:128
qgslogger.h
Qgis::CaptureTechnique
CaptureTechnique
Capture technique.
Definition: qgis.h:152
qgsmapmouseevent.h
QgsVectorLayer::geometryType
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Definition: qgsvectorlayer.cpp:720
Qgis::CaptureTechnique::Streaming
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
QgsMapToolDigitizeFeature::digitizingFinished
void digitizingFinished()
Emitted whenever the digitizing has been ended without digitizing any feature.
qgsproject.h
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357
QgsSettingsEntryByValue::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:520
QgsMapTool::mToolName
QString mToolName
The translated name of the map tool.
Definition: qgsmaptool.h:354