17#include "moc_qgscrsdefinitionwidget.cpp"
22#include <QRegularExpression>
23#include <QRegularExpressionMatch>
31 connect( mButtonCalculate, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::pbnCalculate_clicked );
32 connect( mButtonCopyCRS, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::pbnCopyCRS_clicked );
33 connect( mButtonValidate, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::validateCurrent );
39 connect( mFormatComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
this, &QgsCrsDefinitionWidget::formatChanged );
68 switch ( nativeFormat )
78 whileBlocking( mFormatComboBox )->setCurrentIndex( mFormatComboBox->findData(
static_cast<int>( nativeFormat ) ) );
89 mFormatComboBox->setCurrentIndex( mFormatComboBox->findData(
static_cast<int>(
format ) ) );
94 return mTextEditParameters->toPlainText();
99 mTextEditParameters->setPlainText( definition );
102void QgsCrsDefinitionWidget::pbnCopyCRS_clicked()
104 std::unique_ptr<QgsProjectionSelectionDialog> selector = std::make_unique<QgsProjectionSelectionDialog>(
this );
105 if ( selector->exec() )
114void QgsCrsDefinitionWidget::validateCurrent()
116 const QString projDef = mTextEditParameters->toPlainText();
127 PROJ_STRING_LIST warnings =
nullptr;
128 PROJ_STRING_LIST grammarErrors =
nullptr;
129 crs.reset( proj_create_from_wkt( context, projDef.toUtf8().constData(),
nullptr, &warnings, &grammarErrors ) );
130 QStringList warningStrings;
131 QStringList grammarStrings;
132 for (
auto iter = warnings; iter && *iter; ++iter )
133 warningStrings << QString( *iter );
134 for (
auto iter = grammarErrors; iter && *iter; ++iter )
135 grammarStrings << QString( *iter );
136 proj_string_list_destroy( warnings );
137 proj_string_list_destroy( grammarErrors );
141 QMessageBox::information(
this, tr(
"Custom Coordinate Reference System" ), tr(
"This WKT projection definition is valid." ) );
145 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ), tr(
"This WKT projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + warningStrings.join(
'\n' ) + grammarStrings.join(
'\n' ) );
152 const QString projCrsString = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
153 crs.reset( proj_create( context, projCrsString.toUtf8().constData() ) );
156 QMessageBox::information(
this, tr(
"Custom Coordinate Reference System" ), tr(
"This proj projection definition is valid." ) );
160 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ), tr(
"This proj projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + projLogger.
errors().join(
'\n' ) );
167void QgsCrsDefinitionWidget::formatChanged()
170 QString newFormatString;
175 crs.
createFromWkt( multiLineWktToSingleLine( mTextEditParameters->toPlainText() ) );
184 QString proj = mTextEditParameters->toPlainText();
185 proj.replace( QLatin1String(
"+type=crs" ), QString() );
186 proj += QLatin1String(
" +type=crs" );
190 const QByteArray multiLineOption = QStringLiteral(
"MULTILINE=YES" ).toLocal8Bit();
191 const char *
const options[] = { multiLineOption.constData(),
nullptr };
192 newFormatString = QString( proj_as_wkt( pjContext,
crs.get(), PJ_WKT2_2019, options ) );
197 if ( !newFormatString.isEmpty() )
198 mTextEditParameters->setPlainText( newFormatString );
201void QgsCrsDefinitionWidget::pbnCalculate_clicked()
204 QString projDef = mTextEditParameters->toPlainText();
208 double latitude = mNorthWGS84Edit->text().toDouble( &okN );
209 double longitude = mEastWGS84Edit->text().toDouble( &okE );
213 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ), tr(
"Latitude and Longitude must be in decimal form." ) );
214 mProjectedXLabel->clear();
215 mProjectedYLabel->clear();
222 projDef = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
232 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ), tr(
"This CRS projection definition is not valid." ) );
233 mProjectedXLabel->clear();
234 mProjectedYLabel->clear();
260 mProjectedXLabel->setText( QLocale().toString( res.
x(),
'f',
precision ) );
261 mProjectedYLabel->setText( QLocale().toString( res.
y(),
'f',
precision ) );
265 mProjectedXLabel->setText( tr(
"Error" ) );
266 mProjectedYLabel->setText( tr(
"Error" ) );
267 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ), e.
what() );
271QString QgsCrsDefinitionWidget::multiLineWktToSingleLine(
const QString &wkt )
274 const thread_local QRegularExpression re( QStringLiteral(
"\\s*\\n\\s*" ), QRegularExpression::MultilineOption );
275 res.replace( re, QString() );
CrsDefinitionFormat
CRS definition formats.
@ Wkt
WKT format (always recommended over proj string format)
@ Proj
Proj string format.
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool createFromWkt(const QString &wkt)
Sets this CRS using a WKT definition.
QString toProj() const
Returns a Proj string representation of this CRS.
Qgis::CrsDefinitionFormat nativeFormat() const
Returns the native format for the CRS definition.
void setNativeFormat(Qgis::CrsDefinitionFormat format)
Sets the native format for the CRS definition.
QgsCoordinateReferenceSystem toGeographicCrs() const
Returns the geographic CRS associated with this CRS object.
static QgsCoordinateReferenceSystem fromProj(const QString &proj)
Creates a CRS from a proj style formatted string.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
QString celestialBodyName() const
Attempts to retrieve the name of the celestial body associated with the CRS (e.g.
static QgsCoordinateReferenceSystem fromWkt(const QString &wkt)
Creates a CRS from a WKT spatial ref sys definition string.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
Custom exception class which is raised when an operation is not supported.
A class to represent a 2D point.
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.
std::unique_ptr< PJ, ProjPJDeleter > proj_pj_unique_ptr
Scoped Proj PJ object.
Scoped object for temporary swapping to an error-collecting PROJ log function.
QStringList errors() const
Returns the (possibly empty) list of collected errors.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
struct projCtx_t PJ_CONTEXT
const QgsCoordinateReferenceSystem & crs