Skip to content
Permalink
Browse files

add qgsgraphbuilder interface

  • Loading branch information
Sergey Yakushevs
Sergey Yakushevs committed May 24, 2011
1 parent f2442d0 commit 6c1c5271a65a5aab3c598d9f540885b68067be6a
@@ -0,0 +1,47 @@
/***************************************************************************
qgsedgeproperter.h
--------------------------------------
Date : 2011-04-01
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS <at> list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSEDGEPROPERTERH
#define QGSEDGEPROPERTERH

// QT4 includes
#include <QVariant>

// QGIS includes
#include <qgsfeature.h>
#include <qgslabel.h>

class ANALYSIS_EXPORT QgsEdgeProperter
{
public:
QgsEdgeProperter()
{ }

virtual QgsAttributeList requiredAttributes() const
{ return QgsAttributeList(); }

virtual QVariant property( double distance, const QgsFeature& f ) const
{ return QVariant(); }
};

class ANALYSIS_EXPORT QgsEdgeDistanceProperter : public QgsEdgeProperter
{
public:
virtual QVariant property( double distance, const QgsFeature& ) const
{
return QVariant( distance );
}
};
#endif //QGSEDGEPROPERTYH
@@ -0,0 +1,103 @@
/***************************************************************************
* Copyright (C) 2010 by Sergey Yakushev *
* yakushevs <at> list.ru *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/

/**
* \file qgsgraphbuilder.cpp
* \brief implementation of QgsGraphBuilder
*/

#include "qgsgraphbuilder.h"
#include "qgsgraph.h"

// Qgis includes
#include <qgsfeature.h>
#include <qgsgeometry.h>

QgsGraphBuilder::QgsGraphBuilder( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea &da, bool otfEnabled, double topologyTolerance ) :
QgsGraphBuilderInterface( crs, da, otfEnabled, topologyTolerance )
{
mGraph = new QgsGraph();
}

QgsGraphBuilder::~QgsGraphBuilder()
{
if ( mGraph != NULL )
delete mGraph;
}

QgsPoint QgsGraphBuilder::addVertex( const QgsPoint& pt )
{
int id = pointId( pt );
if ( id != -1 )
return mGraph->vertex( id ).point();

if ( topologyTolerance() > 0 )
{
int newId = mGraph->addVertex( pt );

QgsFeature f( newId );
f.setGeometry( QgsGeometry::fromPoint( pt ) );
mPointIndex.insertFeature( f );

return pt;
}
int newId = mGraph->addVertex( pt );

mPointMap[ pt ] = newId;
return pt;
}

void QgsGraphBuilder::addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& prop )
{
int pt1_id = pointId( pt1 );
int pt2_id = pointId( pt2 );
if ( pt1_id == -1 )
{
// FIXME to QgsDebug
std::cerr << "haven't vertex at (" << pt1.x() << ";" << pt1.y() << ")\n";
return;
}
if ( pt2_id == -1 )
{
std::cerr << "haven't vertex at (" << pt2.x() << ";" << pt2.y() << ")\n";
return;
}
mGraph->addEdge( pt1_id, pt2_id, prop );
}

QgsGraph* QgsGraphBuilder::graph()
{
QgsGraph* res = mGraph;
mGraph = NULL;
return res;
}

int QgsGraphBuilder::pointId( const QgsPoint& pt )
{
if ( topologyTolerance() > 0.0 )
{
QgsRectangle r( pt.x() - topologyTolerance(), pt.y() - topologyTolerance(), pt.x() + topologyTolerance(), pt.y() + topologyTolerance() );
QList< int > searchResult = mPointIndex.intersects( r );
if ( !searchResult.empty() )
{
return searchResult.front();
}

}else
{
std::map< QgsPoint, int, QgsPointCompare >::iterator it = mPointMap.find( pt );
if ( it != mPointMap.end() )
{
return it->second;
}
}
return -1;
}
@@ -0,0 +1,82 @@
/***************************************************************************
qgsgraphbuilder.h
--------------------------------------
Date : 2010-10-25
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS@list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHBUILDERH
#define QGSGRAPHBUILDERH

#include "qgsgraphbuilderintr.h"

//QT4 includes

//QGIS includes
#include <qgsspatialindex.h>

//forward declarations
class QgsDistanceArea;
class QgsCoordinateTransform;
class QgsGraph;

/**
* \ingroup analysis
* \class QgsGraphBuilder
* \brief This class making the QgsGraph object
*/

class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface
{
private:
/**
* \class QgsPointCompare
* \brief equivalence ratio
*/
class QgsPointCompare
{
public:
bool operator()( const QgsPoint& a, const QgsPoint& b ) const
{
return a.x() == b.x() ? a.y() < b.y() : a.x() < b.x();
}
};

public:
/**
* default constructor
*/
QgsGraphBuilder( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea& da, bool otfEnabled, double topologyTolerance = 0.0 );

~QgsGraphBuilder();

/*
* MANDATORY BUILDER PROPERTY DECLARATION
*/
virtual QgsPoint addVertex( const QgsPoint& pt );

virtual void addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& prop );

/**
* return QgsGraph result;
*/
QgsGraph* graph();

private:
// return -1 if pt not found
int pointId( const QgsPoint& pt );

QgsSpatialIndex mPointIndex;

std::map< QgsPoint, int, QgsPointCompare > mPointMap;

QgsGraph *mGraph;
};
#endif //QGSGRAPHBUILDERH
@@ -0,0 +1,96 @@
/***************************************************************************
qgsgraphbuilder.h
--------------------------------------
Date : 2010-10-22
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS <at> list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSGRAPHBUILDERINTERFACE
#define QGSGRAPHBUILDERINTERFACE

//QT4 includes
#include <QVector>
#include <QVariant>

//QGIS includes
#include <qgspoint.h>
#include <qgscoordinatereferencesystem.h>
#include <qgsdistancearea.h>

//forward declarations

/**
* \ingroup analysis
* \class QgsGraphBuilderInterface
* \brief Determine interface for creating a graph. Contains the settings of the graph.
*/
class ANALYSIS_EXPORT QgsGraphBuilderInterface
{
public:
/**
* QgsGraphBuilderInterface constructor
* @param crs Coordinate reference system for new graph vertex
* @param da Object for edge measurement. Source CRS will be set to graph crs
* @param ctfEnabled enable coordinate transform from source graph CRS to CRS graph
* @param topologyTolerance sqrt distance between source point as one graph vertex
*/
QgsGraphBuilderInterface( const QgsCoordinateReferenceSystem& crs, const QgsDistanceArea& da, bool ctfEnabled = true, double topologyTolerance = 0.0) :
mCrs( crs ), mDa( da ), mCtfEnabled ( ctfEnabled ), mTopologyTolerance( topologyTolerance )
{
mDa.setSourceCrs( mCrs.srsid() );
}

//! Destructor
virtual ~QgsGraphBuilderInterface()
{ }

//! get destinaltion Crs
QgsCoordinateReferenceSystem& destinationCrs()
{
return mCrs;
}

//! get coordinate transformation enabled
bool coordinateTransformationEnabled()
{
return mCtfEnabled;
}

//! get topology tolerance
bool topologyTolerance()
{
return mTopologyTolerance;
}

//! get measurement tool
QgsDistanceArea& distanceArea()
{
return mDa;
}

//! add vertex
virtual QgsPoint addVertex( const QgsPoint& pt )
{ return pt; }

//! add arc
virtual void addArc( const QgsPoint& pt1, const QgsPoint& pt2, const QVector< QVariant >& properties )
{ }

private:
QgsCoordinateReferenceSystem mCrs;

QgsDistanceArea mDa;

bool mCtfEnabled;

double mTopologyTolerance;

};
#endif //QGSGRAPHBUILDERINTERFACE

0 comments on commit 6c1c527

Please sign in to comment.
You can’t perform that action at this time.