QGIS API Documentation 4.3.0-Master (d3b565c628d)
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(
58 "The table must contain a field with the X coordinate of each point and another "
59 "one with the Y coordinate, as well as optional fields with Z and M values. A CRS "
60 "for the output layer has to be specified, and the coordinates in the table are "
61 "assumed to be expressed in the units used by that CRS. The attributes table of "
62 "the resulting layer will be the input table."
63 );
64}
65
66QString QgsPointsLayerFromTableAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Generates a point layer based on the coordinates from an input table." );
69}
70
71Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsLayerFromTableAlgorithm::documentationFlags() const
72{
74}
75
76QgsPointsLayerFromTableAlgorithm *QgsPointsLayerFromTableAlgorithm::createInstance() const
77{
78 return new QgsPointsLayerFromTableAlgorithm();
79}
80
81void QgsPointsLayerFromTableAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
84 addParameter( new QgsProcessingParameterField( u"XFIELD"_s, QObject::tr( "X field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any ) );
85 addParameter( new QgsProcessingParameterField( u"YFIELD"_s, QObject::tr( "Y field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any ) );
86 addParameter( new QgsProcessingParameterField( u"ZFIELD"_s, QObject::tr( "Z field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
87 addParameter( new QgsProcessingParameterField( u"MFIELD"_s, QObject::tr( "M field" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
88 addParameter( new QgsProcessingParameterCrs( u"TARGET_CRS"_s, QObject::tr( "Target CRS" ), u"EPSG:4326"_s ) );
89
90 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Points from table" ), Qgis::ProcessingSourceType::VectorPoint ) );
91}
92
93QVariantMap QgsPointsLayerFromTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94{
95 QGS_MARK_ALGORITHM_SOURCE
96
97 std::unique_ptr<QgsProcessingFeatureSource> featureSource( parameterAsSource( parameters, u"INPUT"_s, context ) );
98 if ( !featureSource )
99 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
100
101 const QgsFields fields = featureSource->fields();
102 const QString xFieldName = parameterAsString( parameters, u"XFIELD"_s, context );
103 const int xFieldIndex = fields.lookupField( xFieldName );
104 if ( xFieldIndex < 0 )
105 throw QgsProcessingException( QObject::tr( "X field “%1” does not exist" ).arg( xFieldName ) );
106
107 const QString yFieldName = parameterAsString( parameters, u"YFIELD"_s, context );
108 const int yFieldIndex = fields.lookupField( yFieldName );
109 if ( yFieldIndex < 0 )
110 throw QgsProcessingException( QObject::tr( "Y field “%1” does not exist" ).arg( yFieldName ) );
111
112 QString fieldName = parameterAsString( parameters, u"ZFIELD"_s, context );
113 int zFieldIndex = -1;
114 if ( !fieldName.isEmpty() )
115 {
116 zFieldIndex = fields.lookupField( fieldName );
117 if ( zFieldIndex < 0 )
118 throw QgsProcessingException( QObject::tr( "Z field “%1” does not exist" ).arg( fieldName ) );
119 }
120
121 fieldName = parameterAsString( parameters, u"MFIELD"_s, context );
122 int mFieldIndex = -1;
123 if ( !fieldName.isEmpty() )
124 {
125 mFieldIndex = fields.lookupField( fieldName );
126 if ( mFieldIndex < 0 )
127 throw QgsProcessingException( QObject::tr( "M field “%1” does not exist" ).arg( fieldName ) );
128 }
129
130 Qgis::WkbType outputWkbType = Qgis::WkbType::Point;
131 if ( zFieldIndex >= 0 )
132 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
133 if ( mFieldIndex >= 0 )
134 outputWkbType = QgsWkbTypes::addM( outputWkbType );
135
136 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, u"TARGET_CRS"_s, context );
137
138 QString dest;
139 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, outputWkbType, crs, QgsFeatureSink::RegeneratePrimaryKey ) );
140 if ( !sink )
141 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
142
143 const double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
144
148 QgsFeature f;
149 int current = 0;
150
151 while ( fi.nextFeature( f ) )
152 {
153 if ( feedback->isCanceled() )
154 {
155 break;
156 }
157
158 const QgsAttributes attrs = f.attributes();
159
160 bool xOk = false;
161 bool yOk = false;
162 const double x = attrs.at( xFieldIndex ).toDouble( &xOk );
163 const double y = attrs.at( yFieldIndex ).toDouble( &yOk );
164
165 if ( !QgsVariantUtils::isNull( attrs.at( xFieldIndex ) ) && !QgsVariantUtils::isNull( attrs.at( yFieldIndex ) ) && xOk && yOk )
166 {
167 QgsPoint point( x, y );
168
169 if ( zFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( zFieldIndex ) ) )
170 point.addZValue( attrs.at( zFieldIndex ).toDouble() );
171
172 if ( mFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( mFieldIndex ) ) )
173 point.addMValue( attrs.at( mFieldIndex ).toDouble() );
174
175 f.setGeometry( QgsGeometry( point.clone() ) );
176 }
177
178 if ( !sink->addFeature( f ) )
179 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
180 else
181 feedback->featureAddedToSink( u"OUTPUT"_s );
182 feedback->setProgress( current * step );
183 current++;
184 }
185
186 sink->finalize();
187 feedback->featureSinkFinalized( u"OUTPUT"_s );
188
189 QVariantMap outputs;
190 outputs.insert( u"OUTPUT"_s, dest );
191 return outputs;
192}
193
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3755
@ VectorPoint
Vector point layers.
Definition qgis.h:3750
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2360
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3836
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3847
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3930
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
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:64
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
Container of fields for a vector layer.
Definition qgsfields.h:45
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.