Skip to content

Commit

Permalink
Merge pull request #4872 from alexbruy/network-api
Browse files Browse the repository at this point in the history
[API] small improvements for network analysis API
  • Loading branch information
alexbruy committed Jul 17, 2017
2 parents 246685b + 09bb55a commit 6c0cb2f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 53 deletions.
16 changes: 13 additions & 3 deletions doc/api_break.dox
Expand Up @@ -361,8 +361,6 @@ Data Providers {#qgis_api_break_3_0_DataProviders}
- mAttrPalIndexName: overwrite palAttributeIndexNames()




Qgis {#qgis_api_break_3_0_Qgis}
----

Expand Down Expand Up @@ -1116,7 +1114,7 @@ version instead.
- QgsExpression::Node::referencedColumns() returns QSet<QString> instead of QStringList
- `QgsExpression::Node` was renamed to `QgsExpressionNode`
- `QgsExpression::Function` was renamed to `QgsExpressionFunction`
- variableHelpText() no longer returns a formatted HTML string. It now just returns the plain text help string. Use formatVariableHelp()
- variableHelpText() no longer returns a formatted HTML string. It now just returns the plain text help string. Use formatVariableHelp()
to obtain the formatted version.


Expand Down Expand Up @@ -2512,6 +2510,18 @@ QgsRendererAbstractMetadata {#qgis_api_break_3_0_QgsRendererAbstractMetadata}

- createRenderer() now expects a reference to QgsReadWriteContext as the second argument

QgsGraphDirector {#qgis_api_break_3_0_QgsGraphDirector}
----------------

- makeGraph() now uses QgsFeedback for progress reporting and cancelation
- buildProgress() signal was removed
- buildMessage() signal was removed

QgsVectorLayerDirector {#qgis_api_break_3_0_QgsVectorLayerDirector}
----------------------

- QgsVectorLayerDirector() constructor now expects a reference to QgsFeatureSource as the first argument
- makeGraph() now uses QgsFeedback for progress reporting and cancelation

Processing {#qgis_api_break_3_0_Processing}
----------
Expand Down
14 changes: 3 additions & 11 deletions python/analysis/network/qgsgraphdirector.sip
Expand Up @@ -31,29 +31,21 @@ class QgsGraphDirector : QObject
else
sipType = NULL;
%End
signals:
void buildProgress( int, int ) const;
%Docstring
Emitted to report graph building progress
%End
void buildMessage( const QString & ) const;
%Docstring
Emitted to report information about graph building
%End

public:

virtual ~QgsGraphDirector();

virtual void makeGraph( QgsGraphBuilderInterface *builder,
const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY > &snappedPoints /Out/ ) const;
QVector< QgsPointXY > &snappedPoints /Out/,
QgsFeedback *feedback = 0 ) const;
%Docstring
Make a graph using QgsGraphBuilder

\param builder the graph builder
\param additionalPoints list of points that should be snapped to the graph
\param snappedPoints list of snapped points
\param feedback feedback object for reporting progress
.. note::

if snappedPoints[i] == QgsPointXY(0.0,0.0) then snapping failed.
Expand Down
7 changes: 4 additions & 3 deletions python/analysis/network/qgsvectorlayerdirector.sip
Expand Up @@ -29,7 +29,7 @@ class QgsVectorLayerDirector : QgsGraphDirector
DirectionBoth,
};

QgsVectorLayerDirector( QgsVectorLayer *myLayer,
QgsVectorLayerDirector( QgsFeatureSource *source,
int directionFieldId,
const QString &directDirectionValue,
const QString &reverseDirectionValue,
Expand All @@ -38,7 +38,7 @@ class QgsVectorLayerDirector : QgsGraphDirector
);
%Docstring
Default constructor
\param myLayer source vector layer
\param source feature source representing network
\param directionFieldId field containing direction value
\param directDirectionValue value for direct one-way road
\param reverseDirectionValue value for reversed one-way road
Expand All @@ -51,7 +51,8 @@ class QgsVectorLayerDirector : QgsGraphDirector

virtual void makeGraph( QgsGraphBuilderInterface *builder,
const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY> &snappedPoints /Out/ ) const;
QVector< QgsPointXY> &snappedPoints /Out/,
QgsFeedback *feedback = 0 ) const;
%Docstring
MANDATORY DIRECTOR PROPERTY DECLARATION
%End
Expand Down
16 changes: 7 additions & 9 deletions src/analysis/network/qgsgraphdirector.h
Expand Up @@ -20,8 +20,9 @@
#include <QVector>
#include <QList>

#include <qgis.h>
#include <qgspoint.h>
#include "qgis.h"
#include "qgspoint.h"
#include "qgsfeedback.h"
#include "qgsnetworkstrategy.h"
#include "qgis_analysis.h"

Expand Down Expand Up @@ -53,12 +54,6 @@ class ANALYSIS_EXPORT QgsGraphDirector : public QObject

Q_OBJECT

signals:
//! Emitted to report graph building progress
void buildProgress( int, int ) const;
//! Emitted to report information about graph building
void buildMessage( const QString & ) const;

public:

virtual ~QgsGraphDirector() { }
Expand All @@ -69,15 +64,18 @@ class ANALYSIS_EXPORT QgsGraphDirector : public QObject
* \param builder the graph builder
* \param additionalPoints list of points that should be snapped to the graph
* \param snappedPoints list of snapped points
* \param feedback feedback object for reporting progress
* \note if snappedPoints[i] == QgsPointXY(0.0,0.0) then snapping failed.
*/
virtual void makeGraph( QgsGraphBuilderInterface *builder,
const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY > &snappedPoints SIP_OUT ) const
QVector< QgsPointXY > &snappedPoints SIP_OUT,
QgsFeedback *feedback = nullptr ) const
{
Q_UNUSED( builder );
Q_UNUSED( additionalPoints );
Q_UNUSED( snappedPoints );
Q_UNUSED( feedback );
}

//! Add optimization strategy
Expand Down
57 changes: 35 additions & 22 deletions src/analysis/network/qgsvectorlayerdirector.cpp
Expand Up @@ -22,12 +22,12 @@
#include "qgsgraphbuilderinterface.h"

#include "qgsfeatureiterator.h"
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>
#include <qgspoint.h>
#include <qgsgeometry.h>
#include <qgsdistancearea.h>
#include <qgswkbtypes.h>
#include "qgsfeaturesource.h"
#include "qgsvectordataprovider.h"
#include "qgspoint.h"
#include "qgsgeometry.h"
#include "qgsdistancearea.h"
#include "qgswkbtypes.h"

#include <QString>
#include <QtAlgorithms>
Expand Down Expand Up @@ -102,15 +102,15 @@ bool TiePointInfoCompare( const TiePointInfo &a, const TiePointInfo &b )
return a.mFirstPoint.x() == b.mFirstPoint.x() ? a.mFirstPoint.y() < b.mFirstPoint.y() : a.mFirstPoint.x() < b.mFirstPoint.x();
}

QgsVectorLayerDirector::QgsVectorLayerDirector( QgsVectorLayer *myLayer,
QgsVectorLayerDirector::QgsVectorLayerDirector( QgsFeatureSource *source,
int directionFieldId,
const QString &directDirectionValue,
const QString &reverseDirectionValue,
const QString &bothDirectionValue,
const Direction defaultDirection
)
{
mVectorLayer = myLayer;
mSource = source;
mDirectionFieldId = directionFieldId;
mDirectDirectionValue = directDirectionValue;
mReverseDirectionValue = reverseDirectionValue;
Expand All @@ -124,25 +124,20 @@ QString QgsVectorLayerDirector::name() const
}

void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY > &snappedPoints ) const
QVector< QgsPointXY > &snappedPoints, QgsFeedback *feedback ) const
{
QgsVectorLayer *vl = mVectorLayer;

if ( !vl )
return;

int featureCount = ( int ) vl->featureCount() * 2;
int featureCount = ( int ) mSource->featureCount() * 2;
int step = 0;

QgsCoordinateTransform ct;
ct.setSourceCrs( vl->crs() );
ct.setSourceCrs( mSource->sourceCrs() );
if ( builder->coordinateTransformationEnabled() )
{
ct.setDestinationCrs( builder->destinationCrs() );
}
else
{
ct.setDestinationCrs( vl->crs() );
ct.setDestinationCrs( mSource->sourceCrs() );
}

snappedPoints = QVector< QgsPointXY >( additionalPoints.size(), QgsPointXY( 0.0, 0.0 ) );
Expand All @@ -156,13 +151,18 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
//Graph's points;
QVector< QgsPointXY > points;

QgsFeatureIterator fit = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeatureIterator fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );

// begin: tie points to the graph
QgsAttributeList la;
QgsFeature feature;
while ( fit.nextFeature( feature ) )
{
if ( feedback && feedback->isCanceled() )
{
return;
}

QgsMultiPolyline mpl;
if ( QgsWkbTypes::flatType( feature.geometry().geometry()->wkbType() ) == QgsWkbTypes::MultiLineString )
mpl = feature.geometry().asMultiPolyline();
Expand Down Expand Up @@ -212,7 +212,11 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
isFirstPoint = false;
}
}
emit buildProgress( ++step, featureCount );
if ( feedback )
{
feedback->setProgress( 100.0 * static_cast< double >( ++step ) / featureCount );
}

}
// end: tie points to graph

Expand Down Expand Up @@ -276,9 +280,14 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
} // end fill attribute list 'la'

// begin graph construction
fit = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
while ( fit.nextFeature( feature ) )
{
if ( feedback && feedback->isCanceled() )
{
return;
}

Direction directionType = mDefaultDirection;

// What direction have feature?
Expand Down Expand Up @@ -387,6 +396,10 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
isFirstPoint = false;
} // for (it = pl.begin(); it != pl.end(); ++it)
}
emit buildProgress( ++step, featureCount );
} // while( vl->nextFeature(feature) )
if ( feedback )
{
feedback->setProgress( 100.0 * static_cast< double >( ++step ) / featureCount );
}

} // while( mSource->nextFeature(feature) )
} // makeGraph( QgsGraphBuilderInterface *builder, const QVector< QgsPointXY >& additionalPoints, QVector< QgsPointXY >& tiedPoint )
11 changes: 6 additions & 5 deletions src/analysis/network/qgsvectorlayerdirector.h
Expand Up @@ -22,7 +22,7 @@
#include "qgis_analysis.h"

class QgsGraphBuilderInterface;
class QgsVectorLayer;
class QgsFeatureSource;

/**
* \ingroup analysis
Expand Down Expand Up @@ -51,15 +51,15 @@ class ANALYSIS_EXPORT QgsVectorLayerDirector : public QgsGraphDirector

/**
* Default constructor
* \param myLayer source vector layer
* \param source feature source representing network
* \param directionFieldId field containing direction value
* \param directDirectionValue value for direct one-way road
* \param reverseDirectionValue value for reversed one-way road
* \param bothDirectionValue value for two-way (bidirectional) road
* \param defaultDirection default direction. Will be used if corresponding
* attribute value is not set or does not equal to the given values
*/
QgsVectorLayerDirector( QgsVectorLayer *myLayer,
QgsVectorLayerDirector( QgsFeatureSource *source,
int directionFieldId,
const QString &directDirectionValue,
const QString &reverseDirectionValue,
Expand All @@ -74,12 +74,13 @@ class ANALYSIS_EXPORT QgsVectorLayerDirector : public QgsGraphDirector
*/
void makeGraph( QgsGraphBuilderInterface *builder,
const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY> &snappedPoints SIP_OUT ) const override;
QVector< QgsPointXY> &snappedPoints SIP_OUT,
QgsFeedback *feedback = nullptr ) const override;

QString name() const override;

private:
QgsVectorLayer *mVectorLayer = nullptr;
QgsFeatureSource *mSource = nullptr;
int mDirectionFieldId;
QString mDirectDirectionValue;
QString mReverseDirectionValue;
Expand Down

0 comments on commit 6c0cb2f

Please sign in to comment.