QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsapplication.h"
20 #include "qgsvectorlayer.h"
21 
23 
24 QString QgsExtentFromLayerAlgorithm::name() const
25 {
26  return QStringLiteral( "polygonfromlayerextent" );
27 }
28 
29 QString QgsExtentFromLayerAlgorithm::displayName() const
30 {
31  return QObject::tr( "Extract layer extent" );
32 }
33 
34 QStringList QgsExtentFromLayerAlgorithm::tags() const
35 {
36  return QObject::tr( "polygon,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded" ).split( ',' );
37 }
38 
39 QString QgsExtentFromLayerAlgorithm::group() const
40 {
41  return QObject::tr( "Layer tools" );
42 }
43 
44 QString QgsExtentFromLayerAlgorithm::groupId() const
45 {
46  return QStringLiteral( "layertools" );
47 }
48 
49 QString QgsExtentFromLayerAlgorithm::shortHelpString() const
50 {
51  return QObject::tr( "This algorithm takes a map layer and generates a new vector "
52  "layer with the minimum bounding box (rectangle polygon with "
53  "N-S orientation) that covers the input layer. Optionally, the "
54  "extent can be enlarged to a rounded value." );
55 }
56 
57 QString QgsExtentFromLayerAlgorithm::svgIconPath() const
58 {
59  return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) );
60 }
61 
62 QIcon QgsExtentFromLayerAlgorithm::icon() const
63 {
64  return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) );
65 }
66 
67 QgsExtentFromLayerAlgorithm *QgsExtentFromLayerAlgorithm::createInstance() const
68 {
69  return new QgsExtentFromLayerAlgorithm();
70 }
71 
72 void QgsExtentFromLayerAlgorithm::initAlgorithm( const QVariantMap & )
73 {
74  addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
75 
76  auto roundParam = std::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "ROUND_TO" ), QObject::tr( "Round values to" ), 0, QStringLiteral( "INPUT" ), 0 );
77  roundParam->setFlags( QgsProcessingParameterDefinition::FlagAdvanced );
78  addParameter( roundParam.release() );
79 
80  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), QgsProcessing::TypeVectorPolygon ) );
81 }
82 
83 QVariantMap QgsExtentFromLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
84 {
85  QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
86 
87  if ( !layer )
88  throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
89 
90  const double roundTo = parameterAsDouble( parameters, QStringLiteral( "ROUND_TO" ), context );
91 
92  QgsFields fields;
93  fields.append( QgsField( QStringLiteral( "MINX" ), QVariant::Double ) );
94  fields.append( QgsField( QStringLiteral( "MINY" ), QVariant::Double ) );
95  fields.append( QgsField( QStringLiteral( "MAXX" ), QVariant::Double ) );
96  fields.append( QgsField( QStringLiteral( "MAXY" ), QVariant::Double ) );
97  fields.append( QgsField( QStringLiteral( "CNTX" ), QVariant::Double ) );
98  fields.append( QgsField( QStringLiteral( "CNTY" ), QVariant::Double ) );
99  fields.append( QgsField( QStringLiteral( "AREA" ), QVariant::Double ) );
100  fields.append( QgsField( QStringLiteral( "PERIM" ), QVariant::Double ) );
101  fields.append( QgsField( QStringLiteral( "HEIGHT" ), QVariant::Double ) );
102  fields.append( QgsField( QStringLiteral( "WIDTH" ), QVariant::Double ) );
103 
104  QString dest;
105  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, layer->crs() ) );
106  if ( !sink )
107  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
108 
109  if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
110  {
111  vl->updateExtents();
112  }
113 
114  QgsRectangle rect = layer->extent();
115 
116  if ( roundTo > 0 )
117  {
118  rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo );
119  rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo );
120  rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo );
121  rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo );
122  }
123 
124  const QgsGeometry geom = QgsGeometry::fromRect( rect );
125 
126  const double minX = rect.xMinimum();
127  const double maxX = rect.xMaximum();
128  const double minY = rect.yMinimum();
129  const double maxY = rect.yMaximum();
130  const double height = rect.height();
131  const double width = rect.width();
132  const double cntX = minX + width / 2.0;
133  const double cntY = minY + height / 2.0;
134  const double area = width * height;
135  const double perim = 2 * width + 2 * height;
136 
137  QgsFeature feat;
138  feat.setGeometry( geom );
139  feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width );
140  if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
141  throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
142 
143  QVariantMap outputs;
144  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
145  return outputs;
146 }
147 
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:451
QgsProcessingParameterMapLayer
A map layer parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2901
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsApplication::iconPath
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Definition: qgsapplication.cpp:682
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
qgsapplication.h
qgsalgorithmextentfromlayer.h
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsMapLayer::extent
virtual QgsRectangle extent() const
Returns the extent of the layer.
Definition: qgsmaplayer.cpp:305
QgsRectangle::setXMinimum
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
Definition: qgsrectangle.h:151
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsRectangle::setXMaximum
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
Definition: qgsrectangle.h:156
qgsvectorlayer.h
QgsRectangle::setYMaximum
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
Definition: qgsrectangle.h:166
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
QgsRectangle::setYMinimum
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
Definition: qgsrectangle.h:161
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsWkbTypes::Polygon
@ Polygon
Definition: qgswkbtypes.h:74
QgsGeometry::fromRect
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
Definition: qgsgeometry.cpp:241
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50