Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[convert to curve] wip
  • Loading branch information
olivierdalang authored and nyalldawson committed Jun 18, 2021
1 parent c7e75a9 commit c946f5a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
49 changes: 28 additions & 21 deletions src/app/vertextool/qgsvertextool.cpp
Expand Up @@ -2562,12 +2562,15 @@ void QgsVertexTool::deleteVertex()

void QgsVertexTool::toggleVertexCurve()
{
std::cout << "test";
QgsMessageLog::logMessage("test", "DEBUG");

Vertex toConvert = Vertex(nullptr, -1, -1);
if ( mSelectedVertices.size() == 1 )
{
toConvert = mSelectedVertices.first();
}
else if( mDraggingVertexType == AddingVertex )
else if( mDraggingVertexType == AddingVertex || mDraggingVertexType == MovingVertex )
{
toConvert = *mDraggingVertex;
}
Expand Down Expand Up @@ -2605,11 +2608,12 @@ void QgsVertexTool::toggleVertexCurve()
// return;
// }

QgsCompoundCurve *compoundCurve = dynamic_cast<QgsCompoundCurve *>( geom.get() );
if( ! compoundCurve ){
QgsMessageLog::logMessage("Only compound curves supported (for now)", "DEBUG");
return;
}
// QgsCompoundCurve *compoundCurve = dynamic_cast<QgsCompoundCurve *>( geom.get() );
// if( ! compoundCurve ){
// compoundCurve->convertTo(QgsWkbTypes::CompoundCurve);
// QgsMessageLog::logMessage("Only compound curves supported (for now)", "DEBUG");
// return;
// }

// const QgsCurve *c0 = compoundCurve->curveAt(0);
// QgsMessageLog::logMessage("Curve 0 : " + c0->asWkt(), "DEBUG");
Expand Down Expand Up @@ -2645,42 +2649,45 @@ void QgsVertexTool::toggleVertexCurve()


QgsAbstractGeometry *geomTmp = geom.constGet()->clone();
QgsCompoundCurve *compoundCurveCopy = compoundCurve->clone();

if( ! geomTmp->convertTo(QgsWkbTypes::CompoundCurve) ){
QgsMessageLog::logMessage("Could not convert "+geomTmp->wktTypeStr() + " to CompoundCurve", "DEBUG");
return;
}
QgsCompoundCurve *compoundCurveCopy = (QgsCompoundCurve*)geomTmp;


bool success = false;
if ( vId.type == QgsVertexId::CurveVertex ) {
layer->beginEditCommand( tr( "Converting vertex to linear" ) );
// layer->deleteVertex( fId, vNr );
// layer->insertVertex( vPt, fId, vNr );
// vId.type = QgsVertexId::CurveVertex;
// feature.setGeometry( QgsGeometry(compoundCurveCopy ));
compoundCurveCopy->convertVertex( vId, QgsVertexId::SegmentVertex );
success = compoundCurveCopy->convertVertex( vId, QgsVertexId::SegmentVertex );

} else {
layer->beginEditCommand( tr( "Converting vertex to curve" ) );
// layer->deleteVertex( fId, vNr );
// layer->insertVertex( vPt, fId, vNr );
// vId.type = QgsVertexId::SegmentVertex;
// feature.setGeometry( QgsGeometry(compoundCurveCopy ));
compoundCurveCopy->convertVertex( vId, QgsVertexId::CurveVertex );
success = compoundCurveCopy->convertVertex( vId, QgsVertexId::CurveVertex );
}

geom.set( compoundCurveCopy );
layer->changeGeometry( fId, geom );

// if ( success )
// {
if ( success )
{
QgsMessageLog::logMessage("Should be OK", "DEBUG");
geom.set( compoundCurveCopy );
layer->changeGeometry( fId, geom );
layer->endEditCommand();
layer->triggerRepaint();
// }
// else
// {
// QgsMessageLog::logMessage("Has failed :-/", "DEBUG");
// layer->destroyEditCommand();
// }

}
else
{
QgsMessageLog::logMessage("Has failed :-/", "DEBUG");
layer->destroyEditCommand();
}

if ( mVertexEditor && mLockedFeature )
mVertexEditor->updateEditor( mLockedFeature.get() );
Expand Down
66 changes: 45 additions & 21 deletions src/core/geometry/qgscompoundcurve.cpp
Expand Up @@ -936,40 +936,64 @@ bool QgsCompoundCurve::convertVertex( QgsVertexId position, QgsVertexId::VertexT

QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );


QgsMessageLog::logMessage("Starting convertVertex", "DEBUG");

for ( auto it = curveIds.constBegin(); it != curveIds.constEnd(); ++it )
if(curveIds.length() != 1)
{
QgsMessageLog::logMessage("Treating curve "+QString::number(it->first), "DEBUG");
QgsMessageLog::logMessage("There is not exactly one subcurve at this position. " + QString::number(curveIds.length()), "DEBUG");
return false;
}

int curveId = curveIds[0].first;
QgsVertexId subVertexId = curveIds[0].second;
QgsCurve *curve = mCurves[curveId];

if(subVertexId.vertex == 0 || subVertexId.vertex == curve->numPoints()-1)
{
QgsMessageLog::logMessage("Cannot convert first or last point of a sub-curve. Merge the subcurves first", "DEBUG");
return false;
}

const int curveId = it->first;
const QgsCurve *curve = curveAt(curveId);
const QgsVertexId subVertexId = it->second;


if( const QgsCircularString *circularString = dynamic_cast<const QgsCircularString *>( curve ) )
{
// If it's a circular string, we convert to LineString

// If it's a circular string, we convert to LineString
const QgsCircularString *circularString = dynamic_cast<const QgsCircularString *>( curve );
if( circularString ){
QgsMessageLog::logMessage("Dealing with CircularString", "DEBUG");
// remove the existing CircularString

QgsMessageLog::logMessage("A. remove curve" + QString::number(curveId), "DEBUG");
// We remove the existing CircularString and create a LineString instead
removeCurve(curveId);
QVector<QgsPoint> points;
QgsPointSequence points;
circularString->points(points);
removeCurve(curveId);
QgsLineString *newLineString = new QgsLineString(points);
mCurves.insert(it->first, newLineString);

mCurves.insert(curveId, newLineString);
}

const QgsLineString *lineString = dynamic_cast<const QgsLineString *>( curve );
if( lineString ){
QgsMessageLog::logMessage("TODO : LineString not treated yet", "DEBUG");
// TODO
else if( const QgsLineString *lineString = dynamic_cast<const QgsLineString *>( curve ) )
{
// If it's a linestring, we split and insert a curve

QgsPointSequence points;
lineString->points(points);

// QgsMessageLog::logMessage("TODO : LineString not treated yet", "DEBUG");
QgsPointSequence partA = points.mid(0, subVertexId.vertex);
QgsPointSequence partB = QgsPointSequence() << points[subVertexId.vertex-1] << points[subVertexId.vertex] << points[subVertexId.vertex+1];
QgsPointSequence partC = points.mid(subVertexId.vertex+1);

QgsLineString *curveA = new QgsLineString();
curveA->setPoints(partA);
QgsCircularString *curveB = new QgsCircularString();
curveB->setPoints(partB);
QgsLineString *curveC = new QgsLineString();
curveC->setPoints(partC);

removeCurve(curveId);
mCurves.insert(curveId, curveC);
mCurves.insert(curveId, curveB);
mCurves.insert(curveId, curveA);
}

}

// We merge consecutive LineStrings
// TODO ? : move this to a new mergeConsecutiveLineStrings() method;
Expand Down
8 changes: 5 additions & 3 deletions test_digicurve.py
Expand Up @@ -5,18 +5,20 @@


f1 = QgsFeature()
f1.setGeometry(QgsGeometry.fromWkt("LINESTRING(0 0, 5 5, 10 0, 15 5, 20 0)"))
f1.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE((0 0, 5 5, 10 0, 15 5, 20 0))"))
f2 = QgsFeature()
f2.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE((0 10, 5 15), CIRCULARSTRING(5 15, 10 10, 15 15), (15 15, 20 10))"))
f3 = QgsFeature()
f3.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE(CIRCULARSTRING(0 20, 5 25, 10 20, 15 25, 20 20))"))
f3.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE(CIRCULARSTRING(0 20, 5 25, 10 20, 15 25, 20 20, 25 25, 30 20))"))
f4 = QgsFeature()
f4.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE(CIRCULARSTRING(0 30, 5 35, 10 30), (10 30, 15 35, 20 30))"))
f5 = QgsFeature()
f5.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE((0 50, 5 55), (5 55, 10 50, 15 55, 20 50))"))
f6 = QgsFeature()
f6.setGeometry(QgsGeometry.fromWkt("COMPOUNDCURVE(CIRCULARSTRING(0 60, 5 65, 10 60), (10 60, 15 65), CIRCULARSTRING(15 65, 20 60, 25 65))"))
l.dataProvider().addFeatures([f1, f2, f3, f4, f5, f6])
f7 = QgsFeature()
f7.setGeometry(QgsGeometry.fromWkt("LINESTRING(0 70, 5 75, 10 70, 15 75, 20 70)"))
l.dataProvider().addFeatures([f1, f2, f3, f4, f5, f6, f7])


for f in l.getFeatures():
Expand Down

0 comments on commit c946f5a

Please sign in to comment.