25 QString QgsServiceAreaFromPointAlgorithm::name()
const 27 return QStringLiteral(
"serviceareafrompoint" );
30 QString QgsServiceAreaFromPointAlgorithm::displayName()
const 32 return QObject::tr(
"Service area (from point)" );
35 QStringList QgsServiceAreaFromPointAlgorithm::tags()
const 37 return QObject::tr(
"network,service,area,shortest,fastest" ).split(
',' );
40 QString QgsServiceAreaFromPointAlgorithm::shortHelpString()
const 42 return QObject::tr(
"This algorithm creates a new vector with all the edges or parts of edges of a network line layer that can be reached within a distance or a time, starting from a point feature. The distance and the time (both referred to as \"travel cost\") must be specified respectively in the network layer units or in seconds." );
45 QgsServiceAreaFromPointAlgorithm *QgsServiceAreaFromPointAlgorithm::createInstance()
const 47 return new QgsServiceAreaFromPointAlgorithm();
50 void QgsServiceAreaFromPointAlgorithm::initAlgorithm(
const QVariantMap & )
54 addParameter(
new QgsProcessingParameterNumber( QStringLiteral(
"TRAVEL_COST" ), QObject::tr(
"Travel cost (distance for 'Shortest', time for 'Fastest')" ),
57 std::unique_ptr< QgsProcessingParameterBoolean > includeBounds = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"INCLUDE_BOUNDS" ), QObject::tr(
"Include upper/lower bound points" ),
false, true );
59 addParameter( includeBounds.release() );
61 std::unique_ptr< QgsProcessingParameterFeatureSink > outputLines = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"OUTPUT_LINES" ), QObject::tr(
"Service area (lines)" ),
63 outputLines->setCreateByDefault(
true );
64 addParameter( outputLines.release() );
66 std::unique_ptr< QgsProcessingParameterFeatureSink > outputPoints = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"OUTPUT" ), QObject::tr(
"Service area (boundary nodes)" ),
68 outputPoints->setCreateByDefault(
false );
69 addParameter( outputPoints.release() );
74 loadCommonParams( parameters, context, feedback );
76 QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
77 double travelCost = parameterAsDouble( parameters, QStringLiteral(
"TRAVEL_COST" ), context );
79 bool includeBounds =
true;
80 if ( parameters.contains( QStringLiteral(
"INCLUDE_BOUNDS" ) ) )
82 includeBounds = parameterAsBool( parameters, QStringLiteral(
"INCLUDE_BOUNDS" ), context );
85 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
86 QVector< QgsPointXY > snappedPoints;
87 mDirector->makeGraph( mBuilder.get(), QVector< QgsPointXY >() << startPoint, snappedPoints, feedback );
89 feedback->
pushInfo( QObject::tr(
"Calculating service area…" ) );
91 int idxStart = graph->
findVertex( snappedPoints[0] );
94 QVector< double > costs;
101 int inboundEdgeIndex;
102 double startVertexCost, endVertexCost;
106 for (
int i = 0; i < costs.size(); i++ )
108 inboundEdgeIndex = tree.at( i );
109 if ( inboundEdgeIndex == -1 && i != idxStart )
115 startVertexCost = costs.at( i );
116 if ( startVertexCost > travelCost )
122 vertices.insert( i );
127 for (
int edgeId : outgoingEdges )
129 edge = graph->
edge( edgeId );
130 endVertexCost = startVertexCost + edge.
cost( 0 ).toDouble();
132 if ( endVertexCost <= travelCost )
142 edgeEnd.
x(), edgeEnd.
y(), endVertexCost, travelCost );
144 points.push_back( interpolatedEndPoint );
145 lines.push_back(
QgsPolylineXY() << edgeStart << interpolatedEndPoint );
151 QList< int > verticesList = vertices.toList();
152 points.reserve( verticesList.size() );
153 std::sort( verticesList.begin(), verticesList.end() );
154 for (
int v : verticesList )
159 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
164 fields.
append(
QgsField( QStringLiteral(
"type" ), QVariant::String ) );
165 fields.
append(
QgsField( QStringLiteral(
"start" ), QVariant::String ) );
170 QString pointsSinkId;
171 std::unique_ptr< QgsFeatureSink > pointsSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, pointsSinkId, fields,
176 outputs.insert( QStringLiteral(
"OUTPUT" ), pointsSinkId );
186 QVector< int > nodes;
189 for (
int i = 0; i < costs.size(); i++ )
191 if ( costs.at( i ) > travelCost && tree.at( i ) != -1 )
193 vertexId = graph->
edge( tree.at( i ) ).fromVertex();
194 if ( costs.at( vertexId ) <= travelCost )
196 nodes.push_back( i );
201 for (
int i : nodes )
203 upperBoundary.push_back( graph->
vertex( graph->
edge( tree.at( i ) ).toVertex() ).point() );
204 lowerBoundary.push_back( graph->
vertex( graph->
edge( tree.at( i ) ).fromVertex() ).point() );
221 std::unique_ptr< QgsFeatureSink > linesSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT_LINES" ), context, linesSinkId, fields,
226 outputs.insert( QStringLiteral(
"OUTPUT_LINES" ), linesSinkId );
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
Parameter is an advanced parameter which should be hidden from users by default.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
A class to represent a 2D point.
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
Container of fields for a vector layer.
QgsPointXY point() const
Returns point associated with graph vertex.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
QgsGraphEdgeIds outgoingEdges() const
Returns outgoing edge ids, i.e.
int findVertex(const QgsPointXY &pt) const
Find vertex by associated point.
Encapsulate a field in an attribute table or data source.
A numeric parameter for processing algorithms.
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Mathematical graph representation.
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
int toVertex() const
Returns the index of the vertex at the end of this edge.
A point parameter for processing algorithms.
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Contains information about the context in which a processing algorithm is executed.
static QgsPointXY interpolatePointOnLineByValue(double x1, double y1, double v1, double x2, double y2, double v2, double value)
Interpolates the position of a point along the line from (x1, y1) to (x2, y2).
This class implements a graph edge.
static void dijkstra(const QgsGraph *source, int startVertexIdx, int criterionNum, QVector< int > *resultTree=nullptr, QVector< double > *resultCost=nullptr)
Solve shortest path problem using Dijkstra algorithm.