QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmpointslayerfromtable.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmpointslayerfromtable.cpp
3 ---------------------
4 begin : November 2019
5 copyright : (C) 2019 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 "qgsvariantutils.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsPointsLayerFromTableAlgorithm::name() const
29{
30 return u"createpointslayerfromtable"_s;
31}
32
33QString QgsPointsLayerFromTableAlgorithm::displayName() const
34{
35 return QObject::tr( "Create points layer from table" );
36}
37
38QStringList QgsPointsLayerFromTableAlgorithm::tags() const
39{
40 return QObject::tr( "points,create,values,attributes" ).split( ',' );
41}
42
43QString QgsPointsLayerFromTableAlgorithm::group() const
44{
45 return QObject::tr( "Vector creation" );
46}
47
48QString QgsPointsLayerFromTableAlgorithm::groupId() const
49{
50 return u"vectorcreation"_s;
51}
52
53QString QgsPointsLayerFromTableAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm generates a point layer based on the coordinates from an input table." )
56 + u"\n\n"_s
57 + QObject::tr( "The table must contain a field with the X coordinate of each point and another "
58 "one with the Y coordinate, as well as optional fields with Z and M values. A CRS "
59 "for the output layer has to be specified, and the coordinates in the table are "
60 "assumed to be expressed in the units used by that CRS. The attributes table of "
61 "the resulting layer will be the input table." );
62}
63
64QString QgsPointsLayerFromTableAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Generates a point layer based on the coordinates from an input table." );
67}
68
69Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsLayerFromTableAlgorithm::documentationFlags() const
70{
72}
73
74QgsPointsLayerFromTableAlgorithm *QgsPointsLayerFromTableAlgorithm::createInstance() const
75{
76 return new QgsPointsLayerFromTableAlgorithm();
77}
78
79void QgsPointsLayerFromTableAlgorithm::initAlgorithm( const QVariantMap & )
80{
81 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
82 addParameter( new QgsProcessingParameterField( u"XFIELD"_s, QObject::tr( "X field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any ) );
83 addParameter( new QgsProcessingParameterField( u"YFIELD"_s, QObject::tr( "Y field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any ) );
84 addParameter( new QgsProcessingParameterField( u"ZFIELD"_s, QObject::tr( "Z field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
85 addParameter( new QgsProcessingParameterField( u"MFIELD"_s, QObject::tr( "M field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
86 addParameter( new QgsProcessingParameterCrs( u"TARGET_CRS"_s, QObject::tr( "Target CRS" ), u"EPSG:4326"_s ) );
87
88 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Points from table" ), Qgis::ProcessingSourceType::VectorPoint ) );
89}
90
91QVariantMap QgsPointsLayerFromTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
92{
93 std::unique_ptr<QgsProcessingFeatureSource> featureSource( parameterAsSource( parameters, u"INPUT"_s, context ) );
94 if ( !featureSource )
95 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
96
97 const QgsFields fields = featureSource->fields();
98 const QString xFieldName = parameterAsString( parameters, u"XFIELD"_s, context );
99 const int xFieldIndex = fields.lookupField( xFieldName );
100 if ( xFieldIndex < 0 )
101 throw QgsProcessingException( QObject::tr( "X field “%1” does not exist" ).arg( xFieldName ) );
102
103 const QString yFieldName = parameterAsString( parameters, u"YFIELD"_s, context );
104 const int yFieldIndex = fields.lookupField( yFieldName );
105 if ( yFieldIndex < 0 )
106 throw QgsProcessingException( QObject::tr( "Y field “%1” does not exist" ).arg( yFieldName ) );
107
108 QString fieldName = parameterAsString( parameters, u"ZFIELD"_s, context );
109 int zFieldIndex = -1;
110 if ( !fieldName.isEmpty() )
111 {
112 zFieldIndex = fields.lookupField( fieldName );
113 if ( zFieldIndex < 0 )
114 throw QgsProcessingException( QObject::tr( "Z field “%1” does not exist" ).arg( fieldName ) );
115 }
116
117 fieldName = parameterAsString( parameters, u"MFIELD"_s, context );
118 int mFieldIndex = -1;
119 if ( !fieldName.isEmpty() )
120 {
121 mFieldIndex = fields.lookupField( fieldName );
122 if ( mFieldIndex < 0 )
123 throw QgsProcessingException( QObject::tr( "M field “%1” does not exist" ).arg( fieldName ) );
124 }
125
126 Qgis::WkbType outputWkbType = Qgis::WkbType::Point;
127 if ( zFieldIndex >= 0 )
128 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
129 if ( mFieldIndex >= 0 )
130 outputWkbType = QgsWkbTypes::addM( outputWkbType );
131
132 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, u"TARGET_CRS"_s, context );
133
134 QString dest;
135 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, outputWkbType, crs, QgsFeatureSink::RegeneratePrimaryKey ) );
136 if ( !sink )
137 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
138
139 const double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
140
144 QgsFeature f;
145 int current = 0;
146
147 while ( fi.nextFeature( f ) )
148 {
149 if ( feedback->isCanceled() )
150 {
151 break;
152 }
153
154 const QgsAttributes attrs = f.attributes();
155
156 bool xOk = false;
157 bool yOk = false;
158 const double x = attrs.at( xFieldIndex ).toDouble( &xOk );
159 const double y = attrs.at( yFieldIndex ).toDouble( &yOk );
160
161 if ( !QgsVariantUtils::isNull( attrs.at( xFieldIndex ) ) && !QgsVariantUtils::isNull( attrs.at( yFieldIndex ) ) && xOk && yOk )
162 {
163 QgsPoint point( x, y );
164
165 if ( zFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( zFieldIndex ) ) )
166 point.addZValue( attrs.at( zFieldIndex ).toDouble() );
167
168 if ( mFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( mFieldIndex ) ) )
169 point.addMValue( attrs.at( mFieldIndex ).toDouble() );
170
171 f.setGeometry( QgsGeometry( point.clone() ) );
172 }
173
174 if ( !sink->addFeature( f ) )
175 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
176 feedback->setProgress( current * step );
177 current++;
178 }
179
180 sink->finalize();
181
182 QVariantMap outputs;
183 outputs.insert( u"OUTPUT"_s, dest );
184 return outputs;
185}
186
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2254
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3690
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Point
Point.
Definition qgis.h:282
A vector of attributes.
Represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
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 coordinate reference system parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.