[Qgis-developer] Snapping problem

Olivier Tournaire olitour at gmail.com
Sun Jan 11 07:52:17 EST 2009


Hi all,

I am currently trying to use the snapping capabilities of QGIS, and I face
some problems. When I edit a new geometry, I would like to know the vertices
which have been snapped.
So, I do this as follow, for example with a polyline

void QgsCGALArr2PluginGui::addInArrangement( const QgsPolyline &line )
{
    if ( !m_mapCanvasSnapper )
        m_mapCanvasSnapper = new QgsMapCanvasSnapper( m_mapCanvas );
    else
        m_mapCanvasSnapper->setMapCanvas( m_mapCanvas );
    // Differenciation des snaps sur sommets et sur segments
    QMultiMap < double, QgsSnappingResult > snappingVerticesResults,
snappingSegmentsResults;
    QMultiMap < double, QgsSnappingResult > globalSnappingVerticesResults,
globalSnappingSegmentsResults;
    // On commence par checker que des sommets de la polyligne n'existent
pas deja
    QString text(""), numericString;
    for (int i = 0; i < line.size(); ++i)
    {
        QgsPoint pt = line.at(i);
        // On teste le snap sur chacun de layers
          for (unsigned int j = 0;j<m_mapCanvas->layerCount();++j)
          {
              QgsMapLayer *currentLayer = m_mapCanvas->layer(j);
            QgsVectorLayer *vector = dynamic_cast<QgsVectorLayer *>(
currentLayer );
            if ( vector )
            {
                snappingVerticesResults.clear();
                snappingSegmentsResults.clear();
                vector->snapWithContext( pt , 5. , snappingVerticesResults ,
QgsSnapper::SnapToVertex );
                vector->snapWithContext( pt , 5. , snappingSegmentsResults ,
QgsSnapper::SnapToSegment );
                // On ajoute les resultats du layer courant aux resultats
globaux
                for ( QMultiMap < double, QgsSnappingResult >::iterator
it=snappingVerticesResults.begin();it!=snappingVerticesResults.end();++it)
                    if ( pt != it.value().snappedVertex )
                        globalSnappingVerticesResults.insertMulti( it.key()
, it.value() );
                for ( QMultiMap < double, QgsSnappingResult >::iterator
it=snappingSegmentsResults.begin();it!=snappingSegmentsResults.end();++it)
                    globalSnappingSegmentsResults.insertMulti( it.key() ,
it.value() );
            }
          }
    }
    for ( QMultiMap < double, QgsSnappingResult >::iterator
it=globalSnappingVerticesResults.begin();it!=globalSnappingVerticesResults.end();++it)
        text += numericString.setNum(it.key()) + "   " +
it.value().snappedVertex.toString() + "\n";
    text += "globalSnappingVerticesResults = " + numericString.setNum(
globalSnappingVerticesResults.size() ) + "\n";
    text += "globalSnappingSegmentsResults = " + numericString.setNum(
globalSnappingSegmentsResults.size() ) + "\n";

    // ...
}

In my project, all layers are "snappable" to vertices and segemnts, with a
tolerance of 5, and the units are in meter. That is why my toleance in the
snapWithContext is set to 5. The problem is that when I edit the geometry,
the snapping works well (the mouse is attracted when close to a vertex or
segment), but, the above code shows that snappingVerticesResults is empty.
So I do not understand why I cannot retrieve the snapped vertices.

In fact, I simply want to maintain a list of vertices, update it each time a
new geometry is added, but, be aware that if the new geometry has a common
vertex with an already existing geometry, do not add this vertex to the list
of vertices. Could you help ?

Best regards,

Olivier

PS: note that I also tried m_mapCanvasSnapper->snapToCurrentLayer(...) and
m_mapCanvasSnapper->snapToBackgroundLayers(...) without any success.

-- 
Dr. Olivier Tournaire
MATIS - Institut Géographique National
73, Ave de Paris
94165 St Mandé cedex, France

tel: (+33) 1 43 98 80 00 - 71 25
fax: (+33) 1 43 98 85 81
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20090111/38421767/attachment.html


More information about the Qgis-developer mailing list