Hi all,<br><br>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.<br>So, I do this as follow, for example with a polyline<br>
<br>void QgsCGALArr2PluginGui::addInArrangement( const QgsPolyline &line )<br>{<br> if ( !m_mapCanvasSnapper )<br> m_mapCanvasSnapper = new QgsMapCanvasSnapper( m_mapCanvas );<br> else<br> m_mapCanvasSnapper->setMapCanvas( m_mapCanvas );<br>
// Differenciation des snaps sur sommets et sur segments<br> QMultiMap < double, QgsSnappingResult > snappingVerticesResults, snappingSegmentsResults;<br> QMultiMap < double, QgsSnappingResult > globalSnappingVerticesResults, globalSnappingSegmentsResults;<br>
// On commence par checker que des sommets de la polyligne n'existent pas deja<br> QString text(""), numericString;<br> for (int i = 0; i < line.size(); ++i)<br> {<br> QgsPoint pt = <a href="http://line.at">line.at</a>(i);<br>
// On teste le snap sur chacun de layers<br> for (unsigned int j = 0;j<m_mapCanvas->layerCount();++j)<br> {<br> QgsMapLayer *currentLayer = m_mapCanvas->layer(j);<br> QgsVectorLayer *vector = dynamic_cast<QgsVectorLayer *>( currentLayer );<br>
if ( vector )<br> {<br> snappingVerticesResults.clear();<br> snappingSegmentsResults.clear();<br> vector->snapWithContext( pt , 5. , snappingVerticesResults , QgsSnapper::SnapToVertex );<br>
vector->snapWithContext( pt , 5. , snappingSegmentsResults , QgsSnapper::SnapToSegment );<br> // On ajoute les resultats du layer courant aux resultats globaux<br> for ( QMultiMap < double, QgsSnappingResult >::iterator it=snappingVerticesResults.begin();it!=snappingVerticesResults.end();++it)<br>
if ( pt != it.value().snappedVertex )<br> globalSnappingVerticesResults.insertMulti( it.key() , it.value() );<br> for ( QMultiMap < double, QgsSnappingResult >::iterator it=snappingSegmentsResults.begin();it!=snappingSegmentsResults.end();++it)<br>
globalSnappingSegmentsResults.insertMulti( it.key() , it.value() );<br> }<br> }<br> }<br> for ( QMultiMap < double, QgsSnappingResult >::iterator it=globalSnappingVerticesResults.begin();it!=globalSnappingVerticesResults.end();++it)<br>
text += numericString.setNum(it.key()) + " " + it.value().snappedVertex.toString() + "\n";<br> text += "globalSnappingVerticesResults = " + numericString.setNum( globalSnappingVerticesResults.size() ) + "\n";<br>
text += "globalSnappingSegmentsResults = " + numericString.setNum( globalSnappingSegmentsResults.size() ) + "\n";<br><br> // ...<br>}<br><br>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.<br>
<br>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 ?<br>
<br>Best regards,<br><br>Olivier<br><br>PS: note that I also tried m_mapCanvasSnapper->snapToCurrentLayer(...) and m_mapCanvasSnapper->snapToBackgroundLayers(...) without any success.<br clear="all"><br>-- <br>Dr. Olivier Tournaire<br>
MATIS - Institut Géographique National<br>73, Ave de Paris<br>94165 St Mandé cedex, France<br><br>tel: (+33) 1 43 98 80 00 - 71 25<br>fax: (+33) 1 43 98 85 81<br>