QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmbatchgeocode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmbatchgeocode.cpp
3 ------------------
4 begin : August 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson 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 "qgsgeocoder.h"
20#include "qgsgeocoderresult.h"
21#include "qgsgeocodercontext.h"
22#include "qgsvectorlayer.h"
23
29
31{
32 return QObject::tr( "geocode" ).split( ',' );
33}
34
36{
37 return QObject::tr( "Vector general" );
38}
39
41{
42 return QStringLiteral( "vectorgeneral" );
43}
44
45void QgsBatchGeocodeAlgorithm::initParameters( const QVariantMap &configuration )
46{
47 mIsInPlace = configuration.value( QStringLiteral( "IN_PLACE" ) ).toBool();
48
49 addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Address field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::String ) );
50
51 if ( mIsInPlace )
52 {
53 const QgsFields newFields = mGeocoder->appendedFields();
54 for ( const QgsField &newField : newFields )
55 addParameter( new QgsProcessingParameterField( newField.name(), QObject::tr( "%1 field" ).arg( newField.name() ), newField.name(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
56 }
57}
58
60{
61 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
62}
63
65{
66 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
67 {
68 return vl->geometryType() == Qgis::GeometryType::Point;
69 }
70 return false;
71}
72
74{
75 return QObject::tr( "Geocoded" );
76}
77
79{
80 mAddressField = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
81
82 if ( mIsInPlace )
83 {
84 const QgsFields newFields = mGeocoder->appendedFields();
85 for ( const QgsField &newField : newFields )
86 mInPlaceFieldMap.insert( newField.name(), parameterAsString( parameters, newField.name(), context ) );
87 }
88
89 return true;
90}
91
96
98{
99 mOutputCrs = inputCrs;
100 return mOutputCrs;
101}
102
104{
105 if ( !mIsInPlace )
106 {
107 // append any additional fields created by the geocoder
108 const QgsFields newFields = mGeocoder->appendedFields();
109 mAdditionalFields = newFields.names();
110
111 return QgsProcessingUtils::combineFields( inputFields, newFields );
112 }
113 else
114 {
115 return inputFields;
116 }
117}
118
120{
121 QgsFeature f = feature;
122
123 const QString address = f.attribute( mAddressField ).toString();
124 if ( address.isEmpty() )
125 {
126 f.padAttributes( mAdditionalFields.count() );
127 feedback->pushWarning( QObject::tr( "Empty address field for feature %1" ).arg( feature.id() ) );
128 return QgsFeatureList() << f;
129 }
130
131 const QgsGeocoderContext geocodeContext( context.transformContext() );
132 const QList<QgsGeocoderResult> results = mGeocoder->geocodeString( address, geocodeContext, feedback );
133 if ( results.empty() )
134 {
135 f.padAttributes( mAdditionalFields.count() );
136 feedback->pushWarning( QObject::tr( "No result for %1" ).arg( address ) );
137 return QgsFeatureList() << f;
138 }
139
140 if ( !results.at( 0 ).isValid() )
141 {
142 f.padAttributes( mAdditionalFields.count() );
143 feedback->reportError( QObject::tr( "Error geocoding %1: %2" ).arg( address, results.at( 0 ).error() ) );
144 return QgsFeatureList() << f;
145 }
146
147 QgsAttributes attr = f.attributes();
148 const QVariantMap additionalAttributes = results.at( 0 ).additionalAttributes();
149 if ( !mIsInPlace )
150 {
151 for ( const QString &additionalField : mAdditionalFields )
152 {
153 attr.append( additionalAttributes.value( additionalField ) );
154 }
155 f.setAttributes( attr );
156 }
157 else
158 {
159 for ( auto it = mInPlaceFieldMap.constBegin(); it != mInPlaceFieldMap.constEnd(); ++it )
160 {
161 if ( !it.value().isEmpty() )
162 {
163 f.setAttribute( it.value(), additionalAttributes.value( it.key() ) );
164 }
165 }
166 }
167
168 const QgsCoordinateTransform transform = QgsCoordinateTransform( results.at( 0 ).crs(), mOutputCrs, context.transformContext() );
169 QgsGeometry g = results.at( 0 ).geometry();
170 try
171 {
172 g.transform( transform );
173 }
174 catch ( QgsCsException & )
175 {
176 feedback->reportError( QObject::tr( "Error transforming %1 to layer CRS" ).arg( address ) );
177 return QgsFeatureList() << f;
178 }
179
180 f.setGeometry( g );
181 return QgsFeatureList() << f;
182}
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
A vector of attributes.
bool prepareAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) override
Prepares the algorithm to run using the specified parameters.
QgsFeatureList processFeature(const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback) override
Processes an individual input feature from the source.
void initParameters(const QVariantMap &configuration=QVariantMap()) override
Initializes any extra parameters added by the algorithm subclass.
QString group() const override
Returns the name of the group this algorithm belongs to.
QString outputName() const override
Returns the translated, user visible name for any layers created by this algorithm.
QgsCoordinateReferenceSystem outputCrs(const QgsCoordinateReferenceSystem &inputCrs) const override
Maps the input source coordinate reference system (inputCrs) to a corresponding output CRS generated ...
QgsBatchGeocodeAlgorithm(QgsGeocoderInterface *geocoder)
Constructor for QgsBatchGeocodeAlgorithm.
QgsFields outputFields(const QgsFields &inputFields) const override
Maps the input source fields (inputFields) to corresponding output fields generated by the algorithm.
QString groupId() const override
Returns the unique ID of the group this algorithm belongs to.
QStringList tags() const override
Returns a list of tags which relate to the algorithm, and are used to assist users in searching for s...
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation ret...
Qgis::WkbType outputWkbType(Qgis::WkbType inputWkbType) const override
Maps the input WKB geometry type (inputWkbType) to the corresponding output WKB type generated by the...
QList< int > inputLayerTypes() const override
Returns the valid input layer types for the source layer for this algorithm.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void padAttributes(int count)
Resizes the attributes attached to this feature by appending the specified count of NULL values to th...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
QStringList names
Definition qgsfields.h:51
Encapsulates the context of a geocoding operation.
Interface for geocoders.
Definition qgsgeocoder.h:37
virtual QList< QgsGeocoderResult > geocodeString(const QString &string, const QgsGeocoderContext &context, QgsFeedback *feedback=nullptr) const
Geocodes a string.
virtual QgsFields appendedFields() const
Returns a set of newly created fields which will be appended to existing features during the geocode ...
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
Base class for all map layer types.
Definition qgsmaplayer.h:76
bool addParameter(QgsProcessingParameterDefinition *parameterDefinition, bool createOutput=true)
Adds a parameter definition to the algorithm.
QString parameterAsString(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static string value.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
An abstract QgsProcessingAlgorithm base class for processing algorithms which operate "feature-by-fea...
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A vector layer or feature source field parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Represents a vector layer which manages a vector based data sets.
QList< QgsFeature > QgsFeatureList