QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmpointtolayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmpointtolayer.cpp
3 ---------------------
4 begin : May 2019
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson 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 <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsPointToLayerAlgorithm::name() const
27{
28 return u"pointtolayer"_s;
29}
30
31void QgsPointToLayerAlgorithm::initAlgorithm( const QVariantMap & )
32{
33 addParameter( new QgsProcessingParameterPoint( u"INPUT"_s, QObject::tr( "Point" ) ) );
34 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Point" ), Qgis::ProcessingSourceType::VectorPoint ) );
35}
36
37QString QgsPointToLayerAlgorithm::shortHelpString() const
38{
39 return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching a point parameter.\n\n"
40 "It can be used in models to convert a point into a layer which can be used for other algorithms which require "
41 "a layer based input." );
42}
43
44QString QgsPointToLayerAlgorithm::shortDescription() const
45{
46 return QObject::tr( "Creates a vector layer that contains a single feature with geometry matching a point parameter." );
47}
48
49QgsPointToLayerAlgorithm *QgsPointToLayerAlgorithm::createInstance() const
50{
51 return new QgsPointToLayerAlgorithm();
52}
53
54QVariantMap QgsPointToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
55{
56 const QgsCoordinateReferenceSystem crs = parameterAsPointCrs( parameters, u"INPUT"_s, context );
57 const QgsGeometry geom = QgsGeometry::fromPointXY( parameterAsPoint( parameters, u"INPUT"_s, context ) );
58
59 QgsFields fields;
60 fields.append( QgsField( u"id"_s, QMetaType::Type::Int ) );
61
62 QString dest;
63 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Point, crs ) );
64 if ( !sink )
65 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
66
67 QgsFeature f;
68 f.setAttributes( QgsAttributes() << 1 );
69 f.setGeometry( geom );
70 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
71 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
72
73 sink->finalize();
74
75 feedback->setProgress( 100 );
76
77 QVariantMap outputs;
78 outputs.insert( u"OUTPUT"_s, dest );
79 return outputs;
80}
81
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ Point
Point.
Definition qgis.h:282
A vector of attributes.
Represents a coordinate reference system (CRS).
@ 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 setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
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:76
A geometry is the spatial representation of a feature.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
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.
A feature sink output for processing algorithms.
A point parameter for processing algorithms.