QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
24
25QString QgsExtentFromLayerAlgorithm::name() const
26{
27 return QStringLiteral( "polygonfromlayerextent" );
28}
29
30QString QgsExtentFromLayerAlgorithm::displayName() const
31{
32 return QObject::tr( "Extract layer extent" );
33}
34
35QStringList QgsExtentFromLayerAlgorithm::tags() const
36{
37 return QObject::tr( "polygon,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded" ).split( ',' );
38}
39
40QString QgsExtentFromLayerAlgorithm::group() const
41{
42 return QObject::tr( "Layer tools" );
43}
44
45QString QgsExtentFromLayerAlgorithm::groupId() const
46{
47 return QStringLiteral( "layertools" );
48}
49
50QString QgsExtentFromLayerAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "This algorithm takes a map layer and generates a new vector "
53 "layer with the minimum bounding box (rectangle polygon with "
54 "N-S orientation) that covers the input layer. Optionally, the "
55 "extent can be enlarged to a rounded value." );
56}
57
58QString QgsExtentFromLayerAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Generates a vector layer with the minimum bounding box that covers the input layer." );
61}
62
63QString QgsExtentFromLayerAlgorithm::svgIconPath() const
64{
65 return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) );
66}
67
68QIcon QgsExtentFromLayerAlgorithm::icon() const
69{
70 return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) );
71}
72
73QgsExtentFromLayerAlgorithm *QgsExtentFromLayerAlgorithm::createInstance() const
74{
75 return new QgsExtentFromLayerAlgorithm();
76}
77
78void QgsExtentFromLayerAlgorithm::initAlgorithm( const QVariantMap & )
79{
80 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
81
82 auto roundParam = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "ROUND_TO" ), QObject::tr( "Round values to" ), 0, QStringLiteral( "INPUT" ), 0 );
83 roundParam->setFlags( Qgis::ProcessingParameterFlag::Advanced );
84 addParameter( roundParam.release() );
85
86 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), Qgis::ProcessingSourceType::VectorPolygon ) );
87}
88
89QVariantMap QgsExtentFromLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
90{
91 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
92
93 if ( !layer )
94 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
95
96 const double roundTo = parameterAsDouble( parameters, QStringLiteral( "ROUND_TO" ), context );
97
98 QgsFields fields;
99 fields.append( QgsField( QStringLiteral( "MINX" ), QMetaType::Type::Double ) );
100 fields.append( QgsField( QStringLiteral( "MINY" ), QMetaType::Type::Double ) );
101 fields.append( QgsField( QStringLiteral( "MAXX" ), QMetaType::Type::Double ) );
102 fields.append( QgsField( QStringLiteral( "MAXY" ), QMetaType::Type::Double ) );
103 fields.append( QgsField( QStringLiteral( "CNTX" ), QMetaType::Type::Double ) );
104 fields.append( QgsField( QStringLiteral( "CNTY" ), QMetaType::Type::Double ) );
105 fields.append( QgsField( QStringLiteral( "AREA" ), QMetaType::Type::Double ) );
106 fields.append( QgsField( QStringLiteral( "PERIM" ), QMetaType::Type::Double ) );
107 fields.append( QgsField( QStringLiteral( "HEIGHT" ), QMetaType::Type::Double ) );
108 fields.append( QgsField( QStringLiteral( "WIDTH" ), QMetaType::Type::Double ) );
109
110 QString dest;
111 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, Qgis::WkbType::Polygon, layer->crs() ) );
112 if ( !sink )
113 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
114
115 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
116 {
117 vl->updateExtents();
118 }
119
120 QgsRectangle rect = layer->extent();
121
122 if ( roundTo > 0 )
123 {
124 rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo );
125 rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo );
126 rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo );
127 rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo );
128 }
129
130 const QgsGeometry geom = QgsGeometry::fromRect( rect );
131
132 const double minX = rect.xMinimum();
133 const double maxX = rect.xMaximum();
134 const double minY = rect.yMinimum();
135 const double maxY = rect.yMaximum();
136 const double height = rect.height();
137 const double width = rect.width();
138 const double cntX = minX + width / 2.0;
139 const double cntY = minY + height / 2.0;
140 const double area = width * height;
141 const double perim = 2 * width + 2 * height;
142
143 QgsFeature feat;
144 feat.setGeometry( geom );
145 feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width );
146 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
147 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
148
149 sink->finalize();
150
151 QVariantMap outputs;
152 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
153 return outputs;
154}
155
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3536
@ Polygon
Polygon.
Definition qgis.h:281
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3763
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:58
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:54
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:73
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:80
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:87
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.