19#include "moc_qgscoordinateoperationwidget.cpp"
32#include <QRegularExpression>
42 mLabelSrcDescription->setTextInteractionFlags( Qt::TextBrowserInteraction );
43 mLabelSrcDescription->setOpenExternalLinks(
true );
44 mInstallGridButton->hide();
46 connect( mInstallGridButton, &QPushButton::clicked,
this, &QgsCoordinateOperationWidget::installGrid );
47 connect( mAllowFallbackCheckBox, &QCheckBox::toggled,
this, [ = ]
52 mCoordinateOperationTableWidget->setColumnCount( 3 );
55 headers << tr(
"Transformation" ) << tr(
"Accuracy (meters)" ) << tr(
"Area of Use" );
56 mCoordinateOperationTableWidget->setHorizontalHeaderLabels( headers );
58 mHideDeprecatedCheckBox->setVisible(
false );
59 mShowSupersededCheckBox->setVisible(
true );
60 mLabelDstDescription->hide();
62 connect( mHideDeprecatedCheckBox, &QCheckBox::stateChanged,
this, [ = ] { loadAvailableOperations(); } );
63 connect( mShowSupersededCheckBox, &QCheckBox::toggled,
this, &QgsCoordinateOperationWidget::showSupersededToggled );
64 connect( mCoordinateOperationTableWidget, &QTableWidget::currentItemChanged,
this, &QgsCoordinateOperationWidget::tableCurrentItemChanged );
67 mLabelSrcDescription->clear();
68 mLabelDstDescription->clear();
79 mainCanvasPoly << mainCanvasPoly.at( 0 );
102 mMakeDefaultCheckBox->setVisible( show );
107 return mMakeDefaultCheckBox->isChecked();
112 return !mCoordinateOperationTableWidget->selectedItems().isEmpty();
117 QList<QgsCoordinateOperationWidget::OperationDetails> res;
118 res.reserve( mDatumTransforms.size() );
122 op.
proj = details.proj;
131void QgsCoordinateOperationWidget::loadAvailableOperations()
133 mCoordinateOperationTableWidget->setRowCount( 0 );
136 int preferredInitialRow = -1;
140 std::unique_ptr< QTableWidgetItem > item = std::make_unique< QTableWidgetItem >();
141 item->setData( ProjRole, transform.proj );
142 item->setData( AvailableRole, transform.isAvailable );
143 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
145 QString name = transform.name;
146 if ( !transform.authority.isEmpty() && !transform.code.isEmpty() )
147 name += QStringLiteral(
" %1 %2:%3" ).arg( QString( QChar( 0x2013 ) ), transform.authority, transform.code );
148 item->setText( name );
152 QFont f = item->font();
155 item->setForeground( QBrush( QColor( 0, 120, 0 ) ) );
158 if ( !transform.isAvailable )
160 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
163 if ( preferredInitialRow < 0 && transform.isAvailable )
166 preferredInitialRow = row;
169 QString missingMessage;
170 if ( !transform.isAvailable )
172 QStringList gridMessages;
173 QStringList missingGrids;
174 QStringList missingGridPackages;
175 QStringList missingGridUrls;
179 if ( !grid.isAvailable )
181 missingGrids << grid.shortName;
182 missingGridPackages << grid.packageName;
183 missingGridUrls << grid.url;
184 QString m = tr(
"This transformation requires the grid file “%1”, which is not available for use on the system." ).arg( grid.shortName );
185 if ( !grid.url.isEmpty() )
187 if ( !grid.packageName.isEmpty() )
189 m +=
' ' + tr(
"This grid is part of the <i>%1</i> package, available for download from <a href=\"%2\">%2</a>." ).arg( grid.packageName, grid.url );
193 m +=
' ' + tr(
"This grid is available for download from <a href=\"%1\">%1</a>." ).arg( grid.url );
200 item->setData( MissingGridsRole, missingGrids );
201 item->setData( MissingGridPackageNamesRole, missingGridPackages );
202 item->setData( MissingGridUrlsRole, missingGridUrls );
204 if ( gridMessages.count() > 1 )
206 for (
int k = 0; k < gridMessages.count(); ++k )
207 gridMessages[k] = QStringLiteral(
"<li>%1</li>" ).arg( gridMessages.at( k ) );
209 missingMessage = QStringLiteral(
"<ul>%1</ul" ).arg( gridMessages.join( QString() ) );
211 else if ( !gridMessages.empty() )
213 missingMessage = gridMessages.constFirst();
217 QStringList areasOfUse;
218 QStringList authorityCodes;
221 QString lastSingleOpScope;
222 QString lastSingleOpRemarks;
226 if ( !singleOpDetails.scope.isEmpty() )
228 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Scope" ), formatScope( singleOpDetails.scope ) );
229 lastSingleOpScope = singleOpDetails.scope;
231 if ( !singleOpDetails.remarks.isEmpty() )
233 if ( !text.isEmpty() )
234 text += QLatin1String(
"<br>" );
235 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Remarks" ), singleOpDetails.remarks );
236 lastSingleOpRemarks = singleOpDetails.remarks;
238 if ( !singleOpDetails.areaOfUse.isEmpty() )
240 if ( !areasOfUse.contains( singleOpDetails.areaOfUse ) )
241 areasOfUse << singleOpDetails.areaOfUse;
243 if ( !singleOpDetails.authority.isEmpty() && !singleOpDetails.code.isEmpty() )
245 const QString identifier = QStringLiteral(
"%1:%2" ).arg( singleOpDetails.authority, singleOpDetails.code );
246 if ( !authorityCodes.contains( identifier ) )
247 authorityCodes << identifier;
250 if ( !text.isEmpty() )
252 opText.append( text );
257 if ( !transform.scope.isEmpty() && transform.scope != lastSingleOpScope )
259 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Scope" ), transform.scope );
261 if ( !transform.remarks.isEmpty() && transform.remarks != lastSingleOpRemarks )
263 if ( !text.isEmpty() )
264 text += QLatin1String(
"<br>" );
265 text += QStringLiteral(
"<b>%1</b>: %2" ).arg( tr(
"Remarks" ), transform.remarks );
267 if ( !text.isEmpty() )
269 opText.append( text );
272 if ( opText.count() > 1 )
274 for (
int k = 0; k < opText.count(); ++k )
275 opText[k] = QStringLiteral(
"<li>%1</li>" ).arg( opText.at( k ) );
278 if ( !transform.areaOfUse.isEmpty() && !areasOfUse.contains( transform.areaOfUse ) )
279 areasOfUse << transform.areaOfUse;
280 item->setData( BoundsRole, transform.bounds );
282 const QString
id = !transform.authority.isEmpty() && !transform.code.isEmpty() ? QStringLiteral(
"%1:%2" ).arg( transform.authority, transform.code ) : QString();
283 if ( !
id.isEmpty() && !authorityCodes.contains(
id ) )
284 authorityCodes << id;
286 const QColor disabled = palette().color( QPalette::Disabled, QPalette::Text );
287 const QColor active = palette().color( QPalette::Active, QPalette::Text );
289 const QColor codeColor(
static_cast< int >( active.red() * 0.6 + disabled.red() * 0.4 ),
290 static_cast< int >( active.green() * 0.6 + disabled.green() * 0.4 ),
291 static_cast< int >( active.blue() * 0.6 + disabled.blue() * 0.4 ) );
292 const QString toolTipString = QStringLiteral(
"<b>%1</b>" ).arg( transform.name )
293 + ( !opText.empty() ? ( opText.count() == 1 ? QStringLiteral(
"<p>%1</p>" ).arg( opText.at( 0 ) ) : QStringLiteral(
"<ul>%1</ul>" ).arg( opText.join( QString() ) ) ) : QString() )
294 + ( !areasOfUse.empty() ? QStringLiteral(
"<p><b>%1</b>: %2</p>" ).arg( tr(
"Area of use" ), areasOfUse.join( QLatin1String(
", " ) ) ) : QString() )
295 + ( !authorityCodes.empty() ? QStringLiteral(
"<p><b>%1</b>: %2</p>" ).arg( tr(
"Identifiers" ), authorityCodes.join( QLatin1String(
", " ) ) ) : QString() )
296 + ( !missingMessage.isEmpty() ? QStringLiteral(
"<p><b style=\"color: red\">%1</b></p>" ).arg( missingMessage ) : QString() )
297 + QStringLiteral(
"<p><code style=\"color: %1\">%2</code></p>" ).arg( codeColor.name(), transform.proj );
299 item->setToolTip( toolTipString );
300 mCoordinateOperationTableWidget->setRowCount( row + 1 );
301 mCoordinateOperationTableWidget->setItem( row, 0, item.release() );
303 item = std::make_unique< QTableWidgetItem >();
304 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
305 item->setText( transform.accuracy >= 0 ? QLocale().toString( transform.accuracy ) : tr(
"Unknown" ) );
306 item->setToolTip( toolTipString );
307 if ( !transform.isAvailable )
309 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
311 mCoordinateOperationTableWidget->setItem( row, 1, item.release() );
314 item = std::make_unique< QTableWidgetItem >();
315 item->setFlags( item->flags() & ~Qt::ItemIsEditable );
316 item->setText( areasOfUse.join( QLatin1String(
", " ) ) );
317 item->setToolTip( toolTipString );
318 if ( !transform.isAvailable )
320 item->setForeground( QBrush( palette().color( QPalette::Disabled, QPalette::Text ) ) );
322 mCoordinateOperationTableWidget->setItem( row, 2, item.release() );
327 if ( mCoordinateOperationTableWidget->currentRow() < 0 )
328 mCoordinateOperationTableWidget->selectRow( preferredInitialRow >= 0 ? preferredInitialRow : 0 );
330 mCoordinateOperationTableWidget->resizeColumnsToContents();
332 tableCurrentItemChanged(
nullptr,
nullptr );
338 settings.
setValue( QStringLiteral(
"Windows/DatumTransformDialog/hideDeprecated" ), mHideDeprecatedCheckBox->isChecked() );
340 for (
int i = 0; i < 2; i++ )
342 settings.
setValue( QStringLiteral(
"Windows/DatumTransformDialog/columnWidths/%1" ).arg( i ), mCoordinateOperationTableWidget->columnWidth( i ) );
353 if ( transform.isAvailable )
355 preferred.
proj = transform.proj;
363QString QgsCoordinateOperationWidget::formatScope(
const QString &s )
367 const thread_local QRegularExpression reGNSS( QStringLiteral(
"\\bGNSS\\b" ) );
368 scope.replace( reGNSS, QObject::tr(
"GNSS (Global Navigation Satellite System)" ) );
370 const thread_local QRegularExpression reCORS( QStringLiteral(
"\\bCORS\\b" ) );
371 scope.replace( reCORS, QObject::tr(
"CORS (Continually Operating Reference Station)" ) );
378 int row = mCoordinateOperationTableWidget->currentRow();
383 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
385 QTableWidgetItem *destItem = mCoordinateOperationTableWidget->item( row, 1 );
387 op.
proj = srcItem ? srcItem->data( ProjRole ).toString() : QString();
388 op.
isAvailable = srcItem ? srcItem->data( AvailableRole ).toBool() :
true;
402 int prevRow = mCoordinateOperationTableWidget->currentRow();
404 for (
int row = 0; row < mCoordinateOperationTableWidget->rowCount(); ++row )
406 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
407 if ( srcItem && srcItem->data( ProjRole ).toString() == operation.
proj )
409 mCoordinateOperationTableWidget->selectRow( row );
414 bool fallbackChanged = mAllowFallbackCheckBox->isChecked() != operation.
allowFallback;
415 mAllowFallbackCheckBox->setChecked( operation.
allowFallback );
418 if ( mCoordinateOperationTableWidget->currentRow() != prevRow || fallbackChanged )
440 mAllowFallbackCheckBox->setVisible( visible );
443bool QgsCoordinateOperationWidget::gridShiftTransformation(
const QString &itemText )
const
445 return !itemText.isEmpty() && !itemText.contains( QLatin1String(
"towgs84" ), Qt::CaseInsensitive );
448bool QgsCoordinateOperationWidget::testGridShiftFileAvailability( QTableWidgetItem *item )
const
455 QString itemText = item->text();
456 if ( itemText.isEmpty() )
461 char *projLib = getenv(
"PROJ_LIB" );
467 QStringList itemEqualSplit = itemText.split(
'=' );
469 for (
int i = 1; i < itemEqualSplit.size(); ++i )
473 filename.append(
'=' );
475 filename.append( itemEqualSplit.at( i ) );
478 QDir projDir( projLib );
479 if ( projDir.exists() )
482 QStringList fileList = projDir.entryList();
483 QStringList::const_iterator fileIt = fileList.constBegin();
484 for ( ; fileIt != fileList.constEnd(); ++fileIt )
487 if ( fileIt->compare( filename, Qt::CaseInsensitive ) == 0 )
489 if ( fileIt->compare( filename ) == 0 )
495 item->setToolTip( tr(
"File '%1' not found in directory '%2'" ).arg( filename, projDir.absolutePath() ) );
501void QgsCoordinateOperationWidget::tableCurrentItemChanged( QTableWidgetItem *, QTableWidgetItem * )
503 int row = mCoordinateOperationTableWidget->currentRow();
506 mLabelSrcDescription->clear();
507 mLabelDstDescription->clear();
509 mInstallGridButton->hide();
513 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
514 mLabelSrcDescription->setText( srcItem ? srcItem->toolTip() : QString() );
525 mAreaCanvas->setPreviewRect( rect );
528 const QStringList missingGrids = srcItem->data( MissingGridsRole ).toStringList();
529 mInstallGridButton->setVisible( !missingGrids.empty() );
530 if ( !missingGrids.empty() )
532 mInstallGridButton->setText( tr(
"Install “%1” Grid…" ).arg( missingGrids.at( 0 ) ) );
539 mInstallGridButton->hide();
541 QTableWidgetItem *destItem = mCoordinateOperationTableWidget->item( row, 1 );
542 mLabelDstDescription->setText( destItem ? destItem->toolTip() : QString() );
545 if ( newOp.proj != mPreviousOp.
proj && !mBlockSignals )
554 loadAvailableOperations();
561 loadAvailableOperations();
564void QgsCoordinateOperationWidget::showSupersededToggled(
bool )
567 loadAvailableOperations();
570void QgsCoordinateOperationWidget::installGrid()
572 int row = mCoordinateOperationTableWidget->currentRow();
573 QTableWidgetItem *srcItem = mCoordinateOperationTableWidget->item( row, 0 );
577 const QStringList missingGrids = srcItem->data( MissingGridsRole ).toStringList();
578 if ( missingGrids.empty() )
581 const QStringList missingGridPackagesNames = srcItem->data( MissingGridPackageNamesRole ).toStringList();
582 const QString packageName = missingGridPackagesNames.value( 0 );
583 const QStringList missingGridUrls = srcItem->data( MissingGridUrlsRole ).toStringList();
584 const QString gridUrl = missingGridUrls.value( 0 );
586 QString downloadMessage;
587 if ( !packageName.isEmpty() )
589 downloadMessage = tr(
"This grid is part of the “<i>%1</i>” package, available for download from <a href=\"%2\">%2</a>." ).arg( packageName, gridUrl );
591 else if ( !gridUrl.isEmpty() )
593 downloadMessage = tr(
"This grid is available for download from <a href=\"%1\">%1</a>." ).arg( gridUrl );
596 const QString longMessage = tr(
"<p>This transformation requires the grid file “%1”, which is not available for use on the system.</p>" ).arg( missingGrids.at( 0 ) );
598 QgsInstallGridShiftFileDialog *dlg =
new QgsInstallGridShiftFileDialog( missingGrids.at( 0 ),
this );
599 dlg->setAttribute( Qt::WA_DeleteOnClose );
600 dlg->setWindowTitle( tr(
"Install Grid File" ) );
601 dlg->setDescription( longMessage );
602 dlg->setDownloadMessage( downloadMessage );
This class represents a coordinate reference system (CRS).
QgsRectangle bounds() const
Returns the approximate bounds for the region the CRS is usable within.
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Contains information about the context in which a coordinate transform is executed.
bool allowFallbackTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if approximate "ballpark" transforms may be used when transforming between a source and ...
QString calculateCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the Proj coordinate operation string to use when transforming from the specified source CRS t...
Custom exception class for Coordinate Reference System related exceptions.
A geometry is the spatial representation of a feature.
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QPolygonF visiblePolygon() const
Returns the visible area as a polygon (may be rotated)
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
This class is a composition of two QSettings instances:
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
const QgsCoordinateReferenceSystem & crs