47 , mCaptureMode( mode )
48 , mCaptureModeFromLayer( mode == CaptureNone )
55 this, &QgsMapToolCapture::currentLayerChanged );
60 mExtraSnapLayer =
new QgsVectorLayer( QStringLiteral(
"LineString?crs=" ), QStringLiteral(
"extra snap" ), QStringLiteral(
"memory" ), layerOptions );
61 mExtraSnapLayer->startEditing();
63 mExtraSnapLayer->addFeature( f );
64 mExtraSnapFeatureId = f.
id();
67 this, &QgsMapToolCapture::updateExtraSnapLayer );
79 mExtraSnapLayer->deleteLater();
80 mExtraSnapLayer =
nullptr;
86 mValidator->deleteLater();
98 if ( mTempRubberBand )
99 mTempRubberBand->show();
107 if ( mTempRubberBand )
108 mTempRubberBand->hide();
116 void QgsMapToolCapture::currentLayerChanged(
QgsMapLayer *layer )
118 if ( !mCaptureModeFromLayer )
145 if ( mTempRubberBand )
152 bool QgsMapToolCapture::tracingEnabled()
160 QgsPointXY QgsMapToolCapture::tracingStartPoint()
165 return mTracingStartPoint;
167 return lastCapturedMapPoint();
193 mTempRubberBand->addPoint( lastCapturedMapPoint() );
199 QgsPoint lastPoint = lastCapturedMapPoint();
201 if ( lastPointXY == pt0 &&
points[0] != lastPointXY )
217 for (
int i = 1; i <
points.count(); ++i )
228 bool QgsMapToolCapture::tracingAddVertex(
const QgsPointXY &point )
234 if ( mTempRubberBand->pointsCount() == 0 )
236 if ( !tracer->
init() )
246 mTracingStartPoint = point;
263 for (
int i = 0; i <
points.count(); ++i )
272 mCaptureCurve.
moveVertex( lastVertexId, layerPoints.first() );
273 mSnappingMatches.removeLast();
276 int pointBefore = mCaptureCurve.
numPoints();
283 if ( settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve" ),
false ).toBool() )
287 if (
capabilities().testFlag( QgsMapToolCapture::Capability::SupportsCurves ) && vlayer->
dataProvider()->
capabilities().testFlag( QgsVectorDataProvider::Capability::CircularGeometries ) )
291 settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve_angle_tolerance" ), 1e-6 ).toDouble(),
292 settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve_distance_tolerance" ), 1e-6 ).toDouble()
294 mCaptureCurve = *qgsgeometry_cast<QgsCompoundCurve *>( curved.
constGet() );
299 int pointAfter = mCaptureCurve.
numPoints();
300 for ( ; pointBefore < pointAfter; ++pointBefore )
312 QgsMapToolCaptureRubberBand *QgsMapToolCapture::createCurveRubberBand()
const
315 QgsMapToolCaptureRubberBand *rb =
new QgsMapToolCaptureRubberBand(
mCanvas );
319 double alphaScale = settings.
value( QStringLiteral(
"qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
320 color.setAlphaF( color.alphaF() * alphaScale );
321 rb->setLineStyle( Qt::DotLine );
322 rb->setStrokeColor( color );
325 rb->setFillColor( fillColor );
330 QgsPoint QgsMapToolCapture::firstCapturedMapPoint()
332 return mCaptureFirstPoint;
335 QgsPoint QgsMapToolCapture::lastCapturedMapPoint()
337 return mCaptureLastPoint;
340 void QgsMapToolCapture::resetRubberBand()
358 if ( mTempRubberBand )
359 mTempRubberBand->setStringType( mDigitizingType );
372 if ( mCaptureMode !=
CapturePoint && mTempRubberBand && mCapturing )
374 bool hasTrace =
false;
377 if ( tracingEnabled() && mCaptureCurve.
numPoints() != 0 )
384 mTempRubberBand->curveIsComplete() )
385 mCircularItermediatePoint = mTempRubberBand->pointFromEnd( 1 );
387 !mTempRubberBand->curveIsComplete() )
388 mCircularItermediatePoint =
QgsPoint();
390 hasTrace = tracingMouseMove( e );
396 mTempRubberBand->addPoint( lastCapturedMapPoint() );
397 if ( !mCircularItermediatePoint.
isEmpty() )
399 mTempRubberBand->movePoint( mCircularItermediatePoint );
400 mTempRubberBand->addPoint( mCircularItermediatePoint );
409 const QgsPoint mapPt = lastCapturedMapPoint();
411 if ( mTempRubberBand )
413 mTempRubberBand->movePoint(
mapPoint );
414 mTempRubberBand->movePoint( 0, mapPt );
421 else if ( mTempRubberBand )
422 mTempRubberBand->movePoint(
mapPoint );
433 QgsDebugMsg( QStringLiteral(
"no vector layer" ) );
448 QgsDebugMsg( QStringLiteral(
"transformation to layer coordinate failed" ) );
466 ( sourceLayer->
crs() == vlayer->
crs() ) )
484 QgsLineString line( geom.constGet()->vertexAt( vId ), geom.constGet()->vertexAt( vId2 ) );
490 layerPoint = geom.constGet()->
vertexAt( vId );
530 QgsDebugMsg( QStringLiteral(
"invalid capture mode" ) );
550 mSnappingMatches.append( match );
554 if ( mCaptureFirstPoint.
isEmpty() )
562 if ( !mTempRubberBand )
564 mTempRubberBand.reset( createCurveRubberBand() );
565 mTempRubberBand->setStringType( mDigitizingType );
569 bool traceCreated =
false;
570 if ( tracingEnabled() )
572 traceCreated = tracingAddVertex(
mapPoint );
577 mTracingStartPoint = traceCreated ? point :
QgsPointXY();
582 mTempRubberBand->movePoint(
mapPoint );
583 if ( mTempRubberBand->curveIsComplete() )
585 const QgsCurve *curve = mTempRubberBand->curve();
591 if ( match.
isValid() && mSnappingMatches.count() > 0 && !mSnappingMatches.last().isValid() )
593 mSnappingMatches.removeLast();
597 mSnappingMatches.removeLast();
598 mSnappingMatches.append( mCircularIntermediateMatch );
600 mSnappingMatches.append( match );
606 else if ( mCaptureCurve.
numPoints() == 0 )
610 mSnappingMatches.append( match );
616 mCircularIntermediateMatch = match;
620 mTempRubberBand->addPoint(
mapPoint );
625 mTempRubberBand->addPoint( lastCapturedMapPoint() );
629 updateExtraSnapLayer();
647 if ( mTempRubberBand )
651 mTempRubberBand->addPoint( endPt );
667 int addedPoint = countAfter - countBefore;
669 updateExtraSnapLayer();
671 for (
int i = 0; i < addedPoint; ++i )
681 mCaptureCurve.
clear();
682 updateExtraSnapLayer();
687 return mSnappingMatches;
694 if ( mTempRubberBand )
696 if (
size() <= 1 && mTempRubberBand->pointsCount() != 0 )
699 QgsPoint lastPoint = mTempRubberBand->lastPoint();
703 mTempRubberBand->removeLastPoint();
704 mTempRubberBand->movePoint( lastPoint );
709 vertexToRemove.
part = 0;
710 vertexToRemove.
ring = 0;
722 int pointsCountBefore = mCaptureCurve.
numPoints();
724 int pointsCountAfter = mCaptureCurve.
numPoints();
725 for ( ; pointsCountAfter < pointsCountBefore; pointsCountAfter++ )
726 if ( !mSnappingMatches.empty() )
727 mSnappingMatches.removeLast();
730 updateExtraSnapLayer();
740 mTempRubberBand->addPoint( lastCapturedMapPoint() );
741 mTempRubberBand->movePoint( lastPoint );
751 if ( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete )
758 else if ( e->key() == Qt::Key_Escape )
783 qDeleteAll( mGeomErrorMarkers );
784 mGeomErrorMarkers.clear();
793 mCaptureCurve.
clear();
794 updateExtraSnapLayer();
795 mSnappingMatches.clear();
797 lCurrentVectorLayer->triggerRepaint();
802 mTempRubberBand.reset();
813 mCaptureCurve.
close();
814 updateExtraSnapLayer();
817 void QgsMapToolCapture::validateGeometry()
820 if ( settings.
value( QStringLiteral(
"qgis/digitizing/validate_geometries" ), 1 ).toInt() == 0 )
825 mValidator->deleteLater();
826 mValidator =
nullptr;
830 while ( !mGeomErrorMarkers.isEmpty() )
832 delete mGeomErrorMarkers.takeFirst();
837 switch ( mCaptureMode )
851 exteriorRing->
close();
862 if ( settings.
value( QStringLiteral(
"qgis/digitizing/validate_geometries" ), 1 ).toInt() == 2 )
883 vm->setToolTip( e.
what() );
885 vm->setZValue( vm->zValue() + 1 );
886 mGeomErrorMarkers << vm;
897 QVector<QgsPointXY> pointsXY;
906 mCaptureCurve.
points( pts );
913 mCaptureCurve.
clear();
915 updateExtraSnapLayer();
916 mSnappingMatches.clear();
917 for (
int i = 0; i < line->
length(); ++i )
925 mCaptureCurve.
clear();
927 updateExtraSnapLayer();
928 mSnappingMatches.clear();
929 for (
int i = 0; i < line->
length(); ++i )
993 void QgsMapToolCapture::updateExtraSnapLayer()
995 if ( !mExtraSnapLayer )
1006 qgsgeometry_cast<QgsCompoundCurve *>( geom.
get() )->close();
1020 setVertexDrawingEnabled(
false );
1023 QgsCurve *QgsMapToolCaptureRubberBand::curve()
1025 if ( mPoints.empty() )
1028 switch ( mStringType )
1034 if ( mPoints.count() != 3 )
1046 bool QgsMapToolCaptureRubberBand::curveIsComplete()
const
1058 mFirstPolygonPoint = firstPolygonPoint;
1059 setStringType( stringType );
1060 setRubberBandGeometryType( geomType );
1069 void QgsMapToolCaptureRubberBand::addPoint(
const QgsPoint &point,
bool doUpdate )
1071 if ( mPoints.count() == 0 )
1072 mPoints.append( point );
1074 mPoints.append( point );
1080 void QgsMapToolCaptureRubberBand::movePoint(
const QgsPoint &point )
1082 if ( mPoints.count() > 0 )
1083 mPoints.last() = point ;
1088 void QgsMapToolCaptureRubberBand::movePoint(
int index,
const QgsPoint &point )
1090 if ( mPoints.count() > 0 && mPoints.size() > index )
1091 mPoints[index] = point;
1096 int QgsMapToolCaptureRubberBand::pointsCount()
1098 return mPoints.size();
1106 void QgsMapToolCaptureRubberBand::setStringType(
const QgsWkbTypes::Type &type )
1114 mPoints.removeAt( 1 );
1121 QgsPoint QgsMapToolCaptureRubberBand::lastPoint()
const
1123 if ( mPoints.empty() )
1126 return mPoints.last();
1129 QgsPoint QgsMapToolCaptureRubberBand::pointFromEnd(
int posFromEnd )
const
1131 if ( posFromEnd < mPoints.size() )
1132 return mPoints.at( mPoints.size() - 1 - posFromEnd );
1137 void QgsMapToolCaptureRubberBand::removeLastPoint()
1139 if ( mPoints.count() > 1 )
1140 mPoints.removeLast();
1150 void QgsMapToolCaptureRubberBand::updateCurve()
1152 std::unique_ptr<QgsCurve> curve;
1153 switch ( mStringType )
1156 curve.reset( createLinearString() );
1159 curve.reset( createCircularString() );
1169 geom->setExteriorRing( curve.release() );
1170 setGeometry( geom.release() );
1174 setGeometry( curve.release() );
1178 QgsCurve *QgsMapToolCaptureRubberBand::createLinearString()
1184 points.prepend( mFirstPolygonPoint );
1185 curve->setPoints( points );
1188 curve->setPoints( mPoints );
1190 return curve.release();
1193 QgsCurve *QgsMapToolCaptureRubberBand::createCircularString()
1196 curve->setPoints( mPoints );
1201 polygonCurve->addVertex( mFirstPolygonPoint );
1202 if ( !mPoints.empty() )
1203 polygonCurve->addVertex( mPoints.first() );
1204 polygonCurve->addCurve( curve.release() );
1205 return polygonCurve.release();
1208 return curve.release();
T * release()
Clears the pointer and returns it.
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Abstract base class for all geometries.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Circular string geometry type.
Compound curve geometry type.
QgsLineString * curveToLine(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a new line string geometry corresponding to a segmentized approximation of the curve.
void close()
Appends first point if not already closed.
void addCurve(QgsCurve *c)
Adds a curve to the geometry (takes ownership)
QgsPoint endPoint() const override SIP_HOLDGIL
Returns the end point of the curve.
int numPoints() const override SIP_HOLDGIL
Returns the number of points in the curve.
void removeCurve(int i)
Removes a curve from the geometry.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
QgsCompoundCurve * clone() const override
Clones the geometry by performing a deep copy.
void points(QgsPointSequence &pts) const override
Returns a list of points within the curve.
void clear() override
Clears the geometry, ie reset it to a null geometry.
QgsPoint startPoint() const override SIP_HOLDGIL
Returns the starting point of the curve.
void addVertex(const QgsPoint &pt)
Adds a vertex to the end of the geometry.
int nCurves() const SIP_HOLDGIL
Returns the number of curves in the geometry.
Custom exception class for Coordinate Reference System related exceptions.
Curve polygon geometry type.
Abstract base class for curved geometry type.
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A rubberband class for QgsAbstractGeometry (considering curved geometries).
void setGeometryType(const QgsWkbTypes::GeometryType &geometryType)
Sets which geometry is handled by the rubber band, polygon or line.
virtual void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
void errorFound(const QgsGeometry::Error &error)
Sent when an error has been found during the validation process.
bool hasWhere() const
true if the location available from
QgsPointXY where() const
The coordinates at which the error is located and should be visualized.
QString what() const
A human readable error message containing details about the error.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool vertexIdFromVertexNr(int number, QgsVertexId &id) const
Calculates the vertex ID from a vertex number.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
QgsGeometry convertToCurves(double distanceTolerance=1e-8, double angleTolerance=1e-8) const
Attempts to convert a non-curved geometry into a curved geometry type (e.g.
static void convertPointList(const QVector< QgsPointXY > &input, QgsPointSequence &output)
Upgrades a point list from QgsPointXY to QgsPoint.
ValidationMethod
Available methods for validating geometries.
@ ValidatorQgisInternal
Use internal QgsGeometryValidator method.
@ ValidatorGeos
Use GEOS validation methods.
Line string geometry type, with support for z-dimension and m-values.
double length() const override SIP_HOLDGIL
Returns the planar, 2-dimensional length of the geometry.
void close()
Closes the line string by appending the first point to the end of the line, if it is not already clos...
Extension of QgsTracer that provides extra functionality:
QAction * actionEnableTracing() const
Access to action that user may use to toggle tracing on/off. May be nullptr if no action was associat...
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
QAction * actionEnableSnapping() const
Access to action that user may use to toggle snapping on/off.
Map canvas is a class for displaying all GIS data types on a canvas.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
bool isSnapped() const
Returns true if there is a snapped point cached.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
QgsPointLocator::Match mapPointMatch() const
Returns the matching data from the most recently snapped point.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
A class to represent a 2D point.
Point geometry type, with support for z-dimension and m-values.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
bool dropMValue() override
Drops any measure values which exist in the geometry.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
void setZ(double z) SIP_HOLDGIL
Sets the point's z-coordinate.
QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
void setExteriorRing(QgsCurve *ring) override
Sets the exterior ring of the polygon.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void snappingConfigChanged(const QgsSnappingConfig &config)
Emitted whenever the configuration for snapping has changed.
A class for drawing transient features (e.g.
void movePoint(const QgsPointXY &p, int geometryIndex=0, int ringIndex=0)
Moves the rubber band point specified by index.
const QgsPointXY * getPoint(int i, int j=0, int ringIndex=0) const
Returns a vertex.
void addGeometry(const QgsGeometry &geometry, QgsVectorLayer *layer, bool doUpdate=true)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Class that shows snapping marker on map canvas for the current snapping match.
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
bool isPointSnapped(const QgsPointXY &pt)
Find out whether the point is snapped to a vertex or edge (i.e. it can be used for tracing start/stop...
QVector< QgsPointXY > findShortestPath(const QgsPointXY &p1, const QgsPointXY &p2, PathError *error=nullptr)
Given two points, find the shortest path and return points on the way.
PathError
Possible errors that may happen when calling findShortestPath()
@ ErrTooManyFeatures
Max feature count threshold was reached while reading features.
bool init()
Build the internal data structures.
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
bool changeGeometry(QgsFeatureId fid, QgsGeometry &geometry, bool skipDefaultValue=false)
Changes a feature's geometry within the layer's edit buffer (but does not immediately commit the chan...
A class for marking vertices of features using e.g.
void setPenWidth(int width)
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
void setIconType(int iconType)
void setColor(const QColor &color)
Sets the stroke color for the marker.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Type
The WKB type describes the number of dimensions a geometry has.
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QVector< QgsPoint > QgsPointSequence
#define QgsDebugMsgLevel(str, level)
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
bool hasEdge() const
Returns true if the Match is an edge.
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
bool hasVertex() const
Returns true if the Match is a vertex.
Setting options for loading vector layers.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
bool loadDefaultStyle
Set to true if the default layer style should be loaded.
Utility class for identifying a unique vertex within a geometry.