QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
21 
22 QString QgsPointToLayerAlgorithm::name() const
23 {
24  return QStringLiteral( "pointtolayer" );
25 }
26 
27 void QgsPointToLayerAlgorithm::initAlgorithm( const QVariantMap & )
28 {
29  addParameter( new QgsProcessingParameterPoint( QStringLiteral( "INPUT" ), QObject::tr( "Point" ) ) );
30  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Point" ), QgsProcessing::TypeVectorPoint ) );
31 }
32 
33 QString QgsPointToLayerAlgorithm::shortHelpString() const
34 {
35  return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching a point parameter.\n\n"
36  "It can be used in models to convert a point into a layer which can be used for other algorithms which require "
37  "a layer based input." );
38 }
39 
40 QgsPointToLayerAlgorithm *QgsPointToLayerAlgorithm::createInstance() const
41 {
42  return new QgsPointToLayerAlgorithm();
43 }
44 
45 QVariantMap QgsPointToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
46 {
47  QgsCoordinateReferenceSystem crs = parameterAsPointCrs( parameters, QStringLiteral( "INPUT" ), context );
48  QgsGeometry geom = QgsGeometry::fromPointXY( parameterAsPoint( parameters, QStringLiteral( "INPUT" ), context ) );
49 
50  QgsFields fields;
51  fields.append( QgsField( QStringLiteral( "id" ), QVariant::Int ) );
52 
53  QString dest;
54  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, crs ) );
55  if ( !sink )
56  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
57 
58  QgsFeature f;
59  f.setAttributes( QgsAttributes() << 1 );
60  f.setGeometry( geom );
61  sink->addFeature( f, QgsFeatureSink::FastInsert );
62 
63  feedback->setProgress( 100 );
64 
65  QVariantMap outputs;
66  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
67  return outputs;
68 }
69 
71 
QgsGeometry::fromPointXY
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
Definition: qgsgeometry.cpp:164
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFields
Definition: qgsfields.h:44
QgsFields::append
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)
Definition: qgsfields.cpp:59
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
qgsalgorithmpointtolayer.h
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsProcessingParameterPoint
Definition: qgsprocessingparameters.h:1535
QgsGeometry
Definition: qgsgeometry.h:122
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsField
Definition: qgsfield.h:49