Skip to content

Commit

Permalink
Add only topo points to editable layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonalai committed Jan 13, 2025
1 parent a81cbb7 commit 880505a
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions src/app/qgsmaptooladdfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include "qgisapp.h"
#include "qgsexpressioncontextutils.h"
#include "qgsrubberband.h"
#include "qgsmultipoint.h"

#include <QSettings>
#include <algorithm>

QgsMapToolAddFeature::QgsMapToolAddFeature( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode )
: QgsMapToolDigitizeFeature( canvas, cadDockWidget, mode )
Expand Down Expand Up @@ -143,45 +145,72 @@ void QgsMapToolAddFeature::featureDigitized( const QgsFeature &feature )
}
if ( topologicalEditing )
{
const QList<QgsMapLayer *> layers = canvas()->layers( true );
QList<QgsPointLocator::Match> sm = snappingMatches();
sm.erase( std::remove_if( sm.begin(), sm.end(), []( const QgsPointLocator::Match &match ) {
return match.layer() == nullptr;
} ),
sm.end() );

for ( QgsMapLayer *layer : layers )
if ( sm.size() )
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
QgsMultiPoint *snapPoints = new QgsMultiPoint();

if ( !vectorLayer || !vectorLayer->isEditable() )
continue;

if ( !( vectorLayer->geometryType() == Qgis::GeometryType::Polygon || vectorLayer->geometryType() == Qgis::GeometryType::Line ) )
continue;
for ( int i = 0; i < sm.size(); ++i )
{
snapPoints->addGeometry( feature.geometry().vertexAt( i ).clone() );
}

vectorLayer->beginEditCommand( tr( "Topological points added by 'Add Feature'" ) );
QgsPointSequence topoPoints;
for ( int j = 0; j < snapPoints->partCount(); ++j )
{
topoPoints.append( *snapPoints->pointN( j ) );
}

int res = 2;
if ( vectorLayer->crs() != vlayer->crs() )
const QList<QgsMapLayer *> layers = canvas()->layers( true );
for ( QgsMapLayer *layer : layers )
{
QgsGeometry transformedGeom = feature.geometry();
try
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vectorLayer || !vectorLayer->isEditable() )
continue;

if ( !( vectorLayer->geometryType() == Qgis::GeometryType::Polygon || vectorLayer->geometryType() == Qgis::GeometryType::Line ) )
continue;

vectorLayer->beginEditCommand( tr( "Topological points added by 'Add Feature'" ) );

int res = 2;
if ( vectorLayer->crs() != vlayer->crs() )
{
// transform digitized geometry from vlayer crs to vectorLayer crs and add topological points
transformedGeom.transform( QgsCoordinateTransform( vlayer->crs(), vectorLayer->crs(), vectorLayer->transformContext() ) );
res = vectorLayer->addTopologicalPoints( transformedGeom );
try
{
QgsMultiPoint *transformedGeom = snapPoints->clone();
// transform digitized geometry from vlayer crs to vectorLayer crs and add topological points
transformedGeom->transform( QgsCoordinateTransform( vlayer->crs(), vectorLayer->crs(), vectorLayer->transformContext() ) );

QgsPointSequence trarnsformedtopoPoints;
for ( int j = 0; j < transformedGeom->partCount(); ++j )
{
trarnsformedtopoPoints.append( *transformedGeom->pointN( j ) );
}

res = vectorLayer->addTopologicalPoints( trarnsformedtopoPoints );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
QgsDebugError( QStringLiteral( "transformation to vectorLayer coordinate failed" ) );
}
}
catch ( QgsCsException &cse )
else
{
Q_UNUSED( cse )
QgsDebugError( QStringLiteral( "transformation to vectorLayer coordinate failed" ) );
res = vectorLayer->addTopologicalPoints( topoPoints );
}
}
else
{
res = vectorLayer->addTopologicalPoints( feature.geometry() );
}

if ( res == 0 ) // i.e. if any points were added
vectorLayer->endEditCommand();
else
vectorLayer->destroyEditCommand();
if ( res == 0 ) // i.e. if points were added
vectorLayer->endEditCommand();
else
vectorLayer->destroyEditCommand();
}
}
}
}
Expand Down

0 comments on commit 880505a

Please sign in to comment.