QGIS API Documentation 3.99.0-Master (09f76ad7019)
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( "This algorithm takes a map layer and generates a new vector "
57 "layer with the minimum bounding box (rectangle polygon with "
58 "N-S orientation) that covers the input layer. Optionally, the "
59 "extent can be enlarged to a rounded value." );
60}
61
62QString QgsExtentFromLayerAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Generates a vector layer with the minimum bounding box that covers the input layer." );
65}
66
67QString QgsExtentFromLayerAlgorithm::svgIconPath() const
68{
69 return QgsApplication::iconPath( u"/algorithms/mAlgorithmExtractLayerExtent.svg"_s );
70}
71
72QIcon QgsExtentFromLayerAlgorithm::icon() const
73{
74 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmExtractLayerExtent.svg"_s );
75}
76
77QgsExtentFromLayerAlgorithm *QgsExtentFromLayerAlgorithm::createInstance() const
78{
79 return new QgsExtentFromLayerAlgorithm();
80}
81
82void QgsExtentFromLayerAlgorithm::initAlgorithm( const QVariantMap & )
83{
84 addParameter( new QgsProcessingParameterMapLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
85
86 auto roundParam = std::make_unique<QgsProcessingParameterDistance>( u"ROUND_TO"_s, QObject::tr( "Round values to" ), 0, u"INPUT"_s, 0 );
87 roundParam->setFlags( Qgis::ProcessingParameterFlag::Advanced );
88 addParameter( roundParam.release() );
89
90 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Extent" ), Qgis::ProcessingSourceType::VectorPolygon ) );
91}
92
93QVariantMap QgsExtentFromLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
94{
95 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
96
97 if ( !layer )
98 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
99
100 const double roundTo = parameterAsDouble( parameters, u"ROUND_TO"_s, context );
101
102 QgsFields fields;
103 fields.append( QgsField( u"MINX"_s, QMetaType::Type::Double ) );
104 fields.append( QgsField( u"MINY"_s, QMetaType::Type::Double ) );
105 fields.append( QgsField( u"MAXX"_s, QMetaType::Type::Double ) );
106 fields.append( QgsField( u"MAXY"_s, QMetaType::Type::Double ) );
107 fields.append( QgsField( u"CNTX"_s, QMetaType::Type::Double ) );
108 fields.append( QgsField( u"CNTY"_s, QMetaType::Type::Double ) );
109 fields.append( QgsField( u"AREA"_s, QMetaType::Type::Double ) );
110 fields.append( QgsField( u"PERIM"_s, QMetaType::Type::Double ) );
111 fields.append( QgsField( u"HEIGHT"_s, QMetaType::Type::Double ) );
112 fields.append( QgsField( u"WIDTH"_s, QMetaType::Type::Double ) );
113
114 QString dest;
115 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, layer->crs() ) );
116 if ( !sink )
117 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
118
119 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
120 {
121 vl->updateExtents();
122 }
123
124 QgsRectangle rect = layer->extent();
125
126 if ( roundTo > 0 )
127 {
128 rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo );
129 rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo );
130 rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo );
131 rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo );
132 }
133
134 const QgsGeometry geom = QgsGeometry::fromRect( rect );
135
136 const double minX = rect.xMinimum();
137 const double maxX = rect.xMaximum();
138 const double minY = rect.yMinimum();
139 const double maxY = rect.yMaximum();
140 const double height = rect.height();
141 const double width = rect.width();
142 const double cntX = minX + width / 2.0;
143 const double cntY = minY + height / 2.0;
144 const double area = width * height;
145 const double perim = 2 * width + 2 * height;
146
147 QgsFeature feat;
148 feat.setGeometry( geom );
149 feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width );
150 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
151 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
152
153 sink->finalize();
154
155 QVariantMap outputs;
156 outputs.insert( u"OUTPUT"_s, dest );
157 return outputs;
158}
159
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ Polygon
Polygon.
Definition qgis.h:284
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
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:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
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
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
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.
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.