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" ),
142 tr(
"This WKT projection definition is valid." ) );
146 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
147 tr(
"This WKT projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + warningStrings.join(
'\n' ) + grammarStrings.join(
'\n' ) );
154 const QString projCrsString = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
155 crs.reset( proj_create( context, projCrsString.toUtf8().constData() ) );
158 QMessageBox::information(
this, tr(
"Custom Coordinate Reference System" ),
159 tr(
"This proj projection definition is valid." ) );
163 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
164 tr(
"This proj projection definition is not valid:" ) + QStringLiteral(
"\n\n" ) + projLogger.
errors().join(
'\n' ) );
171void QgsCrsDefinitionWidget::formatChanged()
174 QString newFormatString;
179 crs.
createFromWkt( multiLineWktToSingleLine( mTextEditParameters->toPlainText() ) );
188 QString proj = mTextEditParameters->toPlainText();
189 proj.replace( QLatin1String(
"+type=crs" ), QString() );
190 proj += QLatin1String(
" +type=crs" );
194 const QByteArray multiLineOption = QStringLiteral(
"MULTILINE=YES" ).toLocal8Bit();
195 const char *
const options[] = {multiLineOption.constData(),
nullptr};
196 newFormatString = QString( proj_as_wkt( pjContext,
crs.get(), PJ_WKT2_2019, options ) );
201 if ( !newFormatString.isEmpty() )
202 mTextEditParameters->setPlainText( newFormatString );
205void QgsCrsDefinitionWidget::pbnCalculate_clicked()
208 QString projDef = mTextEditParameters->toPlainText();
212 double latitude = mNorthWGS84Edit->text().toDouble( &okN );
213 double longitude = mEastWGS84Edit->text().toDouble( &okE );
217 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
218 tr(
"Latitude and Longitude must be in decimal form." ) );
219 mProjectedXLabel->clear();
220 mProjectedYLabel->clear();
227 projDef = projDef + ( projDef.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
237 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
238 tr(
"This CRS projection definition is not valid." ) );
239 mProjectedXLabel->clear();
240 mProjectedYLabel->clear();
266 mProjectedXLabel->setText( QLocale().toString( res.
x(),
'f',
precision ) );
267 mProjectedYLabel->setText( QLocale().toString( res.
y(),
'f',
precision ) );
271 mProjectedXLabel->setText( tr(
"Error" ) );
272 mProjectedYLabel->setText( tr(
"Error" ) );
273 QMessageBox::warning(
this, tr(
"Custom Coordinate Reference System" ),
278QString QgsCrsDefinitionWidget::multiLineWktToSingleLine(
const QString &wkt )
281 const thread_local QRegularExpression re( QStringLiteral(
"\\s*\\n\\s*" ), QRegularExpression::MultilineOption );
282 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