Skip to content

Commit

Permalink
[tracer] Added reprojection of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 8, 2016
1 parent 9a6ffc4 commit 12705b7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
71 changes: 65 additions & 6 deletions src/core/qgstracer.cpp
Expand Up @@ -450,9 +450,17 @@ void extract_linework( QgsGeometry* g, QgsMultiPolyline& mpl )
// -------------


QgsTracer::QgsTracer( QList<QgsVectorLayer*> layers )
: mGraph(0)
QgsTracer::QgsTracer()
: mGraph( 0 )
{
}


void QgsTracer::initGraph()
{
if ( mGraph )
return; // already initialized

QgsFeature f;
QgsMultiPolyline mpl;

Expand All @@ -463,13 +471,29 @@ QgsTracer::QgsTracer( QList<QgsVectorLayer*> layers )
QTime t1, t2, t2a, t3;

t1.start();
foreach ( QgsVectorLayer* vl, layers )
foreach ( QgsVectorLayer* vl, mLayers )
{
QgsCoordinateTransform ct( vl->crs(), mCRS );

QgsFeatureIterator fi = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );
while ( fi.nextFeature( f ) )
{
if ( f.geometry() )
extract_linework( f.geometry(), mpl );
if ( !f.geometry() )
continue;

if ( !ct.isShortCircuited() )
{
try
{
f.geometry()->transform( ct );
}
catch ( QgsCsException& )
{
continue; // ignore if the transform failed
}
}

extract_linework( f.geometry(), mpl );
}
}
int timeExtract = t1.elapsed();
Expand Down Expand Up @@ -509,12 +533,47 @@ QgsTracer::QgsTracer( QList<QgsVectorLayer*> layers )
}

QgsTracer::~QgsTracer()
{
invalidateGraph();
}

void QgsTracer::setLayers( const QList<QgsVectorLayer*>& layers )
{
if ( mLayers == layers )
return;

mLayers = layers;
invalidateGraph();
}

void QgsTracer::setDestinationCrs( const QgsCoordinateReferenceSystem& crs )
{
if ( mCRS == crs )
return;

mCRS = crs;
invalidateGraph();
}

void QgsTracer::init()
{
if ( mGraph )
return;

initGraph();
}


void QgsTracer::invalidateGraph()
{
delete mGraph;
mGraph = 0;
}

QgsPolyline QgsTracer::findShortestPath( const QgsPoint& p1, const QgsPoint& p2 )
QVector<QgsPoint> QgsTracer::findShortestPath( const QgsPoint& p1, const QgsPoint& p2 )
{
init(); // does nothing if the graph exists already

QTime t;
t.start();
int v1 = point_in_graph( *mGraph, p1 );
Expand Down
28 changes: 26 additions & 2 deletions src/core/qgstracer.h
Expand Up @@ -22,7 +22,7 @@ class QgsVectorLayer;
#include <QVector>

#include "qgspoint.h"

#include "qgscoordinatereferencesystem.h"

struct QgsTracerGraph;

Expand All @@ -37,18 +37,42 @@ class CORE_EXPORT QgsTracer : public QObject
{
Q_OBJECT
public:
QgsTracer( QList<QgsVectorLayer*> layers );
QgsTracer();
~QgsTracer();

//! Get layers used for tracing
QList<QgsVectorLayer*> layers() const { return mLayers; }
//! Set layers used for tracing
void setLayers( const QList<QgsVectorLayer*>& layers );

//! Get CRS used for tracing
QgsCoordinateReferenceSystem destinationCrs() const { return mCRS; }
//! Set CRS used for tracing
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );

//! Build the internal data structures. This may take some time
//! depending on how big the input layers are. It is not necessary
//! to call this method explicitly - it will be called by findShortestPath()
//! if necessary.
void init();

//! Given two points, find the shortest path and return points on the way.
//! If the points are not located on existing vertices or edges,
//! search will fail and return empty array. The search will also fail
//! if the two points are not connected.
QVector<QgsPoint> findShortestPath( const QgsPoint& p1, const QgsPoint& p2 );

private:
void initGraph();
void invalidateGraph();

private:
//! Graph data structure for path searching
QgsTracerGraph* mGraph;
//! Input layers for the graph building
QList<QgsVectorLayer*> mLayers;
//! Destination CRS in which graph is built and tracing done
QgsCoordinateReferenceSystem mCRS;
};


Expand Down
17 changes: 8 additions & 9 deletions tests/src/core/testqgstracer.cpp
Expand Up @@ -94,11 +94,10 @@ void TestQgsTracer::testSimple()
* 0,0 +--+ 10,0
*/

QList<QgsVectorLayer*> layers;
QgsVectorLayer* vl = make_layer( wkts );
layers << vl;

QgsTracer tracer( layers );
QgsTracer tracer;
tracer.setLayers( QList<QgsVectorLayer*>() << vl );

QgsPolyline points1 = tracer.findShortestPath( QgsPoint( 0, 0 ), QgsPoint( 20, 10 ) );
QCOMPARE( points1.count(), 3 );
Expand Down Expand Up @@ -142,11 +141,11 @@ void TestQgsTracer::testPolygon()
QStringList wkts;
wkts << "POLYGON((0 0, 0 10, 20 10, 10 0, 0 0))";

QList<QgsVectorLayer*> layers;
QgsVectorLayer* vl = make_layer( wkts );
layers << vl;

QgsTracer tracer( layers );
QgsTracer tracer;
tracer.setLayers( QList<QgsVectorLayer*>() << vl );

QgsPolyline points = tracer.findShortestPath( QgsPoint( 1, 0 ), QgsPoint( 0, 1 ) );
QCOMPARE( points.count(), 3 );
QCOMPARE( points[0], QgsPoint( 1, 0 ) );
Expand All @@ -169,11 +168,11 @@ void TestQgsTracer::testButterfly()
* 0,0
*/

QList<QgsVectorLayer*> layers;
QgsVectorLayer* vl = make_layer( wkts );
layers << vl;

QgsTracer tracer( layers );
QgsTracer tracer;
tracer.setLayers( QList<QgsVectorLayer*>() << vl );

QgsPolyline points = tracer.findShortestPath( QgsPoint( 0, 0 ), QgsPoint( 10, 0 ) );

QCOMPARE( points.count(), 3 );
Expand Down

0 comments on commit 12705b7

Please sign in to comment.