QGIS API Documentation 3.43.0-Master (0cdc48caa8d)
qgsnetworkspeedstrategy.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsspeedstrategy.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
17
18QgsNetworkSpeedStrategy::QgsNetworkSpeedStrategy( int attributeId, double defaultValue, double toMetricFactor )
19 : mAttributeId( attributeId )
20 , mDefaultValue( defaultValue )
21 , mToMetricFactor( toMetricFactor )
22{
23}
24
25QVariant QgsNetworkSpeedStrategy::cost( double distance, const QgsFeature &f ) const
26{
27 double speed = mDefaultValue;
28 const QgsAttributes attrs = f.attributes();
29 if ( mAttributeId >= 0 && mAttributeId < attrs.count() )
30 {
31 const QVariant value = attrs.at( mAttributeId );
32 if ( !QgsVariantUtils::isNull( value ) )
33 {
34 speed = attrs.at( mAttributeId ).toDouble();
35 if ( speed < 0 )
36 speed = mDefaultValue;
37 }
38 }
39
40 return distance / ( speed * mToMetricFactor );
41}
42
44{
45 QSet<int> l;
46 if ( mAttributeId >= 0 )
47 l.insert( mAttributeId );
48 return l;
49}
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
QVariant cost(double distance, const QgsFeature &f) const override
Returns edge cost.
QgsNetworkSpeedStrategy(int attributeId, double defaultValue, double toMetricFactor)
Default constructor.
QSet< int > requiredAttributes() const override
Returns a list of the source layer attributes needed for cost calculation.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.