QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
45 , mCaptureMode( mode )
46 , mCaptureModeFromLayer( mode == CaptureNone )
53 this, &QgsMapToolCapture::currentLayerChanged );
57 mExtraSnapLayer =
new QgsVectorLayer( QStringLiteral(
"LineString?crs=" ), QStringLiteral(
"extra snap" ), QStringLiteral(
"memory" ), layerOptions );
61 mExtraSnapFeatureId = f.
id();
64 this, &QgsMapToolCapture::updateExtraSnapLayer );
75 mValidator->deleteLater();
79 mExtraSnapLayer->deleteLater();
80 mExtraSnapLayer =
nullptr;
90 if ( mTempRubberBand )
91 mTempRubberBand->show();
99 if ( mTempRubberBand )
100 mTempRubberBand->hide();
108 void QgsMapToolCapture::currentLayerChanged(
QgsMapLayer *layer )
110 if ( !mCaptureModeFromLayer )
137 if ( mTempRubberBand )
144 bool QgsMapToolCapture::tracingEnabled()
152 QgsPointXY QgsMapToolCapture::tracingStartPoint()
157 return mTracingStartPoint;
159 return lastCapturedMapPoint();
185 mTempRubberBand->addPoint( lastCapturedMapPoint() );
191 QgsPoint lastPoint = lastCapturedMapPoint();
193 if ( lastPointXY == pt0 &&
points[0] != lastPointXY )
209 for (
int i = 1; i <
points.count(); ++i )
220 bool QgsMapToolCapture::tracingAddVertex(
const QgsPointXY &point )
226 if ( mTempRubberBand->pointsCount() == 0 )
228 if ( !tracer->
init() )
238 mTracingStartPoint = point;
255 for (
int i = 0; i <
points.count(); ++i )
264 mCaptureCurve.
moveVertex( lastVertexId, layerPoints.first() );
265 mSnappingMatches.removeLast();
268 int pointBefore = mCaptureCurve.
numPoints();
275 if ( settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve" ),
false ).toBool() )
279 if (
capabilities().testFlag( QgsMapToolCapture::Capability::SupportsCurves ) && vlayer->
dataProvider()->
capabilities().testFlag( QgsVectorDataProvider::Capability::CircularGeometries ) )
283 settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve_angle_tolerance" ), 1e-6 ).toDouble(),
284 settings.
value( QStringLiteral(
"/qgis/digitizing/convert_to_curve_distance_tolerance" ), 1e-6 ).toDouble()
286 mCaptureCurve = *qgsgeometry_cast<QgsCompoundCurve *>( curved.
constGet() );
291 int pointAfter = mCaptureCurve.
numPoints();
292 for ( ; pointBefore < pointAfter; ++pointBefore )
304 QgsMapToolCaptureRubberBand *QgsMapToolCapture::createCurveRubberBand()
const
307 QgsMapToolCaptureRubberBand *rb =
new QgsMapToolCaptureRubberBand(
mCanvas );
311 double alphaScale = settings.
value( QStringLiteral(
"qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
312 color.setAlphaF( color.alphaF() * alphaScale );
313 rb->setLineStyle( Qt::DotLine );
314 rb->setStrokeColor( color );
317 rb->setFillColor( fillColor );
322 QgsPoint QgsMapToolCapture::firstCapturedMapPoint()
324 return mCaptureFirstPoint;
327 QgsPoint QgsMapToolCapture::lastCapturedMapPoint()
329 return mCaptureLastPoint;
332 void QgsMapToolCapture::resetRubberBand()
350 if ( mTempRubberBand )
351 mTempRubberBand->setStringType( mDigitizingType );
364 if ( mCaptureMode !=
CapturePoint && mTempRubberBand && mCapturing )
366 bool hasTrace =
false;
369 if ( tracingEnabled() && mCaptureCurve.
numPoints() != 0 )
376 mTempRubberBand->curveIsComplete() )
377 mCircularItermediatePoint = mTempRubberBand->pointFromEnd( 1 );
379 !mTempRubberBand->curveIsComplete() )
380 mCircularItermediatePoint =
QgsPoint();
382 hasTrace = tracingMouseMove( e );
388 mTempRubberBand->addPoint( lastCapturedMapPoint() );
389 if ( !mCircularItermediatePoint.
isEmpty() )
391 mTempRubberBand->movePoint( mCircularItermediatePoint );
392 mTempRubberBand->addPoint( mCircularItermediatePoint );
401 const QgsPoint mapPt = lastCapturedMapPoint();
403 if ( mTempRubberBand )
405 mTempRubberBand->movePoint(
mapPoint );
406 mTempRubberBand->movePoint( 0, mapPt );
413 else if ( mTempRubberBand )
414 mTempRubberBand->movePoint(
mapPoint );
425 QgsDebugMsg( QStringLiteral(
"no vector layer" ) );
440 QgsDebugMsg( QStringLiteral(
"transformation to layer coordinate failed" ) );
458 ( sourceLayer->
crs() == vlayer->
crs() ) )
522 QgsDebugMsg( QStringLiteral(
"invalid capture mode" ) );
542 mSnappingMatches.append( match );
546 if ( mCaptureFirstPoint.
isEmpty() )
554 if ( !mTempRubberBand )
556 mTempRubberBand.reset( createCurveRubberBand() );
557 mTempRubberBand->setStringType( mDigitizingType );
561 bool traceCreated =
false;
562 if ( tracingEnabled() )
564 traceCreated = tracingAddVertex(
mapPoint );
569 mTracingStartPoint = traceCreated ? point :
QgsPointXY();
574 mTempRubberBand->movePoint(
mapPoint );
575 if ( mTempRubberBand->curveIsComplete() )
577 const QgsCurve *curve = mTempRubberBand->curve();
583 if ( match.
isValid() && mSnappingMatches.count() > 0 && !mSnappingMatches.last().isValid() )
585 mSnappingMatches.removeLast();
589 mSnappingMatches.removeLast();
590 mSnappingMatches.append( mCircularIntermediateMatch );
592 mSnappingMatches.append( match );
597 else if ( mCaptureCurve.
numPoints() == 0 )
600 mSnappingMatches.append( match );
606 mCircularIntermediateMatch = match;
610 mTempRubberBand->addPoint(
mapPoint );
616 mTempRubberBand->addPoint( lastCapturedMapPoint() );
620 updateExtraSnapLayer();
638 if ( mTempRubberBand )
642 mTempRubberBand->addPoint( endPt );
655 int addedPoint = countAfter - countBefore;
657 updateExtraSnapLayer();
659 for (
int i = 0; i < addedPoint; ++i )
669 mCaptureCurve.
clear();
670 updateExtraSnapLayer();
675 return mSnappingMatches;
682 if ( mTempRubberBand )
684 if (
size() <= 1 && mTempRubberBand->pointsCount() != 0 )
687 QgsPoint lastPoint = mTempRubberBand->lastPoint();
691 mTempRubberBand->removeLastPoint();
692 mTempRubberBand->movePoint( lastPoint );
697 vertexToRemove.
part = 0;
698 vertexToRemove.
ring = 0;
710 int pointsCountBefore = mCaptureCurve.
numPoints();
712 int pointsCountAfter = mCaptureCurve.
numPoints();
713 for ( ; pointsCountAfter < pointsCountBefore; pointsCountAfter++ )
714 if ( !mSnappingMatches.empty() )
715 mSnappingMatches.removeLast();
718 updateExtraSnapLayer();
728 mTempRubberBand->addPoint( lastCapturedMapPoint() );
729 mTempRubberBand->movePoint( lastPoint );
739 if ( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete )
746 else if ( e->key() == Qt::Key_Escape )
771 qDeleteAll( mGeomErrorMarkers );
772 mGeomErrorMarkers.clear();
781 mCaptureCurve.
clear();
782 updateExtraSnapLayer();
783 mSnappingMatches.clear();
785 lCurrentVectorLayer->triggerRepaint();
790 mTempRubberBand.reset();
801 mCaptureCurve.
close();
802 updateExtraSnapLayer();
805 void QgsMapToolCapture::validateGeometry()
808 if ( settings.
value( QStringLiteral(
"qgis/digitizing/validate_geometries" ), 1 ).toInt() == 0 )
813 mValidator->deleteLater();
814 mValidator =
nullptr;
818 while ( !mGeomErrorMarkers.isEmpty() )
820 delete mGeomErrorMarkers.takeFirst();
825 switch ( mCaptureMode )
839 exteriorRing->
close();
850 if ( settings.
value( QStringLiteral(
"qgis/digitizing/validate_geometries" ), 1 ).toInt() == 2 )
871 vm->setToolTip( e.what() );
873 vm->setZValue( vm->zValue() + 1 );
874 mGeomErrorMarkers << vm;
885 QVector<QgsPointXY> pointsXY;
894 mCaptureCurve.
points( pts );
901 mCaptureCurve.
clear();
903 updateExtraSnapLayer();
904 mSnappingMatches.clear();
905 for (
int i = 0; i < line->
length(); ++i )
913 mCaptureCurve.
clear();
915 updateExtraSnapLayer();
916 mSnappingMatches.clear();
917 for (
int i = 0; i < line->
length(); ++i )
981 void QgsMapToolCapture::updateExtraSnapLayer()
991 qgsgeometry_cast<QgsCompoundCurve *>( geom.
get() )->close();
1005 setVertexDrawingEnabled(
false );
1008 QgsCurve *QgsMapToolCaptureRubberBand::curve()
1010 if ( mPoints.empty() )
1013 switch ( mStringType )
1019 if ( mPoints.count() != 3 )
1031 bool QgsMapToolCaptureRubberBand::curveIsComplete()
const
1043 mFirstPolygonPoint = firstPolygonPoint;
1044 setStringType( stringType );
1045 setRubberBandGeometryType( geomType );
1054 void QgsMapToolCaptureRubberBand::addPoint(
const QgsPoint &point,
bool doUpdate )
1056 if ( mPoints.count() == 0 )
1057 mPoints.append( point );
1059 mPoints.append( point );
1065 void QgsMapToolCaptureRubberBand::movePoint(
const QgsPoint &point )
1067 if ( mPoints.count() > 0 )
1068 mPoints.last() = point ;
1073 void QgsMapToolCaptureRubberBand::movePoint(
int index,
const QgsPoint &point )
1075 if ( mPoints.count() > 0 && mPoints.size() > index )
1076 mPoints[index] = point;
1081 int QgsMapToolCaptureRubberBand::pointsCount()
1083 return mPoints.size();
1091 void QgsMapToolCaptureRubberBand::setStringType(
const QgsWkbTypes::Type &type )
1099 mPoints.removeAt( 1 );
1106 QgsPoint QgsMapToolCaptureRubberBand::lastPoint()
const
1108 if ( mPoints.empty() )
1111 return mPoints.last();
1114 QgsPoint QgsMapToolCaptureRubberBand::pointFromEnd(
int posFromEnd )
const
1116 if ( posFromEnd < mPoints.size() )
1117 return mPoints.at( mPoints.size() - 1 - posFromEnd );
1122 void QgsMapToolCaptureRubberBand::removeLastPoint()
1124 if ( mPoints.count() > 1 )
1125 mPoints.removeLast();
1135 void QgsMapToolCaptureRubberBand::updateCurve()
1137 std::unique_ptr<QgsCurve> curve;
1138 switch ( mStringType )
1141 curve.reset( createLinearString() );
1144 curve.reset( createCircularString() );
1154 geom->setExteriorRing( curve.release() );
1155 setGeometry( geom.release() );
1159 setGeometry( curve.release() );
1163 QgsCurve *QgsMapToolCaptureRubberBand::createLinearString()
1169 points.prepend( mFirstPolygonPoint );
1170 curve->setPoints( points );
1173 curve->setPoints( mPoints );
1175 return curve.release();
1178 QgsCurve *QgsMapToolCaptureRubberBand::createCircularString()
1181 curve->setPoints( mPoints );
1186 polygonCurve->addVertex( mFirstPolygonPoint );
1187 if ( !mPoints.empty() )
1188 polygonCurve->addVertex( mPoints.first() );
1189 polygonCurve->addCurve( curve.release() );
1190 return polygonCurve.release();
1193 return curve.release();
Abstract base class for curved geometry type.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
@ ValidatorGeos
Use GEOS validation methods.
QgsCoordinateReferenceSystem crs
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
double length() const override SIP_HOLDGIL
Returns the planar, 2-dimensional length of the geometry.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
void setIconType(int iconType)
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.
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
Point geometry type, with support for z-dimension and m-values.
#define QgsDebugMsgLevel(str, level)
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
bool init()
Build the internal data structures.
A class for drawing transient features (e.g.
int nCurves() const SIP_HOLDGIL
Returns the number of curves in the geometry.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Curve polygon geometry type.
void setExteriorRing(QgsCurve *ring) override
Sets the exterior ring of the polygon.
void addCurve(QgsCurve *c)
Adds a curve to the geometry (takes ownership)
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsPointXY * getPoint(int i, int j=0, int ringIndex=0) const
Returns a vertex.
static QgsProject * instance()
Returns the QgsProject singleton instance.
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.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
This class is a composition of two QSettings instances:
Type
The WKB type describes the number of dimensions a geometry has.
Q_INVOKABLE bool startEditing()
Makes the layer editable.
int numPoints() const override SIP_HOLDGIL
Returns the number of points in the curve.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
Line string geometry type, with support for z-dimension and m-values.
void movePoint(const QgsPointXY &p, int geometryIndex=0, int ringIndex=0)
Moves the rubber band point specified by index.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
bool hasVertex() const
Returns true if the Match is a vertex.
Class that shows snapping marker on map canvas for the current snapping match.
QAction * actionEnableTracing() const
Access to action that user may use to toggle tracing on/off. May be nullptr if no action was associat...
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
QgsPoint startPoint() const override SIP_HOLDGIL
Returns the starting point of the curve.
bool dropMValue() override
Drops any measure values which exist in the geometry.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
T * release()
Clears the pointer and returns it.
A class for marking vertices of features using e.g.
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...
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
bool hasEdge() const
Returns true if the Match is an edge.
This class wraps a request for features to a vector layer (or directly its vector data provider).
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
QgsPointLocator::Match mapPointMatch() const
Returns the matching data from the most recently snapped point.
Custom exception class for Coordinate Reference System related exceptions.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
Circular string geometry type.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
QgsPoint endPoint() const override SIP_HOLDGIL
Returns the end point of the curve.
void addVertex(const QgsPoint &pt)
Adds a vertex to the end of the geometry.
Extension of QgsTracer that provides extra functionality:
@ ErrTooManyFeatures
Max feature count threshold was reached while reading features.
@ ValidatorQgisInternal
Use internal QgsGeometryValidator method.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
void setPenWidth(int width)
void points(QgsPointSequence &pts) const override
Returns a list of points within the curve.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
void setCenter(const QgsPointXY &point)
QAction * actionEnableSnapping() const
Access to action that user may use to toggle snapping on/off.
void setColor(const QColor &color)
Sets the stroke color for the marker.
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
virtual void setGeometry(QgsAbstractGeometry *geom)
Sets geometry (takes ownership). Geometry is expected to be in map coordinates.
Abstract base class for all geometries.
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
void clear() override
Clears the geometry, ie reset it to a null geometry.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
A class to represent a 2D point.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
A rubberband class for QgsAbstractGeometry (considering curved geometries).
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.
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
Setting options for loading vector layers.
void setGeometryType(const QgsWkbTypes::GeometryType &geometryType)
Sets which geometry is handled by the rubber band, polygon or line.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
QgsCompoundCurve * clone() const override
Clones the geometry by performing a deep copy.
static void convertPointList(const QVector< QgsPointXY > &input, QgsPointSequence &output)
Upgrades a point list from QgsPointXY to QgsPoint.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
bool nextFeature(QgsFeature &f)
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
QVector< QgsPoint > QgsPointSequence
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
A geometry is the spatial representation of a feature.
void close()
Closes the line string by appending the first point to the end of the line, if it is not already clos...
Represents a vector layer which manages a vector based data sets.
Base class for all map layer types.
void addGeometry(const QgsGeometry &geometry, QgsVectorLayer *layer)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
Utility class for identifying a unique vertex within a geometry.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
bool convertTo(QgsWkbTypes::Type type) override
Converts the geometry to a specified type.
ValidationMethod
Available methods for validating geometries.
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
bool vertexIdFromVertexNr(int number, QgsVertexId &id) const
Calculates the vertex ID from a vertex number.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
PathError
Possible errors that may happen when calling findShortestPath()
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
void setZ(double z) SIP_HOLDGIL
Sets the point's z-coordinate.
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...
void snappingConfigChanged(const QgsSnappingConfig &config)
Emitted whenever the configuration for snapping has changed.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
void errorFound(const QgsGeometry::Error &error)
Sent when an error has been found during the validation process.
Compound curve geometry type.
bool isSnapped() const
Returns true if there is a snapped point cached.