Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] port extract layer extent to C++
- Loading branch information
Showing
9 changed files
with
210 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/analysis/processing/qgsalgorithmextentfromlayer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/*************************************************************************** | ||
qgsalgorithmextentfromlayer.cpp | ||
--------------------- | ||
begin : November 2019 | ||
copyright : (C) 2019 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmextentfromlayer.h" | ||
#include "qgsapplication.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsExtentFromLayerAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "polygonfromlayerextent" ); | ||
} | ||
|
||
QString QgsExtentFromLayerAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Extract layer extent" ); | ||
} | ||
|
||
QStringList QgsExtentFromLayerAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "polygon,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded" ).split( ',' ); | ||
} | ||
|
||
QString QgsExtentFromLayerAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Raster terrain analysis" ); | ||
} | ||
|
||
QString QgsExtentFromLayerAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "rasterterrainanalysis" ); | ||
} | ||
|
||
QString QgsExtentFromLayerAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "This algorithm takes a map layer and generates a new vector " | ||
"layer with the minimum bounding box (rectangle polygon with " | ||
"N-S orientation) that covers the input layer. Optionally, the " | ||
"extent can be enlarged to a rounded value." ); | ||
} | ||
|
||
QString QgsExtentFromLayerAlgorithm::svgIconPath() const | ||
{ | ||
return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) ); | ||
} | ||
|
||
QIcon QgsExtentFromLayerAlgorithm::icon() const | ||
{ | ||
return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmExtractLayerExtent.svg" ) ); | ||
} | ||
|
||
QgsExtentFromLayerAlgorithm *QgsExtentFromLayerAlgorithm::createInstance() const | ||
{ | ||
return new QgsExtentFromLayerAlgorithm(); | ||
} | ||
|
||
void QgsExtentFromLayerAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) ); | ||
|
||
auto roundParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "ROUND_TO" ), QObject::tr( "Round values to" ), 0, QStringLiteral( "INPUT" ), 0 ); | ||
roundParam->setFlags( QgsProcessingParameterDefinition::FlagAdvanced ); | ||
addParameter( roundParam.release() ); | ||
|
||
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), QgsProcessing::TypeVectorPolygon ) ); | ||
} | ||
|
||
QVariantMap QgsExtentFromLayerAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) | ||
{ | ||
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ); | ||
|
||
if ( !layer ) | ||
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) ); | ||
|
||
double roundTo = parameterAsDouble( parameters, QStringLiteral( "ROUND_TO" ), context ); | ||
|
||
QgsFields fields; | ||
fields.append( QgsField( QStringLiteral( "MINX" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "MINY" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "MAXX" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "MAXY" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "CNTX" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "CNTY" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "AREA" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "PERIM" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "HEIGHT" ), QVariant::Double ) ); | ||
fields.append( QgsField( QStringLiteral( "WIDTH" ), QVariant::Double ) ); | ||
|
||
QString dest; | ||
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, layer->crs() ) ); | ||
if ( !sink ) | ||
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); | ||
|
||
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) ) | ||
{ | ||
vl->updateExtents(); | ||
} | ||
|
||
QgsRectangle rect = layer->extent(); | ||
|
||
if ( roundTo > 0 ) | ||
{ | ||
rect.setXMinimum( std::floor( rect.xMinimum() / roundTo ) * roundTo ); | ||
rect.setYMinimum( std::floor( rect.yMinimum() / roundTo ) * roundTo ); | ||
rect.setXMaximum( std::ceil( rect.xMaximum() / roundTo ) * roundTo ); | ||
rect.setYMaximum( std::ceil( rect.yMaximum() / roundTo ) * roundTo ); | ||
} | ||
|
||
QgsGeometry geom = QgsGeometry::fromRect( rect ); | ||
|
||
double minX = rect.xMinimum(); | ||
double maxX = rect.xMaximum(); | ||
double minY = rect.yMinimum(); | ||
double maxY = rect.yMaximum(); | ||
double height = rect.height(); | ||
double width = rect.width(); | ||
double cntX = minX + width / 2.0; | ||
double cntY = minY + height / 2.0; | ||
double area = width * height; | ||
double perim = 2 * width + 2 * height; | ||
|
||
QgsFeature feat; | ||
feat.setGeometry( geom ); | ||
feat.setAttributes( QgsAttributes() << minX << minY << maxX << maxY << cntX << cntY << area << perim << height << width ); | ||
sink->addFeature( feat, QgsFeatureSink::FastInsert ); | ||
|
||
QVariantMap outputs; | ||
outputs.insert( QStringLiteral( "OUTPUT" ), dest ); | ||
return outputs; | ||
} | ||
|
||
///@endcond |
Oops, something went wrong.