25using namespace Qt::StringLiterals;
29QString QgsDefineProjectionAlgorithm::name()
const
31 return u
"definecurrentprojection"_s;
34QString QgsDefineProjectionAlgorithm::displayName()
const
36 return QObject::tr(
"Define projection" );
39QStringList QgsDefineProjectionAlgorithm::tags()
const
41 return QObject::tr(
"layer,shp,prj,qpj,change,alter" ).split(
',' );
44QString QgsDefineProjectionAlgorithm::group()
const
46 return QObject::tr(
"Vector general" );
49QString QgsDefineProjectionAlgorithm::groupId()
const
51 return u
"vectorgeneral"_s;
54QString QgsDefineProjectionAlgorithm::shortHelpString()
const
57 "This algorithm sets an existing layer's projection to the provided CRS without reprojecting features. "
58 "Contrary to the \"Assign projection\" algorithm, it will not output a new layer.\n\n"
59 "If the input layer is a shapefile, the .prj file will be overwritten — or created if "
60 "missing — to match the provided CRS."
64QString QgsDefineProjectionAlgorithm::shortDescription()
const
66 return QObject::tr(
"Sets an existing layer's projection to the provided CRS without reprojecting features." );
69QgsDefineProjectionAlgorithm *QgsDefineProjectionAlgorithm::createInstance()
const
71 return new QgsDefineProjectionAlgorithm();
74void QgsDefineProjectionAlgorithm::initAlgorithm(
const QVariantMap & )
83 QGS_MARK_ALGORITHM_SOURCE
85 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
91 mLayerId = layer->id();
93 if ( layer->providerType().compare( u
"ogr"_s, Qt::CaseSensitivity::CaseInsensitive ) == 0 )
97 if ( parts.size() > 0 )
99 layerPath = parts.value( u
"path"_s ).toString();
102 if ( !layerPath.isEmpty() && layerPath.endsWith( u
".shp"_s, Qt::CaseSensitivity::CaseInsensitive ) )
104 const QString filePath = layerPath.chopped( 4 );
107 QFile prjFile( filePath +
".prj"_L1 );
108 if ( prjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
110 QTextStream stream( &prjFile );
111 stream << wkt << Qt::endl;
115 feedback->
pushWarning( QObject::tr(
"Failed to open .prj file for writing." ) );
118 QFile qpjFile( filePath +
".qpj"_L1 );
119 if ( qpjFile.exists() )
126 feedback->
pushWarning( QObject::tr(
"Data source isn't a shapefile, skipping .prj creation" ) );
131 feedback->
pushInfo( QObject::tr(
"Data source isn't a shapefile, skipping .prj creation" ) );
134 layer->setCrs( crs );
135 layer->triggerRepaint();
142 Q_UNUSED( parameters );
144 Q_UNUSED( feedback );
147 results.insert( u
"OUTPUT"_s, mLayerId );
@ VectorAnyGeometry
Any vector layer with geometry.
@ Wkt1Esri
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
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.
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.