QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
76 const QgsCoordinateReferenceSystem &dCrs, const bool allowCrsChanges, const bool showMakeDefault, const bool forceChoice,
77 QPair<int, int> selectedDatumTransforms,
78 QWidget *parent,
79 Qt::WindowFlags f, const QString &selectedProj, QgsMapCanvas *mapCanvas, bool allowFallback )
80 : QDialog( parent, f )
81 , mPreviousCursorOverride( std::make_unique< QgsTemporaryCursorRestoreOverride >() ) // this dialog is often shown while cursor overrides are in place, so temporarily remove them
82{
83 setupUi( this );
84
85 QgsCoordinateReferenceSystem sourceCrs = sCrs;
86 QgsCoordinateReferenceSystem destinationCrs = dCrs;
87
89
90 if ( !showMakeDefault )
91 mCoordinateOperationsWidget->setShowMakeDefault( false );
92
93 if ( forceChoice )
94 {
95 mButtonBox->removeButton( mButtonBox->button( QDialogButtonBox::Cancel ) );
96 setWindowFlags( windowFlags() | Qt::CustomizeWindowHint );
97 setWindowFlags( windowFlags() & ~Qt::WindowCloseButtonHint );
98 }
99
100 if ( !sourceCrs.isValid() )
101 sourceCrs = QgsProject::instance()->crs();
102 if ( !sourceCrs.isValid() )
103 sourceCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
104 if ( !destinationCrs.isValid() )
105 destinationCrs = QgsProject::instance()->crs();
106 if ( !destinationCrs.isValid() )
107 destinationCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
108
109 mSourceProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );
110 mDestinationProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );
111
112 mSourceProjectionSelectionWidget->setCrs( sourceCrs );
113 mDestinationProjectionSelectionWidget->setCrs( destinationCrs );
114 if ( !allowCrsChanges )
115 {
116 mCrsStackedWidget->setCurrentIndex( 1 );
117 mSourceProjectionSelectionWidget->setEnabled( false );
118 mDestinationProjectionSelectionWidget->setEnabled( false );
119 mSourceCrsLabel->setText( QgsProjectionSelectionWidget::crsOptionText( sourceCrs ) );
120 mDestCrsLabel->setText( QgsProjectionSelectionWidget::crsOptionText( destinationCrs ) );
121 }
122
123 mCoordinateOperationsWidget->setMapCanvas( mapCanvas );
124
125 connect( mSourceProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, &QgsDatumTransformDialog::setSourceCrs );
126 connect( mDestinationProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, &QgsDatumTransformDialog::setDestinationCrs );
127
128 mCoordinateOperationsWidget->setSourceCrs( sourceCrs );
129 mCoordinateOperationsWidget->setDestinationCrs( destinationCrs );
130
131 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [ = ]
132 {
133 QgsHelp::openHelp( QStringLiteral( "working_with_projections/working_with_projections.html" ) );
134 } );
135
136 connect( mCoordinateOperationsWidget, &QgsCoordinateOperationWidget::operationChanged, this, &QgsDatumTransformDialog::operationChanged );
138 deets.proj = selectedProj;
139 deets.sourceTransformId = selectedDatumTransforms.first;
140 deets.destinationTransformId = selectedDatumTransforms.second;
141 deets.allowFallback = allowFallback;
142 mCoordinateOperationsWidget->setSelectedOperation( deets );
143
144 connect( mCoordinateOperationsWidget, &QgsCoordinateOperationWidget::operationDoubleClicked, this, [ = ]
145 {
146 if ( mCoordinateOperationsWidget->sourceCrs().isValid() && mCoordinateOperationsWidget->destinationCrs().isValid()
147 && mCoordinateOperationsWidget->selectedOperation().isAvailable )
148 accept();
149 } );
150}
151
152void QgsDatumTransformDialog::setOKButtonEnabled()
153{
154 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( mCoordinateOperationsWidget->sourceCrs().isValid() && mCoordinateOperationsWidget->destinationCrs().isValid()
155 && mCoordinateOperationsWidget->selectedOperation().isAvailable );
156}
157
159{
160 if ( mCoordinateOperationsWidget->makeDefaultSelected() && mCoordinateOperationsWidget->hasSelection() )
161 {
162 QgsSettings settings;
163 settings.beginGroup( QStringLiteral( "/Projections" ) );
164
166
167 const QString srcAuthId = dt.sourceCrs.authid();
168 const QString destAuthId = dt.destinationCrs.authid();
169 const int sourceDatumTransform = dt.sourceTransformId;
170 QString sourceDatumProj;
172 if ( sourceDatumTransform >= 0 )
173 sourceDatumProj = QgsDatumTransform::datumTransformToProj( sourceDatumTransform );
174 const int destinationDatumTransform = dt.destinationTransformId;
175 QString destinationDatumProj;
176 if ( destinationDatumTransform >= 0 )
177 destinationDatumProj = QgsDatumTransform::datumTransformToProj( destinationDatumTransform );
179 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_srcTransform" ), sourceDatumProj );
180 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_destTransform" ), destinationDatumProj );
181 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_coordinateOp" ), dt.proj );
182 settings.setValue( srcAuthId + QStringLiteral( "//" ) + destAuthId + QStringLiteral( "_allowFallback" ), dt.allowFallback );
183 }
184 QDialog::accept();
185}
186
188{
189 if ( !mButtonBox->button( QDialogButtonBox::Cancel ) )
190 return; // users HAVE to make a choice, no click on the dialog "x" to avoid this!
191
192 QDialog::reject();
193}
194
195bool QgsDatumTransformDialog::shouldAskUserForSelection() const
196{
197 if ( mCoordinateOperationsWidget->availableOperations().count() > 1 )
198 {
199 return QgsSettings().value( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App ).toBool();
200 }
201 // TODO: show if transform grids are required, but missing
202 return false;
203}
204
205QgsDatumTransformDialog::TransformInfo QgsDatumTransformDialog::defaultDatumTransform() const
206{
207 TransformInfo preferred;
208 preferred.sourceCrs = mCoordinateOperationsWidget->sourceCrs();
209 preferred.destinationCrs = mCoordinateOperationsWidget->destinationCrs();
210 const QgsCoordinateOperationWidget::OperationDetails defaultOp = mCoordinateOperationsWidget->defaultOperation();
211 preferred.sourceTransformId = defaultOp.sourceTransformId;
212 preferred.destinationTransformId = defaultOp.destinationTransformId;
213 preferred.proj = defaultOp.proj;
214 return preferred;
215}
216
217void QgsDatumTransformDialog::applyDefaultTransform()
218{
219 if ( mCoordinateOperationsWidget->availableOperations().count() > 0 )
220 {
222 const TransformInfo dt = defaultDatumTransform();
224 context.addSourceDestinationDatumTransform( dt.sourceCrs, dt.destinationCrs, dt.sourceTransformId, dt.destinationTransformId );
226
227 // on proj 6 builds, removing a coordinate operation falls back to default
228 context.removeCoordinateOperation( dt.sourceCrs, dt.destinationCrs );
230 }
231}
232
234{
235 const QgsCoordinateOperationWidget::OperationDetails selected = mCoordinateOperationsWidget->selectedOperation();
236 TransformInfo sdt;
237 sdt.sourceCrs = mCoordinateOperationsWidget->sourceCrs();
238 sdt.destinationCrs = mCoordinateOperationsWidget->destinationCrs();
241 sdt.proj = selected.proj;
242 sdt.allowFallback = selected.allowFallback;
243 return sdt;
244}
245
246bool QgsDatumTransformDialog::gridShiftTransformation( const QString &itemText ) const
247{
248 return !itemText.isEmpty() && !itemText.contains( QLatin1String( "towgs84" ), Qt::CaseInsensitive );
249}
250
251void QgsDatumTransformDialog::operationChanged()
252{
253 setOKButtonEnabled();
254}
255
256void QgsDatumTransformDialog::setSourceCrs( const QgsCoordinateReferenceSystem &sourceCrs )
257{
258 mCoordinateOperationsWidget->setSourceCrs( sourceCrs );
259 setOKButtonEnabled();
260}
261
262void QgsDatumTransformDialog::setDestinationCrs( const QgsCoordinateReferenceSystem &destinationCrs )
263{
264 mCoordinateOperationsWidget->setDestinationCrs( destinationCrs );
265 setOKButtonEnabled();
266}
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:209
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:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
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