QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
23
24QString QgsPointsLayerFromTableAlgorithm::name() const
25{
26 return QStringLiteral( "createpointslayerfromtable" );
27}
28
29QString QgsPointsLayerFromTableAlgorithm::displayName() const
30{
31 return QObject::tr( "Create points layer from table" );
32}
33
34QStringList QgsPointsLayerFromTableAlgorithm::tags() const
35{
36 return QObject::tr( "points,create,values,attributes" ).split( ',' );
37}
38
39QString QgsPointsLayerFromTableAlgorithm::group() const
40{
41 return QObject::tr( "Vector creation" );
42}
43
44QString QgsPointsLayerFromTableAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorcreation" );
47}
48
49QString QgsPointsLayerFromTableAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm generates a point layer based on the coordinates from an input table." )
52 + QStringLiteral( "\n\n" )
53 + QObject::tr( "The table must contain a field with the X coordinate of each point and another "
54 "one with the Y coordinate, as well as optional fields with Z and M values. A CRS "
55 "for the output layer has to be specified, and the coordinates in the table are "
56 "assumed to be expressed in the units used by that CRS. The attributes table of "
57 "the resulting layer will be the input table." );
58}
59
60QString QgsPointsLayerFromTableAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Generates a point layer based on the coordinates from an input table." );
63}
64
65Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsLayerFromTableAlgorithm::documentationFlags() const
66{
68}
69
70QgsPointsLayerFromTableAlgorithm *QgsPointsLayerFromTableAlgorithm::createInstance() const
71{
72 return new QgsPointsLayerFromTableAlgorithm();
73}
74
75void QgsPointsLayerFromTableAlgorithm::initAlgorithm( const QVariantMap & )
76{
77 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
78 addParameter( new QgsProcessingParameterField( QStringLiteral( "XFIELD" ), QObject::tr( "X field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any ) );
79 addParameter( new QgsProcessingParameterField( QStringLiteral( "YFIELD" ), QObject::tr( "Y field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any ) );
80 addParameter( new QgsProcessingParameterField( QStringLiteral( "ZFIELD" ), QObject::tr( "Z field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
81 addParameter( new QgsProcessingParameterField( QStringLiteral( "MFIELD" ), QObject::tr( "M field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
82 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
83
84 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Points from table" ), Qgis::ProcessingSourceType::VectorPoint ) );
85}
86
87QVariantMap QgsPointsLayerFromTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
88{
89 std::unique_ptr<QgsProcessingFeatureSource> featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
90 if ( !featureSource )
91 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
92
93 const QgsFields fields = featureSource->fields();
94 const QString xFieldName = parameterAsString( parameters, QStringLiteral( "XFIELD" ), context );
95 const int xFieldIndex = fields.lookupField( xFieldName );
96 if ( xFieldIndex < 0 )
97 throw QgsProcessingException( QObject::tr( "X field “%1” does not exist" ).arg( xFieldName ) );
98
99 const QString yFieldName = parameterAsString( parameters, QStringLiteral( "YFIELD" ), context );
100 const int yFieldIndex = fields.lookupField( yFieldName );
101 if ( yFieldIndex < 0 )
102 throw QgsProcessingException( QObject::tr( "Y field “%1” does not exist" ).arg( yFieldName ) );
103
104 QString fieldName = parameterAsString( parameters, QStringLiteral( "ZFIELD" ), context );
105 int zFieldIndex = -1;
106 if ( !fieldName.isEmpty() )
107 {
108 zFieldIndex = fields.lookupField( fieldName );
109 if ( zFieldIndex < 0 )
110 throw QgsProcessingException( QObject::tr( "Z field “%1” does not exist" ).arg( fieldName ) );
111 }
112
113 fieldName = parameterAsString( parameters, QStringLiteral( "MFIELD" ), context );
114 int mFieldIndex = -1;
115 if ( !fieldName.isEmpty() )
116 {
117 mFieldIndex = fields.lookupField( fieldName );
118 if ( mFieldIndex < 0 )
119 throw QgsProcessingException( QObject::tr( "M field “%1” does not exist" ).arg( fieldName ) );
120 }
121
122 Qgis::WkbType outputWkbType = Qgis::WkbType::Point;
123 if ( zFieldIndex >= 0 )
124 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
125 if ( mFieldIndex >= 0 )
126 outputWkbType = QgsWkbTypes::addM( outputWkbType );
127
128 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
129
130 QString dest;
131 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, outputWkbType, crs, QgsFeatureSink::RegeneratePrimaryKey ) );
132 if ( !sink )
133 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
134
135 const double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
136
140 QgsFeature f;
141 int current = 0;
142
143 while ( fi.nextFeature( f ) )
144 {
145 if ( feedback->isCanceled() )
146 {
147 break;
148 }
149
150 const QgsAttributes attrs = f.attributes();
151
152 bool xOk = false;
153 bool yOk = false;
154 const double x = attrs.at( xFieldIndex ).toDouble( &xOk );
155 const double y = attrs.at( yFieldIndex ).toDouble( &yOk );
156
157 if ( !QgsVariantUtils::isNull( attrs.at( xFieldIndex ) ) && !QgsVariantUtils::isNull( attrs.at( yFieldIndex ) ) && xOk && yOk )
158 {
159 QgsPoint point( x, y );
160
161 if ( zFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( zFieldIndex ) ) )
162 point.addZValue( attrs.at( zFieldIndex ).toDouble() );
163
164 if ( mFieldIndex >= 0 && !QgsVariantUtils::isNull( attrs.at( mFieldIndex ) ) )
165 point.addMValue( attrs.at( mFieldIndex ).toDouble() );
166
167 f.setGeometry( QgsGeometry( point.clone() ) );
168 }
169
170 if ( !sink->addFeature( f ) )
171 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
172 feedback->setProgress( current * step );
173 current++;
174 }
175
176 sink->finalize();
177
178 QVariantMap outputs;
179 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
180 return outputs;
181}
182
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ VectorPoint
Vector point layers.
Definition qgis.h:3534
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2196
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3619
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3630
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ Point
Point.
Definition qgis.h:279
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:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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:49
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.