QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 * )
96{
97 QgsMapLayer *layer = parameterAsLayer( parameters, u"INPUT"_s, context );
98
99 if ( !layer )
100 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
101
102 const double roundTo = parameterAsDouble( parameters, u"ROUND_TO"_s, context );
103
104 QgsFields fields;
105 fields.append( QgsField( u"MINX"_s, QMetaType::Type::Double ) );
106 fields.append( QgsField( u"MINY"_s, QMetaType::Type::Double ) );
107 fields.append( QgsField( u"MAXX"_s, QMetaType::Type::Double ) );
108 fields.append( QgsField( u"MAXY"_s, QMetaType::Type::Double ) );
109 fields.append( QgsField( u"CNTX"_s, QMetaType::Type::Double ) );
110 fields.append( QgsField( u"CNTY"_s, QMetaType::Type::Double ) );
111 fields.append( QgsField( u"AREA"_s, QMetaType::Type::Double ) );
112 fields.append( QgsField( u"PERIM"_s, QMetaType::Type::Double ) );
113 fields.append( QgsField( u"HEIGHT"_s, QMetaType::Type::Double ) );
114 fields.append( QgsField( u"WIDTH"_s, QMetaType::Type::Double ) );
115
116 QString dest;
117 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, layer->crs() ) );
118 if ( !sink )
119 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
120
121 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
122 {
123 vl->updateExtents();
124 }
125
126 QgsRectangle rect = layer->extent();
127
128 if ( roundTo > 0 )
129 {
130 rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo );
131 rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo );
132 rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo );
133 rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo );
134 }
135
136 const QgsGeometry geom = QgsGeometry::fromRect( rect );
137
138 const double minX = rect.xMinimum();
139 const double maxX = rect.xMaximum();
140 const double minY = rect.yMinimum();
141 const double maxY = rect.yMaximum();
142 const double height = rect.height();
143 const double width = rect.width();
144 const double cntX = minX + width / 2.0;
145 const double cntY = minY + height / 2.0;
146 const double area = width * height;
147 const double perim = 2 * width + 2 * height;
148
149 QgsFeature feat;
150 feat.setGeometry( geom );
151 feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width );
152 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
153 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
154
155 sink->finalize();
156
157 QVariantMap outputs;
158 outputs.insert( u"OUTPUT"_s, dest );
159 return outputs;
160}
161
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ Polygon
Polygon.
Definition qgis.h:298
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
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: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
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.