QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmserviceareafrompoint.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmserviceareafrompoint.cpp
3 ---------------------
4 begin : July 2018
5 copyright : (C) 2018 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsgeometryutils.h"
21#include "qgsgraphanalyzer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsServiceAreaFromPointAlgorithm::name() const
30{
31 return u"serviceareafrompoint"_s;
32}
33
34QString QgsServiceAreaFromPointAlgorithm::displayName() const
35{
36 return QObject::tr( "Service area (from point)" );
37}
38
39QStringList QgsServiceAreaFromPointAlgorithm::tags() const
40{
41 return QObject::tr( "network,service,area,shortest,fastest" ).split( ',' );
42}
43
44QString QgsServiceAreaFromPointAlgorithm::shortHelpString() const
45{
46 return QObject::tr(
47 "This algorithm creates a new vector layer with all the edges or parts of edges "
48 "of a network line layer that can be reached within a distance or a time, "
49 "starting from a point feature. The distance and the time (both referred to "
50 "as \"travel cost\") must be specified respectively in the network layer "
51 "units or in hours."
52 );
53}
54
55QString QgsServiceAreaFromPointAlgorithm::shortDescription() const
56{
57 return QObject::tr(
58 "Creates a vector layer with all the edges or parts of edges "
59 "of a network line layer that can be reached within a distance or a time, "
60 "starting from a point feature."
61 );
62}
63
64QgsServiceAreaFromPointAlgorithm *QgsServiceAreaFromPointAlgorithm::createInstance() const
65{
66 return new QgsServiceAreaFromPointAlgorithm();
67}
68
69void QgsServiceAreaFromPointAlgorithm::initAlgorithm( const QVariantMap & )
70{
71 addCommonParams();
72 addParameter( new QgsProcessingParameterPoint( u"START_POINT"_s, QObject::tr( "Start point" ) ) );
73
74 auto travelCost
75 = std::make_unique<QgsProcessingParameterNumber>( u"TRAVEL_COST"_s, QObject::tr( "Travel cost (distance for 'Shortest', time for 'Fastest')" ), Qgis::ProcessingNumberParameterType::Double, 0, true, 0 );
76 travelCost->setFlags( travelCost->flags() | Qgis::ProcessingParameterFlag::Hidden );
77 addParameter( travelCost.release() );
78
79 addParameter(
80 new QgsProcessingParameterNumber( u"TRAVEL_COST2"_s, QObject::tr( "Travel cost (distance for 'Shortest', time for 'Fastest')" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0 )
81 );
82
83 std::unique_ptr<QgsProcessingParameterNumber> maxPointDistanceFromNetwork
84 = std::make_unique<QgsProcessingParameterDistance>( u"POINT_TOLERANCE"_s, QObject::tr( "Maximum point distance from network" ), QVariant(), u"INPUT"_s, true, 0 );
85 maxPointDistanceFromNetwork->setFlags( maxPointDistanceFromNetwork->flags() | Qgis::ProcessingParameterFlag::Advanced );
86 maxPointDistanceFromNetwork->setHelp(
87 QObject::tr( "Specifies an optional limit on the distance from the point to the network layer. If the point is further from the network than this distance an error will be raised." )
88 );
89 addParameter( maxPointDistanceFromNetwork.release() );
90
91 auto includeBounds = std::make_unique<QgsProcessingParameterBoolean>( u"INCLUDE_BOUNDS"_s, QObject::tr( "Include upper/lower bound points" ), false, true );
92 includeBounds->setFlags( includeBounds->flags() | Qgis::ProcessingParameterFlag::Advanced );
93 addParameter( includeBounds.release() );
94
95 auto outputLines = std::make_unique<QgsProcessingParameterFeatureSink>( u"OUTPUT_LINES"_s, QObject::tr( "Service area (lines)" ), Qgis::ProcessingSourceType::VectorLine, QVariant(), true );
96 outputLines->setCreateByDefault( true );
97 addParameter( outputLines.release() );
98
99 auto outputPoints = std::make_unique<QgsProcessingParameterFeatureSink>( u"OUTPUT"_s, QObject::tr( "Service area (boundary nodes)" ), Qgis::ProcessingSourceType::VectorPoint, QVariant(), true );
100 outputPoints->setCreateByDefault( false );
101 addParameter( outputPoints.release() );
102}
103
104QVariantMap QgsServiceAreaFromPointAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
105{
106 loadCommonParams( parameters, context, feedback );
107
108 const QgsPointXY startPoint = parameterAsPoint( parameters, u"START_POINT"_s, context, mNetwork->sourceCrs() );
109
110 // use older deprecated travel cost style if specified, to maintain old api
111 const bool useOldTravelCost = parameters.value( u"TRAVEL_COST"_s ).isValid();
112 double travelCost = parameterAsDouble( parameters, useOldTravelCost ? u"TRAVEL_COST"_s : u"TRAVEL_COST2"_s, context );
113
114 const int strategy = parameterAsInt( parameters, u"STRATEGY"_s, context );
115 if ( strategy && !useOldTravelCost )
116 travelCost *= mMultiplier;
117
118 bool includeBounds = true; // default to true to maintain 3.0 API
119 if ( parameters.contains( u"INCLUDE_BOUNDS"_s ) )
120 {
121 includeBounds = parameterAsBool( parameters, u"INCLUDE_BOUNDS"_s, context );
122 }
123
124 feedback->pushInfo( QObject::tr( "Building graph…" ) );
125 QVector<QgsPointXY> snappedPoints;
126 mDirector->makeGraph( mBuilder.get(), { startPoint }, snappedPoints, feedback );
127 const QgsPointXY snappedStartPoint = snappedPoints[0];
128
129 // check distance for the snapped point
130 if ( parameters.value( u"POINT_TOLERANCE"_s ).isValid() )
131 {
132 const double pointDistanceThreshold = parameterAsDouble( parameters, u"POINT_TOLERANCE"_s, context );
133
134 double distancePointToNetwork = 0;
135 try
136 {
137 distancePointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
138 }
139 catch ( QgsCsException & )
140 {
141 throw QgsProcessingException( QObject::tr( "An error occurred while calculating length" ) );
142 }
143
144
145 if ( distancePointToNetwork > pointDistanceThreshold )
146 {
147 throw QgsProcessingException( QObject::tr( "Point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distancePointToNetwork ).arg( pointDistanceThreshold ) );
148 }
149 }
150
151 feedback->pushInfo( QObject::tr( "Calculating service area…" ) );
152 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
153 const int idxStart = graph->findVertex( snappedStartPoint );
154
155 QVector<int> tree;
156 QVector<double> costs;
157 QgsGraphAnalyzer::dijkstra( graph.get(), idxStart, 0, &tree, &costs );
158
159 QgsMultiPointXY points;
160 QgsMultiPolylineXY lines;
161 QSet<int> vertices;
162
163 int inboundEdgeIndex;
164 double startVertexCost, endVertexCost;
165 QgsPointXY edgeStart, edgeEnd;
166 QgsGraphEdge edge;
167
168 for ( int i = 0; i < costs.size(); i++ )
169 {
170 inboundEdgeIndex = tree.at( i );
171 if ( inboundEdgeIndex == -1 && i != idxStart )
172 {
173 // unreachable vertex
174 continue;
175 }
176
177 startVertexCost = costs.at( i );
178 if ( startVertexCost > travelCost )
179 {
180 // vertex is too expensive, discard
181 continue;
182 }
183
184 vertices.insert( i );
185 edgeStart = graph->vertex( i ).point();
186
187 // find all edges coming from this vertex
188 const QList<int> outgoingEdges = graph->vertex( i ).outgoingEdges();
189 for ( const int edgeId : outgoingEdges )
190 {
191 edge = graph->edge( edgeId );
192 endVertexCost = startVertexCost + edge.cost( 0 ).toDouble();
193 edgeEnd = graph->vertex( edge.toVertex() ).point();
194 if ( endVertexCost <= travelCost )
195 {
196 // end vertex is cheap enough to include
197 vertices.insert( edge.toVertex() );
198 lines.push_back( QgsPolylineXY() << edgeStart << edgeEnd );
199 }
200 else
201 {
202 // travelCost sits somewhere on this edge, interpolate position
203 const QgsPointXY interpolatedEndPoint = QgsGeometryUtils::interpolatePointOnLineByValue( edgeStart.x(), edgeStart.y(), startVertexCost, edgeEnd.x(), edgeEnd.y(), endVertexCost, travelCost );
204
205 points.push_back( interpolatedEndPoint );
206 lines.push_back( QgsPolylineXY() << edgeStart << interpolatedEndPoint );
207 }
208 } // edges
209 } // costs
210
211 // convert to list and sort to maintain same order of points between algorithm runs
212 QList<int> verticesList = qgis::setToList( vertices );
213 points.reserve( verticesList.size() );
214 std::sort( verticesList.begin(), verticesList.end() );
215 for ( const int v : verticesList )
216 {
217 points.push_back( graph->vertex( v ).point() );
218 }
219
220 feedback->pushInfo( QObject::tr( "Writing results…" ) );
221
222 QVariantMap outputs;
223
224 QgsFields fields;
225 fields.append( QgsField( u"type"_s, QMetaType::Type::QString ) );
226 fields.append( QgsField( u"start"_s, QMetaType::Type::QString ) );
227
228 QgsFeature feat;
229 feat.setFields( fields );
230
231 QString pointsSinkId;
232 std::unique_ptr<QgsFeatureSink> pointsSink( parameterAsSink( parameters, u"OUTPUT"_s, context, pointsSinkId, fields, Qgis::WkbType::MultiPoint, mNetwork->sourceCrs() ) );
233
234 if ( pointsSink )
235 {
236 outputs.insert( u"OUTPUT"_s, pointsSinkId );
237
238 const QgsGeometry geomPoints = QgsGeometry::fromMultiPointXY( points );
239 feat.setGeometry( geomPoints );
240 feat.setAttributes( QgsAttributes() << u"within"_s << startPoint.toString() );
241 if ( !pointsSink->addFeature( feat, QgsFeatureSink::FastInsert ) )
242 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, u"OUTPUT"_s ) );
243
244 if ( includeBounds )
245 {
246 QgsMultiPointXY upperBoundary, lowerBoundary;
247 QVector<int> nodes;
248
249 int vertexId;
250 for ( int i = 0; i < costs.size(); i++ )
251 {
252 if ( costs.at( i ) > travelCost && tree.at( i ) != -1 )
253 {
254 vertexId = graph->edge( tree.at( i ) ).fromVertex();
255 if ( costs.at( vertexId ) <= travelCost )
256 {
257 nodes.push_back( i );
258 }
259 }
260 } // costs
261
262 upperBoundary.reserve( nodes.size() );
263 lowerBoundary.reserve( nodes.size() );
264 for ( const int i : nodes )
265 {
266 upperBoundary.push_back( graph->vertex( graph->edge( tree.at( i ) ).toVertex() ).point() );
267 lowerBoundary.push_back( graph->vertex( graph->edge( tree.at( i ) ).fromVertex() ).point() );
268 } // nodes
269
270 const QgsGeometry geomUpper = QgsGeometry::fromMultiPointXY( upperBoundary );
271 const QgsGeometry geomLower = QgsGeometry::fromMultiPointXY( lowerBoundary );
272
273 feat.setGeometry( geomUpper );
274 feat.setAttributes( QgsAttributes() << u"upper"_s << startPoint.toString() );
275 if ( !pointsSink->addFeature( feat, QgsFeatureSink::FastInsert ) )
276 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, u"OUTPUT"_s ) );
277
278 feat.setGeometry( geomLower );
279 feat.setAttributes( QgsAttributes() << u"lower"_s << startPoint.toString() );
280 if ( !pointsSink->addFeature( feat, QgsFeatureSink::FastInsert ) )
281 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, u"OUTPUT"_s ) );
282 } // includeBounds
283
284 pointsSink->finalize();
285 }
286
287 QString linesSinkId;
288 std::unique_ptr<QgsFeatureSink> linesSink( parameterAsSink( parameters, u"OUTPUT_LINES"_s, context, linesSinkId, fields, Qgis::WkbType::MultiLineString, mNetwork->sourceCrs() ) );
289
290 if ( linesSink )
291 {
292 outputs.insert( u"OUTPUT_LINES"_s, linesSinkId );
293 const QgsGeometry geomLines = QgsGeometry::fromMultiPolylineXY( lines );
294 feat.setGeometry( geomLines );
295 feat.setAttributes( QgsAttributes() << u"lines"_s << startPoint.toString() );
296 if ( !linesSink->addFeature( feat, QgsFeatureSink::FastInsert ) )
297 throw QgsProcessingException( writeFeatureError( linesSink.get(), parameters, u"OUTPUT_LINES"_s ) );
298 linesSink->finalize();
299 }
300
301 return outputs;
302}
303
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ MultiPoint
MultiPoint.
Definition qgis.h:300
@ MultiLineString
MultiLineString.
Definition qgis.h:301
@ Hidden
Parameter is hidden and should not be shown to users.
Definition qgis.h:3881
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
A vector of attributes.
Custom exception class for Coordinate Reference System related exceptions.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
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).
A geometry is the spatial representation of a feature.
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
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.
Represents an edge in a graph.
Definition qgsgraph.h:44
int toVertex() const
Returns the index of the vertex at the end of this edge.
Definition qgsgraph.cpp:185
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Definition qgsgraph.cpp:170
Represents a 2D point.
Definition qgspointxy.h:62
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A numeric parameter for processing algorithms.
A point parameter for processing algorithms.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition qgsgeometry.h:98
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition qgsgeometry.h:63