Skip to content

Commit

Permalink
Use simpler approach to test CRS in custom Crs widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 11, 2022
1 parent 1f240c0 commit 8b538c0
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/gui/qgscrsdefinitionwidget.cpp
Expand Up @@ -208,9 +208,7 @@ void QgsCrsDefinitionWidget::formatChanged()
void QgsCrsDefinitionWidget::pbnCalculate_clicked()
{
// We must check the prj def is valid!
PJ_CONTEXT *pContext = QgsProjContext::get();
QString projDef = mTextEditParameters->toPlainText();
QgsDebugMsgLevel( QStringLiteral( "Proj: %1" ).arg( projDef ), 3 );

// Get the WGS84 coordinates
bool okN, okE;
Expand All @@ -226,10 +224,18 @@ void QgsCrsDefinitionWidget::pbnCalculate_clicked()
return;
}

QgsCoordinateReferenceSystem target;
if ( static_cast< Qgis::CrsDefinitionFormat >( mFormatComboBox->currentData().toInt() ) == Qgis::CrsDefinitionFormat::Proj )
{
projDef = projDef + ( projDef.contains( QStringLiteral( "+type=crs" ) ) ? QString() : QStringLiteral( " +type=crs" ) );
QgsProjUtils::proj_pj_unique_ptr res( proj_create_crs_to_crs( pContext, "EPSG:4326", projDef.toUtf8(), nullptr ) );
if ( !res )
target = QgsCoordinateReferenceSystem::fromProj( projDef );
}
else
{
target = QgsCoordinateReferenceSystem::fromWkt( projDef );
}

if ( !target.isValid() )
{
QMessageBox::warning( this, tr( "Custom Coordinate Reference System" ),
tr( "This CRS projection definition is not valid." ) );
Expand All @@ -238,37 +244,20 @@ void QgsCrsDefinitionWidget::pbnCalculate_clicked()
return;
}

// careful -- proj 6 respects CRS axis, so we've got latitude/longitude flowing in, and ....?? coming out?
proj_trans_generic( res.get(), PJ_FWD,
&latitude, sizeof( double ), 1,
&longitude, sizeof( double ), 1,
nullptr, sizeof( double ), 0,
nullptr, sizeof( double ), 0 );
int projResult = proj_errno( res.get() );

if ( projResult != 0 )
const QgsCoordinateTransform transform( target.toGeodeticCrs(), target, QgsCoordinateTransformContext() );
try
{
mProjectedXLabel->setText( tr( "Error" ) );
mProjectedYLabel->setText( tr( "Error" ) );
QgsDebugMsg( proj_errno_string( projResult ) );
const QgsPointXY res = transform.transform( QgsPointXY( longitude, latitude ) );
const int precision = target.isGeographic() ? 7 : 4;
mProjectedXLabel->setText( QLocale().toString( res.x(), 'f', precision ) );
mProjectedYLabel->setText( QLocale().toString( res.y(), 'f', precision ) );
}
else
catch ( QgsCsException &e )
{
QString tmp;

int precision = 4;
bool isLatLong = false;

isLatLong = QgsProjUtils::usesAngularUnit( projDef );
if ( isLatLong )
{
precision = 7;
}

tmp = QLocale().toString( longitude, 'f', precision );
mProjectedXLabel->setText( tmp );
tmp = QLocale().toString( latitude, 'f', precision );
mProjectedYLabel->setText( tmp );
mProjectedXLabel->setText( tr( "Error" ) );
mProjectedYLabel->setText( tr( "Error" ) );
QMessageBox::warning( this, tr( "Custom Coordinate Reference System" ),
e.what() );
}
}

Expand Down

0 comments on commit 8b538c0

Please sign in to comment.