28using namespace Qt::StringLiterals;
32QString QgsLayoutMapExtentToLayerAlgorithm::name()
const
34 return u
"printlayoutmapextenttolayer"_s;
37QString QgsLayoutMapExtentToLayerAlgorithm::displayName()
const
39 return QObject::tr(
"Print layout map extent to layer" );
42QStringList QgsLayoutMapExtentToLayerAlgorithm::tags()
const
44 return QObject::tr(
"layout,composer,composition,visible" ).split(
',' );
47QString QgsLayoutMapExtentToLayerAlgorithm::group()
const
49 return QObject::tr(
"Cartography" );
52QString QgsLayoutMapExtentToLayerAlgorithm::groupId()
const
54 return u
"cartography"_s;
57QString QgsLayoutMapExtentToLayerAlgorithm::shortDescription()
const
59 return QObject::tr(
"Creates a polygon layer containing the extent of a print layout map item." );
62void QgsLayoutMapExtentToLayerAlgorithm::initAlgorithm(
const QVariantMap & )
66 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u
"CRS"_s, QObject::tr(
"Override CRS" ), QVariant(),
true );
68 addParameter( crsParam.release() );
81QString QgsLayoutMapExtentToLayerAlgorithm::shortHelpString()
const
83 return QObject::tr(
"This algorithm creates a polygon layer containing the extent of a print layout map item (or items), "
84 "with attributes specifying the map size (in layout units), scale and rotation.\n\n"
85 "If the map item parameter is specified, then only the matching map extent will be exported. If it "
86 "is not specified, all map extents from the layout will be exported.\n\n"
87 "Optionally, a specific output CRS can be specified. If it is not specified, the original map "
88 "item CRS will be used." );
91QgsLayoutMapExtentToLayerAlgorithm *QgsLayoutMapExtentToLayerAlgorithm::createInstance()
const
93 return new QgsLayoutMapExtentToLayerAlgorithm();
99 QgsPrintLayout *layout = parameterAsLayout( parameters, u
"LAYOUT"_s, context );
101 throw QgsProcessingException( QObject::tr(
"Cannot find layout with name \"%1\"" ).arg( parameters.value( u
"LAYOUT"_s ).toString() ) );
103 QgsLayoutItemMap *map = qobject_cast<QgsLayoutItemMap *>( parameterAsLayoutItem( parameters, u
"MAP"_s, context, layout ) );
104 if ( !map && parameters.value( u
"MAP"_s ).isValid() )
105 throw QgsProcessingException( QObject::tr(
"Cannot find matching map item with ID %1" ).arg( parameters.value( u
"MAP"_s ).toString() ) );
107 QList<QgsLayoutItemMap *> maps;
115 mFeatures.reserve( maps.size() );
118 if ( !mCrs.isValid() )
119 mCrs = !overrideCrs.
isValid() ? map->crs() : overrideCrs;
122 if ( map->crs() != mCrs )
130 feedback->
reportError( QObject::tr(
"Error reprojecting map to destination CRS" ) );
135 mWidth = map->rect().width();
136 mHeight = map->rect().height();
137 mScale = map->scale();
138 mRotation = map->mapRotation();
154 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double ) );
156 fields.
append(
QgsField( u
"rotation"_s, QMetaType::Type::Double ) );
159 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::Polygon, mCrs ) );
174 outputs.insert( u
"OUTPUT"_s, dest );
176 outputs.insert( u
"WIDTH"_s, mFeatures.size() == 1 ? mWidth : QVariant() );
177 outputs.insert( u
"HEIGHT"_s, mFeatures.size() == 1 ? mHeight : QVariant() );
178 outputs.insert( u
"SCALE"_s, mFeatures.size() == 1 ? mScale : QVariant() );
179 outputs.insert( u
"ROTATION"_s, mFeatures.size() == 1 ? mRotation : QVariant() );
@ VectorPolygon
Vector polygon layers.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Custom exception class for Coordinate Reference System related exceptions.
@ 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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
A geometry is the spatial representation of a feature.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
Layout graphical items for displaying a map.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A numeric output for processing algorithms.
A feature sink output for processing algorithms.
A print layout item parameter, allowing users to select a particular item from a print layout.
A print layout parameter, allowing users to select a print layout.