Skip to content

Commit 6c1c527

Browse files
committed
add qgsgraphbuilder interface
1 parent f2442d0 commit 6c1c527

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgsedgeproperter.h
3+
--------------------------------------
4+
Date : 2011-04-01
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSEDGEPROPERTERH
17+
#define QGSEDGEPROPERTERH
18+
19+
// QT4 includes
20+
#include <QVariant>
21+
22+
// QGIS includes
23+
#include <qgsfeature.h>
24+
#include <qgslabel.h>
25+
26+
class ANALYSIS_EXPORT QgsEdgeProperter
27+
{
28+
public:
29+
QgsEdgeProperter()
30+
{ }
31+
32+
virtual QgsAttributeList requiredAttributes() const
33+
{ return QgsAttributeList(); }
34+
35+
virtual QVariant property( double distance, const QgsFeature& f ) const
36+
{ return QVariant(); }
37+
};
38+
39+
class ANALYSIS_EXPORT QgsEdgeDistanceProperter : public QgsEdgeProperter
40+
{
41+
public:
42+
virtual QVariant property( double distance, const QgsFeature& ) const
43+
{
44+
return QVariant( distance );
45+
}
46+
};
47+
#endif //QGSEDGEPROPERTYH
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/***************************************************************************
2+
* Copyright (C) 2010 by Sergey Yakushev *
3+
* yakushevs <at> list.ru *
4+
* *
5+
* *
6+
* This program is free software; you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation; either version 2 of the License, or *
9+
* (at your option) any later version. *
10+
***************************************************************************/
11+
12+
/**
13+
* \file qgsgraphbuilder.cpp
14+
* \brief implementation of QgsGraphBuilder
15+
*/
16+
17+
#include "qgsgraphbuilder.h"
18+
#include "qgsgraph.h"
19+
20+
// Qgis includes
21+
#include <qgsfeature.h>
22+
#include <qgsgeometry.h>
23+
24+
QgsGraphBuilder::QgsGraphBuilder( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea &da, bool otfEnabled, double topologyTolerance ) :
25+
QgsGraphBuilderInterface( crs, da, otfEnabled, topologyTolerance )
26+
{
27+
mGraph = new QgsGraph();
28+
}
29+
30+
QgsGraphBuilder::~QgsGraphBuilder()
31+
{
32+
if ( mGraph != NULL )
33+
delete mGraph;
34+
}
35+
36+
QgsPoint QgsGraphBuilder::addVertex( const QgsPoint& pt )
37+
{
38+
int id = pointId( pt );
39+
if ( id != -1 )
40+
return mGraph->vertex( id ).point();
41+
42+
if ( topologyTolerance() > 0 )
43+
{
44+
int newId = mGraph->addVertex( pt );
45+
46+
QgsFeature f( newId );
47+
f.setGeometry( QgsGeometry::fromPoint( pt ) );
48+
mPointIndex.insertFeature( f );
49+
50+
return pt;
51+
}
52+
int newId = mGraph->addVertex( pt );
53+
54+
mPointMap[ pt ] = newId;
55+
return pt;
56+
}
57+
58+
void QgsGraphBuilder::addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& prop )
59+
{
60+
int pt1_id = pointId( pt1 );
61+
int pt2_id = pointId( pt2 );
62+
if ( pt1_id == -1 )
63+
{
64+
// FIXME to QgsDebug
65+
std::cerr << "haven't vertex at (" << pt1.x() << ";" << pt1.y() << ")\n";
66+
return;
67+
}
68+
if ( pt2_id == -1 )
69+
{
70+
std::cerr << "haven't vertex at (" << pt2.x() << ";" << pt2.y() << ")\n";
71+
return;
72+
}
73+
mGraph->addEdge( pt1_id, pt2_id, prop );
74+
}
75+
76+
QgsGraph* QgsGraphBuilder::graph()
77+
{
78+
QgsGraph* res = mGraph;
79+
mGraph = NULL;
80+
return res;
81+
}
82+
83+
int QgsGraphBuilder::pointId( const QgsPoint& pt )
84+
{
85+
if ( topologyTolerance() > 0.0 )
86+
{
87+
QgsRectangle r( pt.x() - topologyTolerance(), pt.y() - topologyTolerance(), pt.x() + topologyTolerance(), pt.y() + topologyTolerance() );
88+
QList< int > searchResult = mPointIndex.intersects( r );
89+
if ( !searchResult.empty() )
90+
{
91+
return searchResult.front();
92+
}
93+
94+
}else
95+
{
96+
std::map< QgsPoint, int, QgsPointCompare >::iterator it = mPointMap.find( pt );
97+
if ( it != mPointMap.end() )
98+
{
99+
return it->second;
100+
}
101+
}
102+
return -1;
103+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/***************************************************************************
2+
qgsgraphbuilder.h
3+
--------------------------------------
4+
Date : 2010-10-25
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS@list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSGRAPHBUILDERH
16+
#define QGSGRAPHBUILDERH
17+
18+
#include "qgsgraphbuilderintr.h"
19+
20+
//QT4 includes
21+
22+
//QGIS includes
23+
#include <qgsspatialindex.h>
24+
25+
//forward declarations
26+
class QgsDistanceArea;
27+
class QgsCoordinateTransform;
28+
class QgsGraph;
29+
30+
/**
31+
* \ingroup analysis
32+
* \class QgsGraphBuilder
33+
* \brief This class making the QgsGraph object
34+
*/
35+
36+
class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface
37+
{
38+
private:
39+
/**
40+
* \class QgsPointCompare
41+
* \brief equivalence ratio
42+
*/
43+
class QgsPointCompare
44+
{
45+
public:
46+
bool operator()( const QgsPoint& a, const QgsPoint& b ) const
47+
{
48+
return a.x() == b.x() ? a.y() < b.y() : a.x() < b.x();
49+
}
50+
};
51+
52+
public:
53+
/**
54+
* default constructor
55+
*/
56+
QgsGraphBuilder( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea& da, bool otfEnabled, double topologyTolerance = 0.0 );
57+
58+
~QgsGraphBuilder();
59+
60+
/*
61+
* MANDATORY BUILDER PROPERTY DECLARATION
62+
*/
63+
virtual QgsPoint addVertex( const QgsPoint& pt );
64+
65+
virtual void addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& prop );
66+
67+
/**
68+
* return QgsGraph result;
69+
*/
70+
QgsGraph* graph();
71+
72+
private:
73+
// return -1 if pt not found
74+
int pointId( const QgsPoint& pt );
75+
76+
QgsSpatialIndex mPointIndex;
77+
78+
std::map< QgsPoint, int, QgsPointCompare > mPointMap;
79+
80+
QgsGraph *mGraph;
81+
};
82+
#endif //QGSGRAPHBUILDERH
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/***************************************************************************
2+
qgsgraphbuilder.h
3+
--------------------------------------
4+
Date : 2010-10-22
5+
Copyright : (C) 2010 by Yakushev Sergey
6+
Email : YakushevS <at> list.ru
7+
****************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSGRAPHBUILDERINTERFACE
16+
#define QGSGRAPHBUILDERINTERFACE
17+
18+
//QT4 includes
19+
#include <QVector>
20+
#include <QVariant>
21+
22+
//QGIS includes
23+
#include <qgspoint.h>
24+
#include <qgscoordinatereferencesystem.h>
25+
#include <qgsdistancearea.h>
26+
27+
//forward declarations
28+
29+
/**
30+
* \ingroup analysis
31+
* \class QgsGraphBuilderInterface
32+
* \brief Determine interface for creating a graph. Contains the settings of the graph.
33+
*/
34+
class ANALYSIS_EXPORT QgsGraphBuilderInterface
35+
{
36+
public:
37+
/**
38+
* QgsGraphBuilderInterface constructor
39+
* @param crs Coordinate reference system for new graph vertex
40+
* @param da Object for edge measurement. Source CRS will be set to graph crs
41+
* @param ctfEnabled enable coordinate transform from source graph CRS to CRS graph
42+
* @param topologyTolerance sqrt distance between source point as one graph vertex
43+
*/
44+
QgsGraphBuilderInterface( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea& da, bool ctfEnabled = true, double topologyTolerance = 0.0) :
45+
mCrs( crs ), mDa( da ), mCtfEnabled ( ctfEnabled ), mTopologyTolerance( topologyTolerance )
46+
{
47+
mDa.setSourceCrs( mCrs.srsid() );
48+
}
49+
50+
//! Destructor
51+
virtual ~QgsGraphBuilderInterface()
52+
{ }
53+
54+
//! get destinaltion Crs
55+
QgsCoordinateReferenceSystem& destinationCrs()
56+
{
57+
return mCrs;
58+
}
59+
60+
//! get coordinate transformation enabled
61+
bool coordinateTransformationEnabled()
62+
{
63+
return mCtfEnabled;
64+
}
65+
66+
//! get topology tolerance
67+
bool topologyTolerance()
68+
{
69+
return mTopologyTolerance;
70+
}
71+
72+
//! get measurement tool
73+
QgsDistanceArea& distanceArea()
74+
{
75+
return mDa;
76+
}
77+
78+
//! add vertex
79+
virtual QgsPoint addVertex( const QgsPoint& pt )
80+
{ return pt; }
81+
82+
//! add arc
83+
virtual void addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& properties )
84+
{ }
85+
86+
private:
87+
QgsCoordinateReferenceSystem mCrs;
88+
89+
QgsDistanceArea mDa;
90+
91+
bool mCtfEnabled;
92+
93+
double mTopologyTolerance;
94+
95+
};
96+
#endif //QGSGRAPHBUILDERINTERFACE

0 commit comments

Comments
 (0)