QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmexportlayersinformation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmexportlayersinformation.cpp
3 ---------------------------------
4 begin : December 2020
5 copyright : (C) 2020 by Mathieu Pellerin
6 email : nirvn dot asia 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 "qgsproviderregistry.h"
20
22
23QString QgsExportLayersInformationAlgorithm::name() const
24{
25 return QStringLiteral( "exportlayersinformation" );
26}
27
28QString QgsExportLayersInformationAlgorithm::displayName() const
29{
30 return QObject::tr( "Export layer(s) information" );
31}
32
33QStringList QgsExportLayersInformationAlgorithm::tags() const
34{
35 return QObject::tr( "metadata,details,extent" ).split( ',' );
36}
37
38QString QgsExportLayersInformationAlgorithm::group() const
39{
40 return QObject::tr( "Layer tools" );
41}
42
43QString QgsExportLayersInformationAlgorithm::groupId() const
44{
45 return QStringLiteral( "layertools" );
46}
47
48void QgsExportLayersInformationAlgorithm::initAlgorithm( const QVariantMap & )
49{
50 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layer(s)" ), Qgis::ProcessingSourceType::MapLayer ) );
51 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output" ), Qgis::ProcessingSourceType::VectorPolygon, QVariant() ) );
52}
53
54QString QgsExportLayersInformationAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm creates a polygon layer with features corresponding to the extent of selected layer(s).\n\n"
57 "Additional layer details - CRS, provider name, file path, layer name, subset filter, abstract and attribution - are attached as attributes to each feature." );
58}
59
60QString QgsExportLayersInformationAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Creates a polygon layer with features corresponding to the extent of selected layer(s)." );
63}
64
65QgsExportLayersInformationAlgorithm *QgsExportLayersInformationAlgorithm::createInstance() const
66{
67 return new QgsExportLayersInformationAlgorithm();
68}
69
70bool QgsExportLayersInformationAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
71{
72 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
73 for ( QgsMapLayer *layer : layers )
74 {
75 if ( !mCrs.isValid() )
76 {
77 mCrs = layer->crs();
78 }
79 else if ( mCrs.authid() != QLatin1String( "EPSG:4326" ) )
80 {
81 if ( mCrs != layer->crs() )
82 {
83 // mixed CRSes, set output CRS to EPSG:4326
84 mCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
85 }
86 }
87 mLayers.emplace_back( layer->clone() );
88 }
89
90 if ( !mCrs.isValid() )
91 mCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
92
93 if ( mLayers.empty() )
94 feedback->reportError( QObject::tr( "No layers selected" ), false );
95
96 return true;
97}
98
99QVariantMap QgsExportLayersInformationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
100{
101 QgsFields outFields;
102 outFields.append( QgsField( QStringLiteral( "name" ), QMetaType::Type::QString ) );
103 outFields.append( QgsField( QStringLiteral( "source" ), QMetaType::Type::QString ) );
104 outFields.append( QgsField( QStringLiteral( "crs" ), QMetaType::Type::QString ) );
105 outFields.append( QgsField( QStringLiteral( "provider" ), QMetaType::Type::QString ) );
106 outFields.append( QgsField( QStringLiteral( "file_path" ), QMetaType::Type::QString ) );
107 outFields.append( QgsField( QStringLiteral( "layer_name" ), QMetaType::Type::QString ) );
108 outFields.append( QgsField( QStringLiteral( "subset" ), QMetaType::Type::QString ) );
109 outFields.append( QgsField( QStringLiteral( "abstract" ), QMetaType::Type::QString ) );
110 outFields.append( QgsField( QStringLiteral( "attribution" ), QMetaType::Type::QString ) );
111
112 QString outputDest;
113 std::unique_ptr<QgsFeatureSink> outputSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, outputDest, outFields, Qgis::WkbType::Polygon, mCrs ) );
114
115 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
116
117 const double step = layers.size() > 0 ? 100.0 / layers.size() : 1;
118 int i = 0;
119 for ( const std::unique_ptr<QgsMapLayer> &layer : mLayers )
120 {
121 i++;
122 if ( feedback->isCanceled() )
123 {
124 break;
125 }
126
127 feedback->setProgress( i * step );
128
129 QgsFeature feature;
130
131 QgsAttributes attributes;
132 attributes << layer->name()
133 << layer->source()
134 << layer->crs().authid();
135 if ( layer->dataProvider() )
136 {
137 const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->dataProvider()->name(), layer->source() );
138 attributes << layer->dataProvider()->name()
139 << parts[QStringLiteral( "path" )]
140 << parts[QStringLiteral( "layerName" )]
141 << parts[QStringLiteral( "subset" )];
142 }
143 else
144 {
145 attributes << QVariant() << QVariant() << QVariant() << QVariant();
146 }
147 attributes << layer->metadata().rights().join( ';' )
148 << layer->serverProperties()->abstract();
149 feature.setAttributes( attributes );
150
151 QgsRectangle rect = layer->extent();
152 if ( !rect.isEmpty() )
153 {
154 if ( layer->crs() != mCrs )
155 {
156 QgsCoordinateTransform transform( layer->crs(), mCrs, context.transformContext() );
157 transform.setBallparkTransformsAreAppropriate( true );
158 try
159 {
160 rect = transform.transformBoundingBox( rect );
161 }
162 catch ( QgsCsException &e )
163 {
164 Q_UNUSED( e )
165 rect = QgsRectangle();
166 feedback->pushInfo( QObject::tr( "Extent of layer %1 could not be reprojected" ).arg( layer->name() ) );
167 }
168 }
169 feature.setGeometry( QgsGeometry::fromRect( rect ) );
170 }
171 if ( !outputSink->addFeature( feature, QgsFeatureSink::FastInsert ) )
172 throw QgsProcessingException( writeFeatureError( outputSink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
173 }
174
175 outputSink->finalize();
176
177 QVariantMap outputs;
178 outputs.insert( QStringLiteral( "OUTPUT" ), outputDest );
179 return outputs;
180}
181
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
@ VectorPolygon
Vector polygon layers.
@ Polygon
Polygon.
A vector of attributes.
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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
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
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Base class for all map layer types.
Definition qgsmaplayer.h:77
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A feature sink output for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
A rectangle specified with double values.