QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsdatumtransformdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatumtransformdialog.cpp
3 ---------------------------
4 begin : November 2013
5 copyright : (C) 2013 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot ch
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#include "moc_qgsdatumtransformdialog.cpp"
22#include "qgslogger.h"
23#include "qgssettings.h"
24#include "qgsproject.h"
25#include "qgsguiutils.h"
26#include "qgsgui.h"
27#include "qgshelp.h"
28
29#include <QDir>
30#include <QPushButton>
31
32#include "qgsprojutils.h"
33#include <proj.h>
34
35bool QgsDatumTransformDialog::run( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, QWidget *parent, QgsMapCanvas *mapCanvas, const QString &windowTitle )
36{
37 if ( sourceCrs == destinationCrs )
38 return true;
39
41 if ( context.hasTransform( sourceCrs, destinationCrs ) )
42 {
43 return true;
44 }
45
46 QgsDatumTransformDialog dlg( sourceCrs, destinationCrs, false, true, false, qMakePair( -1, -1 ), parent, Qt::WindowFlags(), QString(), mapCanvas );
47 if ( !windowTitle.isEmpty() )
48 dlg.setWindowTitle( windowTitle );
49
50 if ( dlg.shouldAskUserForSelection() )
51 {
52 if ( dlg.exec() )
53 {
61 return true;
62 }
63 else
64 {
65 return false;
66 }
67 }
68 else
69 {
70 dlg.applyDefaultTransform();
71 return true;
72 }
73}
74
75QgsDatumTransformDialog::QgsDatumTransformDialog( const QgsCoordinateReferenceSystem &sCrs, const QgsCoordinateReferenceSystem &dCrs, const bool allowCrsChanges, const bool showMakeDefault, const bool forceChoice, QPair<int, int> selectedDatumTransforms, QWidget *parent, Qt::WindowFlags f, const QString &selectedProj, QgsMapCanvas *mapCanvas, bool allowFallback )
76 : QDialog( parent, f )
77 , mPreviousCursorOverride( std::make_unique<QgsTemporaryCursorRestoreOverride>() ) // this dialog is often shown while cursor overrides are in place, so temporarily remove them
78{
79 setupUi( this );
80
81 QgsCoordinateReferenceSystem sourceCrs = sCrs;
82 QgsCoordinateReferenceSystem destinationCrs = dCrs;
83
85
86 if ( !showMakeDefault )
87 mCoordinateOperationsWidget->setShowMakeDefault( false );
88
89 if ( forceChoice )
90 {
91 mButtonBox->removeButton( mButtonBox->button( QDialogButtonBox::Cancel ) );
92 setWindowFlags( windowFlags() | Qt::CustomizeWindowHint );
93 setWindowFlags( windowFlags() & ~Qt::WindowCloseButtonHint );
94 }
95
96 if ( !sourceCrs.isValid() )
97 sourceCrs = QgsProject::instance()->crs();
98 if ( !sourceCrs.isValid() )
99 sourceCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
100 if ( !destinationCrs.isValid() )
101 destinationCrs = QgsProject::instance()->crs();
102 if ( !destinationCrs.isValid() )
103 destinationCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
104
105 mSourceProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );
106 mDestinationProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );
107
108 mSourceProjectionSelectionWidget->setCrs( sourceCrs );
109 mDestinationProjectionSelectionWidget->setCrs( destinationCrs );
110 if ( !allowCrsChanges )
111 {
112 mCrsStackedWidget->setCurrentIndex( 1 );
113 mSourceProjectionSelectionWidget->setEnabled( false );
114 mDestinationProjectionSelectionWidget->setEnabled( false );
115 mSourceCrsLabel->setText( QgsProjectionSelectionWidget::crsOptionText( sourceCrs ) );
116 mDestCrsLabel->setText( QgsProjectionSelectionWidget::crsOptionText( destinationCrs ) );
117 }
118
119 mCoordinateOperationsWidget->setMapCanvas( mapCanvas );
120
121 connect( mSourceProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, &QgsDatumTransformDialog::setSourceCrs );
122 connect( mDestinationProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, &QgsDatumTransformDialog::setDestinationCrs );
123
124 mCoordinateOperationsWidget->setSourceCrs( sourceCrs );
125 mCoordinateOperationsWidget->setDestinationCrs( destinationCrs );
126
127 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [=] {
128 QgsHelp::openHelp( QStringLiteral( "working_with_projections/working_with_projections.html" ) );
129 } );
130
131 connect( mCoordinateOperationsWidget, &QgsCoordinateOperationWidget::operationChanged, this, &QgsDatumTransformDialog::operationChanged );
133 deets.proj = selectedProj;
134 deets.sourceTransformId = selectedDatumTransforms.first;
135 deets.destinationTransformId = selectedDatumTransforms.second;
136 deets.allowFallback = allowFallback;
137 mCoordinateOperationsWidget->setSelectedOperation( deets );
138
139 connect( mCoordinateOperationsWidget, &QgsCoordinateOperationWidget::operationDoubleClicked, this, [=] {
140 if ( mCoordinateOperationsWidget->sourceCrs().isValid() && mCoordinateOperationsWidget->destinationCrs().isValid()
141 && mCoordinateOperationsWidget->selectedOperation().isAvailable )
142 accept();
143 } );
144}
145
146void QgsDatumTransformDialog::setOKButtonEnabled()
147{
148 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( mCoordinateOperationsWidget->sourceCrs().isValid() && mCoordinateOperationsWidget->destinationCrs().isValid() && mCoordinateOperationsWidget->selectedOperation().isAvailable );
149}
150
152{
153 if ( mCoordinateOperationsWidget->makeDefaultSelected() && mCoordinateOperationsWidget->hasSelection() )
154 {
155 QgsSettings settings;
156 settings.beginGroup( QStringLiteral( "/Projections" ) );
157
159
160 const QString srcAuthId = dt.sourceCrs.authid();
161 const QString destAuthId = dt.destinationCrs.authid();
162 const int sourceDatumTransform = dt.sourceTransformId;
163 QString sourceDatumProj;
165 if ( sourceDatumTransform >= 0 )
166 sourceDatumProj = QgsDatumTransform::datumTransformToProj( sourceDatumTransform );
167 const int destinationDatumTransform = dt.destinationTransformId;
168 QString destinationDatumProj;
169 if ( destinationDatumTransform >= 0 )
170 destinationDatumProj = QgsDatumTransform::datumTransformToProj( destinationDatumTransform );
172 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_srcTransform" ), sourceDatumProj );
173 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_destTransform" ), destinationDatumProj );
174 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_coordinateOp" ), dt.proj );
175 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_allowFallback" ), dt.allowFallback );
176 }
177 QDialog::accept();
178}
179
181{
182 if ( !mButtonBox->button( QDialogButtonBox::Cancel ) )
183 return; // users HAVE to make a choice, no click on the dialog "x" to avoid this!
184
185 QDialog::reject();
186}
187
188bool QgsDatumTransformDialog::shouldAskUserForSelection() const
189{
190 if ( mCoordinateOperationsWidget->availableOperations().count() > 1 )
191 {
192 return QgsSettings().value( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App ).toBool();
193 }
194 // TODO: show if transform grids are required, but missing
195 return false;
196}
197
198QgsDatumTransformDialog::TransformInfo QgsDatumTransformDialog::defaultDatumTransform() const
199{
200 TransformInfo preferred;
201 preferred.sourceCrs = mCoordinateOperationsWidget->sourceCrs();
202 preferred.destinationCrs = mCoordinateOperationsWidget->destinationCrs();
203 const QgsCoordinateOperationWidget::OperationDetails defaultOp = mCoordinateOperationsWidget->defaultOperation();
204 preferred.sourceTransformId = defaultOp.sourceTransformId;
205 preferred.destinationTransformId = defaultOp.destinationTransformId;
206 preferred.proj = defaultOp.proj;
207 return preferred;
208}
209
210void QgsDatumTransformDialog::applyDefaultTransform()
211{
212 if ( mCoordinateOperationsWidget->availableOperations().count() > 0 )
213 {
215 const TransformInfo dt = defaultDatumTransform();
217 context.addSourceDestinationDatumTransform( dt.sourceCrs, dt.destinationCrs, dt.sourceTransformId, dt.destinationTransformId );
219
220 // on proj 6 builds, removing a coordinate operation falls back to default
221 context.removeCoordinateOperation( dt.sourceCrs, dt.destinationCrs );
223 }
224}
225
227{
228 const QgsCoordinateOperationWidget::OperationDetails selected = mCoordinateOperationsWidget->selectedOperation();
229 TransformInfo sdt;
230 sdt.sourceCrs = mCoordinateOperationsWidget->sourceCrs();
231 sdt.destinationCrs = mCoordinateOperationsWidget->destinationCrs();
234 sdt.proj = selected.proj;
235 sdt.allowFallback = selected.allowFallback;
236 return sdt;
237}
238
239bool QgsDatumTransformDialog::gridShiftTransformation( const QString &itemText ) const
240{
241 return !itemText.isEmpty() && !itemText.contains( QLatin1String( "towgs84" ), Qt::CaseInsensitive );
242}
243
244void QgsDatumTransformDialog::operationChanged()
245{
246 setOKButtonEnabled();
247}
248
249void QgsDatumTransformDialog::setSourceCrs( const QgsCoordinateReferenceSystem &sourceCrs )
250{
251 mCoordinateOperationsWidget->setSourceCrs( sourceCrs );
252 setOKButtonEnabled();
253}
254
255void QgsDatumTransformDialog::setDestinationCrs( const QgsCoordinateReferenceSystem &destinationCrs )
256{
257 mCoordinateOperationsWidget->setDestinationCrs( destinationCrs );
258 setOKButtonEnabled();
259}
void operationChanged()
Emitted when the operation selected in the dialog is changed.
void operationDoubleClicked()
Emitted when an operation is double-clicked in the widget.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Contains information about the context in which a coordinate transform is executed.
Q_DECL_DEPRECATED bool addSourceDestinationDatumTransform(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, int sourceTransformId, int destinationTransformId)
Adds a new sourceTransform and destinationTransform to use when projecting coordinates from the speci...
bool addCoordinateOperation(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &coordinateOperationProjString, bool allowFallback=true)
Adds a new coordinateOperationProjString to use when projecting coordinates from the specified source...
void removeCoordinateOperation(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs)
Removes the coordinate operation for the specified sourceCrs and destinationCrs.
bool hasTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if the context has a valid coordinate operation to use when transforming from the specif...
QgsDatumTransformDialog(const QgsCoordinateReferenceSystem &sourceCrs=QgsCoordinateReferenceSystem(), const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem(), bool allowCrsChanges=false, bool showMakeDefault=true, bool forceChoice=true, QPair< int, int > selectedDatumTransforms=qMakePair(-1, -1), QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags(), const QString &selectedProj=QString(), QgsMapCanvas *mapCanvas=nullptr, bool allowFallback=true)
Constructor for QgsDatumTransformDialog.
TransformInfo selectedDatumTransform()
Returns the source and destination transforms, each being a pair of QgsCoordinateReferenceSystems and...
static bool run(const QgsCoordinateReferenceSystem &sourceCrs=QgsCoordinateReferenceSystem(), const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem(), QWidget *parent=nullptr, QgsMapCanvas *mapCanvas=nullptr, const QString &windowTitle=QString())
Runs the dialog (if required) prompting for the desired transform to use from sourceCrs to destinatio...
static Q_DECL_DEPRECATED QString datumTransformToProj(int datumTransformId)
Returns a proj string representing the specified datumTransformId datum transform ID.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Map canvas is a class for displaying all GIS data types on a canvas.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
Definition qgsproject.h:113
QgsCoordinateReferenceSystem crs
Definition qgsproject.h:112
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the project's coordinate transform context, which stores various information regarding which dat...
@ CrsNotSet
Not set (hidden by default)
static QString crsOptionText(const QgsCoordinateReferenceSystem &crs)
Returns display text for the specified crs.
void crsChanged(const QgsCoordinateReferenceSystem &crs)
Emitted when the selected CRS is changed.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Temporarily removes all cursor overrides for the QApplication for the lifetime of the object.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600
bool allowFallback
true if fallback transforms can be used
QString proj
Proj coordinate operation description, for Proj >= 6.0 builds only.
Dialog transformation entry info.
int destinationTransformId
Destination transform ID.
QString proj
Proj coordinate operation description, for Proj >= 6.0 builds only.
QgsCoordinateReferenceSystem sourceCrs
Source coordinate reference system.
QgsCoordinateReferenceSystem destinationCrs
Destination coordinate reference system.
bool allowFallback
true if fallback transforms can be used