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