QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmdefineprojection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmdefineprojection.cpp
3 ---------------------
4 begin : February 2025
5 copyright : (C) 2025 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 "qgsproviderregistry.h"
21#include "qgsvectorlayer.h"
22
24
25QString QgsDefineProjectionAlgorithm::name() const
26{
27 return QStringLiteral( "definecurrentprojection" );
28}
29
30QString QgsDefineProjectionAlgorithm::displayName() const
31{
32 return QObject::tr( "Define projection" );
33}
34
35QStringList QgsDefineProjectionAlgorithm::tags() const
36{
37 return QObject::tr( "layer,shp,prj,qpj,change,alter" ).split( ',' );
38}
39
40QString QgsDefineProjectionAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsDefineProjectionAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50QString QgsDefineProjectionAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "This algorithm sets an existing layer's projection to the provided CRS without reprojecting features. "
53 "Contrary to the \"Assign projection\" algorithm, it will not output a new layer.\n\n"
54 "If the input layer is a shapefile, the .prj file will be overwritten — or created if "
55 "missing — to match the provided CRS." );
56}
57
58QString QgsDefineProjectionAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Sets an existing layer's projection to the provided CRS without reprojecting features." );
61}
62
63QgsDefineProjectionAlgorithm *QgsDefineProjectionAlgorithm::createInstance() const
64{
65 return new QgsDefineProjectionAlgorithm();
66}
67
68void QgsDefineProjectionAlgorithm::initAlgorithm( const QVariantMap & )
69{
70 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input shapefile" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry ) ) );
71 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "CRS" ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) );
72 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer with projection" ) ) );
73}
74
75bool QgsDefineProjectionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76{
77 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
78 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
79
80 if ( !layer )
81 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
82
83 mLayerId = layer->id();
84
85 if ( layer->providerType().compare( QStringLiteral( "ogr" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 )
86 {
87 const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->dataProvider()->dataSourceUri() );
88 QString layerPath;
89 if ( parts.size() > 0 )
90 {
91 layerPath = parts.value( QStringLiteral( "path" ) ).toString();
92 }
93
94 if ( !layerPath.isEmpty() && layerPath.endsWith( QStringLiteral( ".shp" ), Qt::CaseSensitivity::CaseInsensitive ) )
95 {
96 const QString filePath = layerPath.chopped( 4 );
97 const QString wkt = crs.toWkt( Qgis::CrsWktVariant::Wkt1Esri );
98
99 QFile prjFile( filePath + QLatin1String( ".prj" ) );
100 if ( prjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
101 {
102 QTextStream stream( &prjFile );
103 stream << wkt << Qt::endl;
104 }
105 else
106 {
107 feedback->pushWarning( QObject::tr( "Failed to open .prj file for writing." ) );
108 }
109
110 QFile qpjFile( filePath + QLatin1String( ".qpj" ) );
111 if ( qpjFile.exists() )
112 {
113 qpjFile.remove();
114 }
115 }
116 else
117 {
118 feedback->pushWarning( QObject::tr( "Data source isn't a shapefile, skipping .prj creation" ) );
119 }
120 }
121 else
122 {
123 feedback->pushInfo( QObject::tr( "Data source isn't a shapefile, skipping .prj creation" ) );
124 }
125
126 layer->setCrs( crs );
127 layer->triggerRepaint();
128
129 return true;
130}
131
132QVariantMap QgsDefineProjectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
133{
134 Q_UNUSED( parameters );
135 Q_UNUSED( context );
136 Q_UNUSED( feedback );
137
138 QVariantMap results;
139 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
140 return results;
141}
142
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3533
@ Wkt1Esri
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
Definition qgis.h:2434
Represents a coordinate reference system (CRS).
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
QString providerType() const
Returns the provider type (provider key) for this layer.
QString id
Definition qgsmaplayer.h:83
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
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.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
A vector layer output for processing algorithms.
A coordinate reference system parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.