25QString QgsServiceAreaFromPointAlgorithm::name()
const
27 return QStringLiteral(
"serviceareafrompoint" );
30QString QgsServiceAreaFromPointAlgorithm::displayName()
const
32 return QObject::tr(
"Service area (from point)" );
35QStringList QgsServiceAreaFromPointAlgorithm::tags()
const
37 return QObject::tr(
"network,service,area,shortest,fastest" ).split(
',' );
40QString QgsServiceAreaFromPointAlgorithm::shortHelpString()
const
42 return QObject::tr(
"This algorithm creates a new vector with all the edges or parts of edges "
43 "of a network line layer that can be reached within a distance or a time, "
44 "starting from a point feature. The distance and the time (both referred to "
45 "as \"travel cost\") must be specified respectively in the network layer "
46 "units or in hours." );
49QgsServiceAreaFromPointAlgorithm *QgsServiceAreaFromPointAlgorithm::createInstance()
const
51 return new QgsServiceAreaFromPointAlgorithm();
54void QgsServiceAreaFromPointAlgorithm::initAlgorithm(
const QVariantMap & )
59 std::unique_ptr<QgsProcessingParameterNumber> travelCost = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"TRAVEL_COST" ), QObject::tr(
"Travel cost (distance for 'Shortest', time for 'Fastest')" ),
Qgis::ProcessingNumberParameterType::Double, 0,
true, 0 );
61 addParameter( travelCost.release() );
65 std::unique_ptr<QgsProcessingParameterNumber> maxPointDistanceFromNetwork = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral(
"POINT_TOLERANCE" ), QObject::tr(
"Maximum point distance from network" ), QVariant(), QStringLiteral(
"INPUT" ),
true, 0 );
67 maxPointDistanceFromNetwork->setHelp( 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." ) );
68 addParameter( maxPointDistanceFromNetwork.release() );
70 std::unique_ptr<QgsProcessingParameterBoolean> includeBounds = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"INCLUDE_BOUNDS" ), QObject::tr(
"Include upper/lower bound points" ),
false,
true );
72 addParameter( includeBounds.release() );
74 std::unique_ptr<QgsProcessingParameterFeatureSink> outputLines = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral(
"OUTPUT_LINES" ), QObject::tr(
"Service area (lines)" ),
Qgis::ProcessingSourceType::VectorLine, QVariant(),
true );
75 outputLines->setCreateByDefault(
true );
76 addParameter( outputLines.release() );
78 std::unique_ptr<QgsProcessingParameterFeatureSink> outputPoints = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral(
"OUTPUT" ), QObject::tr(
"Service area (boundary nodes)" ),
Qgis::ProcessingSourceType::VectorPoint, QVariant(),
true );
79 outputPoints->setCreateByDefault(
false );
80 addParameter( outputPoints.release() );
85 loadCommonParams( parameters, context, feedback );
87 const QgsPointXY startPoint = parameterAsPoint( parameters, QStringLiteral(
"START_POINT" ), context, mNetwork->sourceCrs() );
90 const bool useOldTravelCost = parameters.value( QStringLiteral(
"TRAVEL_COST" ) ).isValid();
91 double travelCost = parameterAsDouble( parameters, useOldTravelCost ? QStringLiteral(
"TRAVEL_COST" ) : QStringLiteral(
"TRAVEL_COST2" ), context );
93 const int strategy = parameterAsInt( parameters, QStringLiteral(
"STRATEGY" ), context );
94 if ( strategy && !useOldTravelCost )
95 travelCost *= mMultiplier;
97 bool includeBounds =
true;
98 if ( parameters.contains( QStringLiteral(
"INCLUDE_BOUNDS" ) ) )
100 includeBounds = parameterAsBool( parameters, QStringLiteral(
"INCLUDE_BOUNDS" ), context );
103 feedback->
pushInfo( QObject::tr(
"Building graph…" ) );
104 QVector<QgsPointXY> snappedPoints;
105 mDirector->makeGraph( mBuilder.get(), { startPoint }, snappedPoints, feedback );
106 const QgsPointXY snappedStartPoint = snappedPoints[0];
109 if ( parameters.value( QStringLiteral(
"POINT_TOLERANCE" ) ).isValid() )
111 const double pointDistanceThreshold = parameterAsDouble( parameters, QStringLiteral(
"POINT_TOLERANCE" ), context );
113 double distancePointToNetwork = 0;
116 distancePointToNetwork = mBuilder->distanceArea()->measureLine( startPoint, snappedStartPoint );
124 if ( distancePointToNetwork > pointDistanceThreshold )
126 throw QgsProcessingException( QObject::tr(
"Point is too far from the network layer (%1, maximum permitted is %2)" ).arg( distancePointToNetwork ).arg( pointDistanceThreshold ) );
130 feedback->
pushInfo( QObject::tr(
"Calculating service area…" ) );
131 std::unique_ptr<QgsGraph> graph( mBuilder->takeGraph() );
132 const int idxStart = graph->findVertex( snappedStartPoint );
135 QVector<double> costs;
142 int inboundEdgeIndex;
143 double startVertexCost, endVertexCost;
147 for (
int i = 0; i < costs.size(); i++ )
149 inboundEdgeIndex = tree.at( i );
150 if ( inboundEdgeIndex == -1 && i != idxStart )
156 startVertexCost = costs.at( i );
157 if ( startVertexCost > travelCost )
163 vertices.insert( i );
164 edgeStart = graph->vertex( i ).point();
167 const QList<int> outgoingEdges = graph->vertex( i ).outgoingEdges();
168 for (
const int edgeId : outgoingEdges )
170 edge = graph->edge( edgeId );
171 endVertexCost = startVertexCost + edge.
cost( 0 ).toDouble();
172 edgeEnd = graph->vertex( edge.
toVertex() ).point();
173 if ( endVertexCost <= travelCost )
184 points.push_back( interpolatedEndPoint );
185 lines.push_back(
QgsPolylineXY() << edgeStart << interpolatedEndPoint );
191 QList<int> verticesList = qgis::setToList( vertices );
192 points.reserve( verticesList.size() );
193 std::sort( verticesList.begin(), verticesList.end() );
194 for (
const int v : verticesList )
196 points.push_back( graph->vertex( v ).point() );
199 feedback->
pushInfo( QObject::tr(
"Writing results…" ) );
204 fields.
append(
QgsField( QStringLiteral(
"type" ), QMetaType::Type::QString ) );
205 fields.
append(
QgsField( QStringLiteral(
"start" ), QMetaType::Type::QString ) );
210 QString pointsSinkId;
211 std::unique_ptr<QgsFeatureSink> pointsSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, pointsSinkId, fields,
Qgis::WkbType::MultiPoint, mNetwork->sourceCrs() ) );
215 outputs.insert( QStringLiteral(
"OUTPUT" ), pointsSinkId );
221 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
229 for (
int i = 0; i < costs.size(); i++ )
231 if ( costs.at( i ) > travelCost && tree.at( i ) != -1 )
233 vertexId = graph->edge( tree.at( i ) ).fromVertex();
234 if ( costs.at( vertexId ) <= travelCost )
236 nodes.push_back( i );
241 upperBoundary.reserve( nodes.size() );
242 lowerBoundary.reserve( nodes.size() );
243 for (
const int i : nodes )
245 upperBoundary.push_back( graph->vertex( graph->edge( tree.at( i ) ).toVertex() ).point() );
246 lowerBoundary.push_back( graph->vertex( graph->edge( tree.at( i ) ).fromVertex() ).point() );
255 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
260 throw QgsProcessingException( writeFeatureError( pointsSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
263 pointsSink->finalize();
267 std::unique_ptr<QgsFeatureSink> linesSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT_LINES" ), context, linesSinkId, fields,
Qgis::WkbType::MultiLineString, mNetwork->sourceCrs() ) );
271 outputs.insert( QStringLiteral(
"OUTPUT_LINES" ), linesSinkId );
276 throw QgsProcessingException( writeFeatureError( linesSink.get(), parameters, QStringLiteral(
"OUTPUT_LINES" ) ) );
277 linesSink->finalize();
@ VectorPoint
Vector point layers.
@ VectorLine
Vector line layers.
@ MultiLineString
MultiLineString.
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
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...
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.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
This class implements a graph edge.
int toVertex() const
Returns the index of the vertex at the end of this edge.
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
A class to represent a 2D point.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
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.
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.