QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#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( "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
60QgsExportLayersInformationAlgorithm *QgsExportLayersInformationAlgorithm::createInstance() const
61{
62 return new QgsExportLayersInformationAlgorithm();
63}
64
65bool QgsExportLayersInformationAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
66{
67 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
68 for ( QgsMapLayer *layer : layers )
69 {
70 if ( !mCrs.isValid() )
71 {
72 mCrs = layer->crs();
73 }
74 else if ( mCrs.authid() != QLatin1String( "EPSG:4326" ) )
75 {
76 if ( mCrs != layer->crs() )
77 {
78 // mixed CRSes, set output CRS to EPSG:4326
79 mCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
80 }
81 }
82 mLayers.emplace_back( layer->clone() );
83 }
84
85 if ( !mCrs.isValid() )
86 mCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
87
88 if ( mLayers.empty() )
89 feedback->reportError( QObject::tr( "No layers selected" ), false );
90
91 return true;
92}
93
94QVariantMap QgsExportLayersInformationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
95{
96 QgsFields outFields;
97 outFields.append( QgsField( QStringLiteral( "name" ), QMetaType::Type::QString ) );
98 outFields.append( QgsField( QStringLiteral( "source" ), QMetaType::Type::QString ) );
99 outFields.append( QgsField( QStringLiteral( "crs" ), QMetaType::Type::QString ) );
100 outFields.append( QgsField( QStringLiteral( "provider" ), QMetaType::Type::QString ) );
101 outFields.append( QgsField( QStringLiteral( "file_path" ), QMetaType::Type::QString ) );
102 outFields.append( QgsField( QStringLiteral( "layer_name" ), QMetaType::Type::QString ) );
103 outFields.append( QgsField( QStringLiteral( "subset" ), QMetaType::Type::QString ) );
104 outFields.append( QgsField( QStringLiteral( "abstract" ), QMetaType::Type::QString ) );
105 outFields.append( QgsField( QStringLiteral( "attribution" ), QMetaType::Type::QString ) );
106
107 QString outputDest;
108 std::unique_ptr<QgsFeatureSink> outputSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, outputDest, outFields, Qgis::WkbType::Polygon, mCrs ) );
109
110 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
111
112 const double step = layers.size() > 0 ? 100.0 / layers.size() : 1;
113 int i = 0;
114 for ( const std::unique_ptr<QgsMapLayer> &layer : mLayers )
115 {
116 i++;
117 if ( feedback->isCanceled() )
118 {
119 break;
120 }
121
122 feedback->setProgress( i * step );
123
124 QgsFeature feature;
125
126 QgsAttributes attributes;
127 attributes << layer->name()
128 << layer->source()
129 << layer->crs().authid();
130 if ( layer->dataProvider() )
131 {
132 const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->dataProvider()->name(), layer->source() );
133 attributes << layer->dataProvider()->name()
134 << parts[QStringLiteral( "path" )]
135 << parts[QStringLiteral( "layerName" )]
136 << parts[QStringLiteral( "subset" )];
137 }
138 else
139 {
140 attributes << QVariant() << QVariant() << QVariant() << QVariant();
141 }
142 attributes << layer->metadata().rights().join( ';' )
143 << layer->serverProperties()->abstract();
144 feature.setAttributes( attributes );
145
146 QgsRectangle rect = layer->extent();
147 if ( !rect.isEmpty() )
148 {
149 if ( layer->crs() != mCrs )
150 {
151 QgsCoordinateTransform transform( layer->crs(), mCrs, context.transformContext() );
152 transform.setBallparkTransformsAreAppropriate( true );
153 try
154 {
155 rect = transform.transformBoundingBox( rect );
156 }
157 catch ( QgsCsException &e )
158 {
159 Q_UNUSED( e )
160 rect = QgsRectangle();
161 feedback->pushInfo( QObject::tr( "Extent of layer %1 could not be reprojected" ).arg( layer->name() ) );
162 }
163 }
164 feature.setGeometry( QgsGeometry::fromRect( rect ) );
165 }
166 if ( !outputSink->addFeature( feature, QgsFeatureSink::FastInsert ) )
167 throw QgsProcessingException( writeFeatureError( outputSink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
168 }
169
170 outputSink->finalize();
171
172 QVariantMap outputs;
173 outputs.insert( QStringLiteral( "OUTPUT" ), outputDest );
174 return outputs;
175}
176
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
@ VectorPolygon
Vector polygon layers.
@ Polygon
Polygon.
A vector of attributes.
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.
@ 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:76
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.