21#include <QRegularExpression>
22#include <QRegularExpressionMatch>
30 connect( mButtonCalculate, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::pbnCalculate_clicked );
31 connect( mButtonCopyCRS, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::pbnCopyCRS_clicked );
32 connect( mButtonValidate, &QPushButton::clicked,
this, &QgsCrsDefinitionWidget::validateCurrent );
38 connect( mFormatComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
this, &QgsCrsDefinitionWidget::formatChanged );
67 switch ( nativeFormat )
77 whileBlocking( mFormatComboBox )->setCurrentIndex( mFormatComboBox->findData(
static_cast< int >( nativeFormat ) ) );
88 mFormatComboBox->setCurrentIndex( mFormatComboBox->findData(
static_cast< int >(
format ) ) );
93 return mTextEditParameters->toPlainText();
98 mTextEditParameters->setPlainText( definition );
101void QgsCrsDefinitionWidget::pbnCopyCRS_clicked()
103 std::unique_ptr< QgsProjectionSelectionDialog > selector = std::make_unique< QgsProjectionSelectionDialog >(
this );
104 if ( selector->exec() )
113void QgsCrsDefinitionWidget::validateCurrent()
115 const QString projDef = mTextEditParameters->toPlainText();
126 PROJ_STRING_LIST warnings =
nullptr;
127 PROJ_STRING_LIST grammarErrors =
nullptr;
128 crs.reset( proj_create_from_wkt( context, projDef.toUtf8().constData(),
nullptr, &warnings, &grammarErrors ) );
129 QStringList warningStrings;
130 QStringList grammarStrings;
131 for (
auto iter = warnings; iter && *iter; ++iter )
132 warningStrings << QString( *iter );
133 for (
auto iter = grammarErrors; iter && *iter; ++iter )
134 grammarStrings << QString( *iter );
135 proj_string_list_destroy( warnings );
136 proj_string_list_destroy( grammarErrors );
140 QMessageBox::information(
this, tr(
"Custom Coordinate Reference System" ),
141 tr(
"This WKT projection definition is valid." ) );
145 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
146 tr(
"This WKT projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + warningStrings.join(
'\n' ) + grammarStrings.join(
'\n' ) );
153 const QString projCrsString = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
154 crs.reset( proj_create( context, projCrsString.toUtf8().constData() ) );
157 QMessageBox::information(
this, tr(
"Custom Coordinate Reference System" ),
158 tr(
"This proj projection definition is valid." ) );
162 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
163 tr(
"This proj projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + projLogger.
errors().join(
'\n' ) );
170void QgsCrsDefinitionWidget::formatChanged()
173 QString newFormatString;
178 crs.
createFromWkt( multiLineWktToSingleLine( mTextEditParameters->toPlainText() ) );
187 QString proj = mTextEditParameters->toPlainText();
188 proj.replace( QLatin1String(
"+type=crs" ), QString() );
189 proj += QLatin1String(
" +type=crs" );
193 const QByteArray multiLineOption = QStringLiteral(
"MULTILINE=YES" ).toLocal8Bit();
194 const char *
const options[] = {multiLineOption.constData(),
nullptr};
195 newFormatString = QString( proj_as_wkt( pjContext,
crs.get(), PJ_WKT2_2019, options ) );
200 if ( !newFormatString.isEmpty() )
201 mTextEditParameters->setPlainText( newFormatString );
204void QgsCrsDefinitionWidget::pbnCalculate_clicked()
207 QString projDef = mTextEditParameters->toPlainText();
211 double latitude = mNorthWGS84Edit->text().toDouble( &okN );
212 double longitude = mEastWGS84Edit->text().toDouble( &okE );
216 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
217 tr(
"Latitude and Longitude must be in decimal form." ) );
218 mProjectedXLabel->clear();
219 mProjectedYLabel->clear();
226 projDef = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
236 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
237 tr(
"This CRS projection definition is not valid." ) );
238 mProjectedXLabel->clear();
239 mProjectedYLabel->clear();
265 mProjectedXLabel->setText( QLocale().toString( res.
x(),
'f',
precision ) );
266 mProjectedYLabel->setText( QLocale().toString( res.
y(),
'f',
precision ) );
270 mProjectedXLabel->setText( tr(
"Error" ) );
271 mProjectedYLabel->setText( tr(
"Error" ) );
272 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
277QString QgsCrsDefinitionWidget::multiLineWktToSingleLine(
const QString &wkt )
280 const thread_local QRegularExpression re( QStringLiteral(
"\\s*\\n\\s*" ), QRegularExpression::MultilineOption );
281 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