27 void QgsBookmarksToLayerAlgorithm::initAlgorithm(
const QVariantMap & )
29 std::unique_ptr< QgsProcessingParameterEnum > sourceParam = qgis::make_unique<QgsProcessingParameterEnum >( QStringLiteral(
"SOURCE" ), QObject::tr(
"Bookmark source" ), QStringList() <<
30 QObject::tr(
"Project bookmarks" ) << QObject::tr(
"User bookmarks" ),
true, QVariantList() << 0 << 1 );
31 QVariantMap wrapperMetadata;
32 wrapperMetadata.insert( QStringLiteral(
"useCheckBoxes" ),
true );
34 metadata.insert( QStringLiteral(
"widget_wrapper" ), wrapperMetadata );
35 sourceParam->setMetadata( metadata );
36 addParameter( sourceParam.release() );
41 QString QgsBookmarksToLayerAlgorithm::name()
const
43 return QStringLiteral(
"bookmarkstolayer" );
46 QString QgsBookmarksToLayerAlgorithm::displayName()
const
48 return QObject::tr(
"Convert spatial bookmarks to layer" );
51 QStringList QgsBookmarksToLayerAlgorithm::tags()
const
53 return QObject::tr(
"save,extract" ).split(
',' );
56 QString QgsBookmarksToLayerAlgorithm::group()
const
58 return QObject::tr(
"Vector general" );
61 QString QgsBookmarksToLayerAlgorithm::groupId()
const
63 return QStringLiteral(
"vectorgeneral" );
66 QString QgsBookmarksToLayerAlgorithm::shortHelpString()
const
68 return QObject::tr(
"This algorithm creates a new layer containing polygon features for stored spatial bookmarks.\n\n"
69 "The export can be filtered to only bookmarks belonging to the current project, to all user bookmarks, or a combination of both." );
72 QString QgsBookmarksToLayerAlgorithm::shortDescription()
const
74 return QObject::tr(
"Converts stored spatial bookmarks to a polygon layer." );
77 QIcon QgsBookmarksToLayerAlgorithm::icon()
const
82 QString QgsBookmarksToLayerAlgorithm::svgIconPath()
const
87 QgsBookmarksToLayerAlgorithm *QgsBookmarksToLayerAlgorithm::createInstance()
const
89 return new QgsBookmarksToLayerAlgorithm();
94 QList< int > sources = parameterAsEnums( parameters, QStringLiteral(
"SOURCE" ), context );
95 if ( sources.contains( 0 ) )
101 if ( sources.contains( 1 ) )
111 fields.
append(
QgsField( QStringLiteral(
"name" ), QVariant::String ) );
112 fields.
append(
QgsField( QStringLiteral(
"group" ), QVariant::String ) );
114 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
QgsWkbTypes::Polygon,
crs ) );
118 int count = mBookmarks.count();
120 double step = count > 0 ? 100.0 / count : 1;
122 for (
const QgsBookmark &b : qgis::as_const( mBookmarks ) )
133 if ( b.extent().crs() !=
crs )
143 feedback->
reportError( QObject::tr(
"Could not reproject bookmark %1 to destination CRS" ).arg( b.name() ) );
157 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
166 void QgsLayerToBookmarksAlgorithm::initAlgorithm(
const QVariantMap & )
170 std::unique_ptr< QgsProcessingParameterEnum > sourceParam = qgis::make_unique<QgsProcessingParameterEnum >( QStringLiteral(
"DESTINATION" ), QObject::tr(
"Bookmark destination" ), QStringList() <<
171 QObject::tr(
"Project bookmarks" ) << QObject::tr(
"User bookmarks" ),
false, 0 );
172 addParameter( sourceParam.release() );
174 addParameter(
new QgsProcessingParameterExpression( QStringLiteral(
"NAME_EXPRESSION" ), QObject::tr(
"Name field" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
175 addParameter(
new QgsProcessingParameterExpression( QStringLiteral(
"GROUP_EXPRESSION" ), QObject::tr(
"Group field" ), QVariant(), QStringLiteral(
"INPUT" ),
true ) );
180 QString QgsLayerToBookmarksAlgorithm::name()
const
182 return QStringLiteral(
"layertobookmarks" );
185 QString QgsLayerToBookmarksAlgorithm::displayName()
const
187 return QObject::tr(
"Convert layer to spatial bookmarks" );
190 QStringList QgsLayerToBookmarksAlgorithm::tags()
const
192 return QObject::tr(
"save,extract,store" ).split(
',' );
195 QString QgsLayerToBookmarksAlgorithm::group()
const
197 return QObject::tr(
"Vector general" );
200 QString QgsLayerToBookmarksAlgorithm::groupId()
const
202 return QStringLiteral(
"vectorgeneral" );
205 QString QgsLayerToBookmarksAlgorithm::shortHelpString()
const
207 return QObject::tr(
"This algorithm creates spatial bookmarks corresponding to the extent of features contained in a layer." );
210 QString QgsLayerToBookmarksAlgorithm::shortDescription()
const
212 return QObject::tr(
"Converts feature extents to stored spatial bookmarks." );
215 QIcon QgsLayerToBookmarksAlgorithm::icon()
const
220 QString QgsLayerToBookmarksAlgorithm::svgIconPath()
const
225 QgsLayerToBookmarksAlgorithm *QgsLayerToBookmarksAlgorithm::createInstance()
const
227 return new QgsLayerToBookmarksAlgorithm();
232 mDest = parameterAsEnum( parameters, QStringLiteral(
"DESTINATION" ), context );
233 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
238 QString nameExpressionString = parameterAsExpression( parameters, QStringLiteral(
"NAME_EXPRESSION" ), context );
239 QString groupExpressionString = parameterAsExpression( parameters, QStringLiteral(
"GROUP_EXPRESSION" ), context );
242 expressionContext.
appendScope( source->createExpressionContextScope() );
245 if ( !nameExpression.
prepare( &expressionContext ) )
250 std::unique_ptr< QgsExpression > groupExpression;
251 if ( !groupExpressionString.isEmpty() )
253 groupExpression = qgis::make_unique< QgsExpression >( groupExpressionString );
254 if ( !groupExpression->prepare( &expressionContext ) )
255 throw QgsProcessingException( QObject::tr(
"Invalid group expression: %1" ).arg( groupExpression->parserErrorString() ) );
256 requiredColumns.unite( groupExpression->referencedColumns() );
262 double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
277 const QString name = nameExpression.
evaluate( &expressionContext ).toString();
286 if ( groupExpression )
288 group = groupExpression->evaluate( &expressionContext ).toString();
289 if ( !groupExpression->evalErrorString().isEmpty() )
291 feedback->
reportError( QObject::tr(
"Error evaluating group expression: %1" ).arg( groupExpression->evalErrorString() ) );
308 return QVariantMap();
325 for (
const QgsBookmark &b : qgis::as_const( mBookmarks ) )
329 res.insert( QStringLiteral(
"COUNT" ), mBookmarks.size() );