QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
Loading...
Searching...
No Matches
qgsalgorithmextentfromlayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextentfromlayer.cpp
3 ---------------------
4 begin : November 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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 "qgsapplication.h"
21#include "qgsvectorlayer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsExtentFromLayerAlgorithm::name() const
30{
31 return u"polygonfromlayerextent"_s;
32}
33
34QString QgsExtentFromLayerAlgorithm::displayName() const
35{
36 return QObject::tr( "Extract layer extent" );
37}
38
39QStringList QgsExtentFromLayerAlgorithm::tags() const
40{
41 return QObject::tr( "polygon,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded" ).split( ',' );
42}
43
44QString QgsExtentFromLayerAlgorithm::group() const
45{
46 return QObject::tr( "Layer tools" );
47}
48
49QString QgsExtentFromLayerAlgorithm::groupId() const
50{
51 return u"layertools"_s;
52}
53
54QString QgsExtentFromLayerAlgorithm::shortHelpString() const
55{
56 return QObject::tr(
57 "This algorithm takes a map layer and generates a new vector "
58 "layer with the minimum bounding box (rectangle polygon with "
59 "N-S orientation) that covers the input layer. Optionally, the "
60 "extent can be enlarged to a rounded value."
61 );
62}
63
64QString QgsExtentFromLayerAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Generates a vector layer with the minimum bounding box that covers the input layer." );
67}
68
69QString QgsExtentFromLayerAlgorithm::svgIconPath() const
70{
71 return QgsApplication::iconPath( u"/algorithms/mAlgorithmExtractLayerExtent.svg"_s );
72}
73
74QIcon QgsExtentFromLayerAlgorithm::icon() const
75{
76 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmExtractLayerExtent.svg"_s );
77}
78
79QgsExtentFromLayerAlgorithm *QgsExtentFromLayerAlgorithm::createInstance() const
80{
81 return new QgsExtentFromLayerAlgorithm();
82}
83
84void QgsExtentFromLayerAlgorithm::initAlgorithm( const QVariantMap & )
85{
86 addParameter( new QgsProcessingParameterMapLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
87
88 auto roundParam = std::make_unique<QgsProcessingParameterDistance>( u"ROUND_TO"_s, QObject::tr( "Round values to" ), 0, u"INPUT"_s, 0 );
89 roundParam->setFlags( Qgis::ProcessingParameterFlag::Advanced );
90 addParameter( roundParam.release() );
91
92 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Extent" ), Qgis::ProcessingSourceType::VectorPolygon ) );
93}
94
95QVariantMap QgsExtentFromLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
96{
97 QGS_MARK_ALGORITHM_SOURCE
98
99 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
100
101 if ( !layer )
102 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
103
104 const double roundTo = parameterAsDouble( parameters, u"ROUND_TO"_s, context );
105
106 QgsFields fields;
107 fields.append( QgsField( u"MINX"_s, QMetaType::Type::Double ) );
108 fields.append( QgsField( u"MINY"_s, QMetaType::Type::Double ) );
109 fields.append( QgsField( u"MAXX"_s, QMetaType::Type::Double ) );
110 fields.append( QgsField( u"MAXY"_s, QMetaType::Type::Double ) );
111 fields.append( QgsField( u"CNTX"_s, QMetaType::Type::Double ) );
112 fields.append( QgsField( u"CNTY"_s, QMetaType::Type::Double ) );
113 fields.append( QgsField( u"AREA"_s, QMetaType::Type::Double ) );
114 fields.append( QgsField( u"PERIM"_s, QMetaType::Type::Double ) );
115 fields.append( QgsField( u"HEIGHT"_s, QMetaType::Type::Double ) );
116 fields.append( QgsField( u"WIDTH"_s, QMetaType::Type::Double ) );
117
118 QString dest;
119 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, layer->crs() ) );
120 if ( !sink )
121 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
122
123 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
124 {
125 vl->updateExtents();
126 }
127
128 QgsRectangle rect = layer->extent();
129
130 if ( roundTo > 0 )
131 {
132 rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo );
133 rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo );
134 rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo );
135 rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo );
136 }
137
138 const QgsGeometry geom = QgsGeometry::fromRect( rect );
139
140 const double minX = rect.xMinimum();
141 const double maxX = rect.xMaximum();
142 const double minY = rect.yMinimum();
143 const double maxY = rect.yMaximum();
144 const double height = rect.height();
145 const double width = rect.width();
146 const double cntX = minX + width / 2.0;
147 const double cntY = minY + height / 2.0;
148 const double area = width * height;
149 const double perim = 2 * width + 2 * height;
150
151 QgsFeature feat;
152 feat.setGeometry( geom );
153 feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width );
154 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
155 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
156 else
157 feedback->featureAddedToSink( u"OUTPUT"_s );
158
159 sink->finalize();
160 feedback->featureSinkFinalized( u"OUTPUT"_s );
161
162 QVariantMap outputs;
163 outputs.insert( u"OUTPUT"_s, dest );
164 return outputs;
165}
166
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3752
@ Polygon
Polygon.
Definition qgis.h:298
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3982
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
A vector of attributes.
@ 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.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
A geometry is the spatial representation of a feature.
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.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
A feature sink output for processing algorithms.
A map layer parameter for processing algorithms.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
Represents a vector layer which manages a vector based dataset.