Skip to content

Commit f6eea72

Browse files
author
stopa85
committed
add topology tolerance
git-svn-id: http://svn.osgeo.org/qgis/trunk@15239 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b642e80 commit f6eea72

File tree

8 files changed

+78
-15
lines changed

8 files changed

+78
-15
lines changed

src/plugins/roadgraph/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ SET (VRP_RCCS roadgraph.qrc)
3030
INCLUDE_DIRECTORIES(
3131
${CMAKE_CURRENT_BINARY_DIR}
3232
${GEOS_INCLUDE_DIR}
33-
../../core
33+
../../core
34+
../../core/spatialindex/
3435
../../gui
3536
..
3637
)

src/plugins/roadgraph/roadgraphplugin.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ RoadGraphPlugin::RoadGraphPlugin( QgisInterface * theQgisInterface ):
7777
mSettings = new RgLineVectorLayerSettings();
7878
mTimeUnitName = "h";
7979
mDistanceUnitName = "km";
80+
mTopologyToleranceFactor = 0.0;
8081
}
8182

8283
RoadGraphPlugin::~RoadGraphPlugin()
@@ -174,17 +175,19 @@ void RoadGraphPlugin::property()
174175

175176
dlg.setTimeUnitName( mTimeUnitName );
176177
dlg.setDistanceUnitName( mDistanceUnitName );
178+
dlg.setTopologyTolerance( mTopologyToleranceFactor );
177179

178180
if ( !dlg.exec() )
179181
return;
180182

181183
mTimeUnitName = dlg.timeUnitName();
182184
mDistanceUnitName = dlg.distanceUnitName();
185+
mTopologyToleranceFactor = dlg.topologyTolerance();
183186

184187
mSettings->write( QgsProject::instance() );
185188
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginTimeUnit", mTimeUnitName );
186189
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginDistanceUnit", mDistanceUnitName );
187-
190+
QgsProject::instance()->writeEntry( "roadgraphplugin", "/topologyToleranceFactor", mTopologyToleranceFactor );
188191
setGuiElementsToDefault();
189192
} //RoadGraphPlugin::property()
190193

@@ -244,6 +247,8 @@ void RoadGraphPlugin::projectRead()
244247
mSettings->read( QgsProject::instance() );
245248
mTimeUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginTimeUnit", "h" );
246249
mDistanceUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginDistanceUnit", "km" );
250+
mTopologyToleranceFactor =
251+
QgsProject::instance()->readDoubleEntry( "roadgraphplugin", "/topologyToleranceFactor", 0.0 );
247252
setGuiElementsToDefault();
248253
}// RoadGraphplguin::projectRead()
249254

@@ -344,6 +349,10 @@ QString RoadGraphPlugin::distanceUnitName()
344349
return mDistanceUnitName;
345350
}
346351

352+
double RoadGraphPlugin::topologyToleranceFactor()
353+
{
354+
return mTopologyToleranceFactor;
355+
}
347356
//////////////////////////////////////////////////////////////////////////
348357
//
349358
//

src/plugins/roadgraph/roadgraphplugin.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class RoadGraphPlugin: public QObject, public QgisPlugin
7171
*/
7272
QString distanceUnitName();
7373

74+
/**
75+
* get topology tolerance factor
76+
*/
77+
double topologyToleranceFactor();
78+
7479
public slots:
7580
void render( QPainter *painter );
7681
//! init the gui
@@ -155,6 +160,12 @@ class RoadGraphPlugin: public QObject, public QgisPlugin
155160
* distance unit for results presentation
156161
*/
157162
QString mDistanceUnitName;
163+
164+
/**
165+
* topology tolerance factor
166+
*/
167+
double mTopologyToleranceFactor;
168+
158169
private:
159170
static const int mArrowSize = 5;
160171
};

src/plugins/roadgraph/settingsdlg.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <QVBoxLayout>
1919
#include <qdialogbuttonbox.h>
2020
#include <qmessagebox.h>
21-
21+
#include <QDoubleSpinBox>
2222

2323
// Qgis includes
2424
#include "settings.h"
@@ -47,6 +47,15 @@ RgSettingsDlg::RgSettingsDlg( RgSettings *settings, QWidget* parent, Qt::WFlags
4747
h->addWidget( mcbPluginsDistanceUnit );
4848
v->addLayout( h );
4949

50+
h = new QHBoxLayout();
51+
l = new QLabel( tr( "Topology tolerance" ), this );
52+
h->addWidget( l );
53+
msbTopologyTolerance = new QDoubleSpinBox( this );
54+
msbTopologyTolerance->setMinimum( 0.0 );
55+
msbTopologyTolerance->setDecimals( 5 );
56+
h->addWidget( msbTopologyTolerance );
57+
v->addLayout( h );
58+
5059
/*
5160
h = new QHBoxLayout();
5261
l = new QLabel( tr("Select graph source:"), this);
@@ -118,3 +127,13 @@ void RgSettingsDlg::setDistanceUnitName( const QString& name )
118127
mcbPluginsDistanceUnit->setCurrentIndex( i );
119128
}
120129
}
130+
131+
void RgSettingsDlg::setTopologyTolerance( double f )
132+
{
133+
msbTopologyTolerance->setValue( f );
134+
}
135+
136+
double RgSettingsDlg::topologyTolerance()
137+
{
138+
return msbTopologyTolerance->value();
139+
}

src/plugins/roadgraph/settingsdlg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// forward declaration QT-classes
2121
class QComboBox;
22+
class QDoubleSpinBox;
2223

2324
// forward declaration Qgis-classes
2425

@@ -47,6 +48,9 @@ class RgSettingsDlg : public QDialog
4748

4849
void setDistanceUnitName( const QString& );
4950

51+
void setTopologyTolerance( double f );
52+
53+
double topologyTolerance();
5054
private:
5155
static const int context_id = 0;
5256

@@ -75,5 +79,10 @@ class RgSettingsDlg : public QDialog
7579
* plugin time unit
7680
*/
7781
QComboBox *mcbPluginsTimeUnit;
82+
83+
/**
84+
* topology tolerance factor
85+
*/
86+
QDoubleSpinBox *msbTopologyTolerance;
7887
};
7988
#endif

src/plugins/roadgraph/shortestpathwidget.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ bool RgShortestPathWidget::getPath( AdjacencyMatrix& matrix, QgsPoint& p1, QgsPo
223223
{
224224
if ( mFrontPointLineEdit->text().isNull() || mBackPointLineEdit->text().isNull() )
225225
return false;
226-
RgSimpleGraphBuilder builder( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationSrs() );
226+
RgSimpleGraphBuilder builder( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationSrs(),
227+
mPlugin->topologyToleranceFactor() );
227228
{
228229
const RgGraphDirector *director = mPlugin->director();
229230
if ( director == NULL )

src/plugins/roadgraph/simplegraphbuilder.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "utils.h"
1919

2020
// Qgis includes
21+
#include <qgsfeature.h>
22+
#include <qgsgeometry.h>
2123

2224
RgSimpleGraphBuilder::RgSimpleGraphBuilder( const QgsCoordinateReferenceSystem& crs, double topologyTolerance ) :
2325
RgGraphBuilder( crs, topologyTolerance )
@@ -26,21 +28,27 @@ RgSimpleGraphBuilder::RgSimpleGraphBuilder( const QgsCoordinateReferenceSystem&
2628

2729
QgsPoint RgSimpleGraphBuilder::addVertex( const QgsPoint& pt )
2830
{
29-
// I cann't use QgsSpatialIndex in this time.
30-
// QgsSpatialIndex::nearestNeighbor() return only features id not geometry
31-
//
32-
// This code is very slow and need me for a tests.
33-
34-
double t = topologyTolerance();
35-
if ( t > 0.0 )
31+
double f = topologyTolerance();
32+
if ( f > 0 )
3633
{
37-
AdjacencyMatrix::iterator it;
38-
for ( it = mMatrix.begin(); it != mMatrix.end(); ++it )
34+
QgsRectangle r( pt.x() - f, pt.y() - f, pt.x() + f, pt.y() + f );
35+
QList< int > searchResult = mPointIndex.intersects( r );
36+
if ( !searchResult.empty() )
3937
{
40-
if ( it->first.sqrDist( pt ) < t )
41-
return it->first;
38+
int i = searchResult.front();
39+
if ( mPointMap[ i ].sqrDist( pt ) < topologyTolerance() )
40+
{
41+
return mPointMap[ i ];
42+
}
4243
}
44+
int newId = mPointMap.size() + 1;
45+
46+
QgsFeature f( newId );
47+
f.setGeometry( QgsGeometry::fromPoint( pt ) );
48+
mPointIndex.insertFeature( f );
49+
mPointMap.insert( newId, pt );
4350
}
51+
4452
mMatrix[ pt ];
4553
return pt;
4654
}

src/plugins/roadgraph/simplegraphbuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//QT4 includes
2222

2323
//QGIS includes
24+
#include <qgsspatialindex.h>
2425

2526
//forward declarations
2627
class QgsDistanceArea;
@@ -51,5 +52,9 @@ class RgSimpleGraphBuilder : public RgGraphBuilder
5152
AdjacencyMatrix adjacencyMatrix();
5253
private:
5354
AdjacencyMatrix mMatrix;
55+
56+
QgsSpatialIndex mPointIndex;
57+
58+
QMap< int, QgsPoint> mPointMap;
5459
};
5560
#endif //SIMPLEGRAPHBUILDER

0 commit comments

Comments
 (0)