24 QString QgsAddXYFieldsAlgorithm::name()
const
26 return QStringLiteral(
"addxyfields" );
29 QString QgsAddXYFieldsAlgorithm::displayName()
const
31 return QObject::tr(
"Add X/Y fields to layer" );
34 QString QgsAddXYFieldsAlgorithm::shortHelpString()
const
36 return QObject::tr(
"Adds X and Y (or latitude/longitude) fields to a point layer. The X/Y fields can be calculated in a different CRS to the layer (e.g. creating latitude/longitude fields for a layer in a project CRS)." );
39 QString QgsAddXYFieldsAlgorithm::shortDescription()
const
41 return QObject::tr(
"Adds X and Y (or latitude/longitude) fields to a point layer." );
44 QStringList QgsAddXYFieldsAlgorithm::tags()
const
46 return QObject::tr(
"add,create,latitude,longitude,columns,attributes" ).split(
',' );
49 QString QgsAddXYFieldsAlgorithm::group()
const
51 return QObject::tr(
"Vector table" );
54 QString QgsAddXYFieldsAlgorithm::groupId()
const
56 return QStringLiteral(
"vectortable" );
59 QString QgsAddXYFieldsAlgorithm::outputName()
const
61 return QObject::tr(
"Added fields" );
64 QList<int> QgsAddXYFieldsAlgorithm::inputLayerTypes()
const
69 QgsAddXYFieldsAlgorithm *QgsAddXYFieldsAlgorithm::createInstance()
const
71 return new QgsAddXYFieldsAlgorithm();
79 void QgsAddXYFieldsAlgorithm::initParameters(
const QVariantMap &configuration )
81 mIsInPlace = configuration.value( QStringLiteral(
"IN_PLACE" ) ).toBool();
83 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"CRS" ), QObject::tr(
"Coordinate system" ), QStringLiteral(
"EPSG:4326" ) ) );
89 addParameter(
new QgsProcessingParameterField( QStringLiteral(
"FIELD_X" ), QObject::tr(
"X field" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
90 addParameter(
new QgsProcessingParameterField( QStringLiteral(
"FIELD_Y" ), QObject::tr(
"Y field" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
98 mInPlaceXFieldIndex = inputFields.
lookupField( mInPlaceXField );
99 mInPlaceYFieldIndex = inputFields.
lookupField( mInPlaceYField );
104 const QString xFieldName = mPrefix +
'x';
105 const QString yFieldName = mPrefix +
'y';
108 outFields.
append(
QgsField( xFieldName, QVariant::Double, QString(), 20, 10 ) );
109 outFields.
append(
QgsField( yFieldName, QVariant::Double, QString(), 20, 10 ) );
116 mSourceCrs = inputCrs;
123 mPrefix = parameterAsString( parameters, QStringLiteral(
"PREFIX" ), context );
126 mInPlaceXField = parameterAsString( parameters, QStringLiteral(
"FIELD_X" ), context );
127 mInPlaceYField = parameterAsString( parameters, QStringLiteral(
"FIELD_Y" ), context );
130 mCrs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
136 if ( mTransformNeedsInitialization )
139 mTransformNeedsInitialization =
false;
141 if ( mIsInPlace && mInPlaceXFieldIndex == -1 )
151 throw QgsProcessingException( QObject::tr(
"Multipoint features are not supported - please convert to single point features first." ) );
156 const QgsPointXY transformed = mTransform.transform( point );
162 feedback->
reportError( QObject::tr(
"Could not transform point to destination CRS" ) );
169 attributes << x << y;
173 attributes[mInPlaceXFieldIndex] = x;
174 attributes[mInPlaceYFieldIndex] = y;
180 bool QgsAddXYFieldsAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
182 if (
const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer * >( layer ) )