Skip to content

Commit

Permalink
add QgsGraph class
Browse files Browse the repository at this point in the history
  • Loading branch information
stopa85milk committed May 23, 2011
1 parent 60353b1 commit 4b462f5
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/analysis/network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@


#############################################################
# sources

SET(QGIS_NETWORK_ANALYSIS_SRCS
qgsgraph.cpp
)

INCLUDE_DIRECTORIES(BEFORE raster)

SET(QGIS_ANALYSIS_MOC_HDRS
)

QT4_WRAP_CPP(QGIS_NETWORK_ANALYSIS_MOC_SRCS ${QGIS_ANALYSIS_MOC_HDRS})


INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}../../core/
interpolation
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
)


#############################################################
# qgis_analysis library

ADD_LIBRARY(qgis_networkanalysis SHARED ${QGIS_NETWORK_ANALYSIS_SRCS} ${QGIS_NETWORK_ANALYSIS_MOC_SRCS} )

ADD_DEPENDENCIES(qgis_networkanalysis qgis_core)

SET_TARGET_PROPERTIES(qgis_networkanalysis PROPERTIES VERSION ${COMPLETE_VERSION} SOVERSION ${COMPLETE_VERSION})

TARGET_LINK_LIBRARIES(qgis_networkanalysis
qgis_core
)

IF (APPLE)
SET_TARGET_PROPERTIES(qgis_networkanalysis PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE )
ENDIF (APPLE)

INSTALL(TARGETS qgis_networkanalysis
RUNTIME DESTINATION ${QGIS_BIN_DIR}
LIBRARY DESTINATION ${QGIS_LIB_DIR}
ARCHIVE DESTINATION ${QGIS_LIB_DIR})


# Added by Tim to install headers

SET(QGIS_NETWORK_ANALYSIS_HDRS
qgsgraph.h )

INSTALL(CODE "MESSAGE(\"Installing NETWORK ANALYSIS headers...\")")
INSTALL(FILES ${QGIS_NETWORK_ANALYSIS_HDRS} ${QGIS_NETWORK_ANALYSIS_MOC_HDRS} DESTINATION ${QGIS_INCLUDE_DIR})
129 changes: 129 additions & 0 deletions src/analysis/network/qgsgraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/***************************************************************************
* Copyright (C) 2011 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 qgsgraph.cpp
* \brief implementation QgsGraph, QgsGraphVertex, QgsGraphEdge
*/

#include "qgsgraph.h"

QgsGraph::QgsGraph()
{
}


QgsGraph::~QgsGraph()
{

}

int QgsGraph::addVertex( const QgsPoint& pt )
{
mGraphVertexes.append( QgsGraphVertex( pt ) );
return mGraphVertexes.size()-1;
}

int QgsGraph::addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant >& properties )
{
QgsGraphEdge e;

e.mProperties = properties;
e.mOut = outVertexIdx;
e.mIn = inVertexIdx;
mGraphEdges.push_back( e );
int edgeIdx = mGraphEdges.size()-1;

mGraphVertexes[ outVertexIdx ].mOutEdges.push_back( edgeIdx );
mGraphVertexes[ inVertexIdx ].mInEdges.push_back( edgeIdx );

return mGraphEdges.size()-1;
}

const QgsGraphVertex& QgsGraph::vertex( int idx ) const
{
return mGraphVertexes[ idx ];
}

const QgsGraphEdge& QgsGraph::edge( int idx ) const
{
return mGraphEdges[ idx ];
}


int QgsGraph::vertexCount() const
{
return mGraphVertexes.size();
}

int QgsGraph::edgeCount() const
{
return mGraphEdges.size();
}

int QgsGraph::findVertex( const QgsPoint& pt ) const
{
int i = 0;
for ( i = 0; i < mGraphVertexes.size(); ++i )
{
if ( mGraphVertexes[ i ].point() == pt )
{
return i;
}
}
return -1;
}

QgsGraphEdge::QgsGraphEdge()
{

}

QVariant QgsGraphEdge::property(int i) const
{
return mProperties[ i ];
}

QVector< QVariant > QgsGraphEdge::properties() const
{
return mProperties;
}

int QgsGraphEdge::in() const
{
return mIn;
}

int QgsGraphEdge::out() const
{
return mOut;
}

QgsGraphVertex::QgsGraphVertex( const QgsPoint& point )
: mCoordinate( point )
{

}

QgsGraphEdgeList QgsGraphVertex::outEdges() const
{
return mOutEdges;
}

QgsGraphEdgeList QgsGraphVertex::inEdges() const
{
return mInEdges;
}

QgsPoint QgsGraphVertex::point() const
{
return mCoordinate;
}
178 changes: 178 additions & 0 deletions src/analysis/network/qgsgraph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/***************************************************************************
graph.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. *
* *
***************************************************************************/

/*
*
* \file qgsgraph.h
* Этот файл описывает встроенные в QGIS классы описывающие математический граф. Вершина графа идентифицируется своими географическими координатами, никакие дополнительные свойства ей не могут быть присвоены. Количество свойств графа определяется разработчиком и не ограничено, например длина и время движения по дуге. Граф может быть направленным, иметь инцедентные ребра и петли.
*
*/

#ifndef QGSGRAPHH
#define QGSGRAPHH

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

// QGIS includes
#include "qgspoint.h"

class QgsGraphVertex;

/**
* \ingroup analysis
* \class QgsGraphEdge
* \brief This class implement a graph edge
*/
class ANALYSIS_EXPORT QgsGraphEdge
{
public:
QgsGraphEdge();

/**
* return property value
* @param propertyIndex property index
*/
QVariant property(int propertyIndex ) const;

/**
* get array of proertyes
*/
QVector< QVariant > properties() const;

/**
* return index of outgoing vertex
*/
int out() const;

/**
* return index of incoming vertex
*/
int in() const;

private:

QVector< QVariant > mProperties;

int mOut;
int mIn;

friend class QgsGraph;
};


typedef QList< int > QgsGraphEdgeList;

/**
* \ingroup analysis
* \class QgsGraphVertex
* \brief This class implement a graph vertex
*/
class ANALYSIS_EXPORT QgsGraphVertex
{
public:
/**
* default constructor. It need for QT's container, e.g. QVector
*/
QgsGraphVertex() {}

/**
* This constructor initializes QgsGraphVertex object and associates a vertex with a point
*/

QgsGraphVertex( const QgsPoint& point );

/**
* return outgoing edges
*/
QgsGraphEdgeList outEdges() const;

/**
* return incoming edges
*/
QgsGraphEdgeList inEdges() const;

/**
* return vertex point
*/
QgsPoint point() const;

private:
QgsPoint mCoordinate;
QgsGraphEdgeList mOutEdges;
QgsGraphEdgeList mInEdges;

friend class QgsGraph;
};

/**
* \ingroup analysis
* \class QgsGraph
* \brief Mathematics graph representation
*/

class ANALYSIS_EXPORT QgsGraph
{
public:
QgsGraph();

~QgsGraph();

// begin graph constructing methods
/**
* add vertex to a grap
*/
int addVertex( const QgsPoint& pt );

/**
* add edge to a graph
*/
int addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant >& properties );

/**
* retrun vertex count
*/
int vertexCount() const;

/**
* return vertex at index
*/
const QgsGraphVertex& vertex( int idx ) const;

/**
* retrun edge count
*/
int edgeCount() const;

/**
* retrun edge at index
*/
const QgsGraphEdge& edge( int idx ) const;

/**
* find vertex by point
* \return vertex index
*/
int findVertex( const QgsPoint& pt ) const;

private:
QVector<QgsGraphVertex> mGraphVertexes;

QVector<QgsGraphEdge> mGraphEdges;
};

#endif //QGSGRAPHH

0 comments on commit 4b462f5

Please sign in to comment.