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