QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
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
20#include "qgsproviderregistry.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsExportLayersInformationAlgorithm::name() const
29{
30 return u"exportlayersinformation"_s;
31}
32
33QString QgsExportLayersInformationAlgorithm::displayName() const
34{
35 return QObject::tr( "Export layer(s) information" );
36}
37
38QStringList QgsExportLayersInformationAlgorithm::tags() const
39{
40 return QObject::tr( "metadata,details,extent" ).split( ',' );
41}
42
43QString QgsExportLayersInformationAlgorithm::group() const
44{
45 return QObject::tr( "Layer tools" );
46}
47
48QString QgsExportLayersInformationAlgorithm::groupId() const
49{
50 return u"layertools"_s;
51}
52
53void QgsExportLayersInformationAlgorithm::initAlgorithm( const QVariantMap & )
54{
55 addParameter( new QgsProcessingParameterMultipleLayers( u"LAYERS"_s, QObject::tr( "Input layer(s)" ), Qgis::ProcessingSourceType::MapLayer ) );
56 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Output" ), Qgis::ProcessingSourceType::VectorPolygon, QVariant() ) );
57}
58
59QString QgsExportLayersInformationAlgorithm::shortHelpString() const
60{
61 return QObject::tr( "This algorithm creates a polygon layer with features corresponding to the extent of selected layer(s).\n\n"
62 "Additional layer details - CRS, provider name, file path, layer name, subset filter, abstract and attribution - are attached as attributes to each feature." );
63}
64
65QString QgsExportLayersInformationAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Creates a polygon layer with features corresponding to the extent of selected layer(s)." );
68}
69
70QgsExportLayersInformationAlgorithm *QgsExportLayersInformationAlgorithm::createInstance() const
71{
72 return new QgsExportLayersInformationAlgorithm();
73}
74
75bool QgsExportLayersInformationAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76{
77 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"LAYERS"_s, context );
78 for ( QgsMapLayer *layer : layers )
79 {
80 if ( !mCrs.isValid() )
81 {
82 mCrs = layer->crs();
83 }
84 else if ( mCrs.authid() != "EPSG:4326"_L1 )
85 {
86 if ( mCrs != layer->crs() )
87 {
88 // mixed CRSes, set output CRS to EPSG:4326
89 mCrs = QgsCoordinateReferenceSystem( u"EPSG:4326"_s );
90 }
91 }
92 mLayers.emplace_back( layer->clone() );
93 }
94
95 if ( !mCrs.isValid() )
96 mCrs = QgsCoordinateReferenceSystem( u"EPSG:4326"_s );
97
98 if ( mLayers.empty() )
99 feedback->reportError( QObject::tr( "No layers selected" ), false );
100
101 return true;
102}
103
104QVariantMap QgsExportLayersInformationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
105{
106 QgsFields outFields;
107 outFields.append( QgsField( u"name"_s, QMetaType::Type::QString ) );
108 outFields.append( QgsField( u"source"_s, QMetaType::Type::QString ) );
109 outFields.append( QgsField( u"crs"_s, QMetaType::Type::QString ) );
110 outFields.append( QgsField( u"provider"_s, QMetaType::Type::QString ) );
111 outFields.append( QgsField( u"file_path"_s, QMetaType::Type::QString ) );
112 outFields.append( QgsField( u"layer_name"_s, QMetaType::Type::QString ) );
113 outFields.append( QgsField( u"subset"_s, QMetaType::Type::QString ) );
114 outFields.append( QgsField( u"abstract"_s, QMetaType::Type::QString ) );
115 outFields.append( QgsField( u"attribution"_s, QMetaType::Type::QString ) );
116
117 QString outputDest;
118 std::unique_ptr<QgsFeatureSink> outputSink( parameterAsSink( parameters, u"OUTPUT"_s, context, outputDest, outFields, Qgis::WkbType::Polygon, mCrs ) );
119
120 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"LAYERS"_s, context );
121
122 const double step = layers.size() > 0 ? 100.0 / layers.size() : 1;
123 int i = 0;
124 for ( const std::unique_ptr<QgsMapLayer> &layer : mLayers )
125 {
126 i++;
127 if ( feedback->isCanceled() )
128 {
129 break;
130 }
131
132 feedback->setProgress( i * step );
133
134 QgsFeature feature;
135
136 QgsAttributes attributes;
137 attributes << layer->name()
138 << layer->source()
139 << layer->crs().authid();
140 if ( layer->dataProvider() )
141 {
142 const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->dataProvider()->name(), layer->source() );
143 attributes << layer->dataProvider()->name()
144 << parts[u"path"_s]
145 << parts[u"layerName"_s]
146 << parts[u"subset"_s];
147 }
148 else
149 {
150 attributes << QVariant() << QVariant() << QVariant() << QVariant();
151 }
152 attributes << layer->metadata().rights().join( ';' )
153 << layer->serverProperties()->abstract();
154 feature.setAttributes( attributes );
155
156 QgsRectangle rect = layer->extent();
157 if ( !rect.isEmpty() )
158 {
159 if ( layer->crs() != mCrs )
160 {
161 QgsCoordinateTransform transform( layer->crs(), mCrs, context.transformContext() );
162 transform.setBallparkTransformsAreAppropriate( true );
163 try
164 {
165 rect = transform.transformBoundingBox( rect );
166 }
167 catch ( QgsCsException &e )
168 {
169 Q_UNUSED( e )
170 rect = QgsRectangle();
171 feedback->pushInfo( QObject::tr( "Extent of layer %1 could not be reprojected" ).arg( layer->name() ) );
172 }
173 }
174 feature.setGeometry( QgsGeometry::fromRect( rect ) );
175 }
176 if ( !outputSink->addFeature( feature, QgsFeatureSink::FastInsert ) )
177 throw QgsProcessingException( writeFeatureError( outputSink.get(), parameters, u"OUTPUT"_s ) );
178 }
179
180 outputSink->finalize();
181
182 QVariantMap outputs;
183 outputs.insert( u"OUTPUT"_s, outputDest );
184 return outputs;
185}
186
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
Definition qgis.h:3603
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ Polygon
Polygon.
Definition qgis.h:284
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:60
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:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
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:76
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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.