Skip to content
Permalink
Browse files

[locator] add degree minute second coordinate support to goto locator

  • Loading branch information
nirvn committed Jul 31, 2020
1 parent 6916305 commit e0de9c5c26c919cbdb3b9ecc3875ad4320d318b4
Showing with 55 additions and 39 deletions.
  1. +49 −39 src/app/locator/qgsinbuiltlocatorfilters.cpp
  2. +6 −0 tests/src/app/testqgsapplocatorfilters.cpp
@@ -734,6 +734,7 @@ void QgsGotoLocatorFilter::fetchResults( const QString &string, const QgsLocator
bool okY = false;
double posX = 0.0;
double posY = 0.0;
bool posIsDms = false;
QLocale locale;

// Coordinates such as 106.8468,-6.3804
@@ -752,58 +753,67 @@ void QgsGotoLocatorFilter::fetchResults( const QString &string, const QgsLocator
// Digit detection using user locale failed, use default C decimal separators
separatorRx = QRegularExpression( QStringLiteral( "^([0-9\\-\\.]*)[\\s\\,]*([0-9\\-\\.]*)$" ) );
match = separatorRx.match( string.trimmed() );
if ( match.hasMatch() )
{
posX = match.captured( 1 ).toDouble( &okX );
posY = match.captured( 2 ).toDouble( &okY );
}
}

if ( match.hasMatch() )
if ( !match.hasMatch() )
{
if ( !okX || !okY )
// Check if the string is a pair of degree minute second
separatorRx = QRegularExpression( QStringLiteral( "^((?:([-+nsew])\\s*)?\\d{1,3}(?:[^0-9.]+[0-5]?\\d)?[^0-9.]+[0-5]?\\d(?:\\.\\d+)?[^0-9.]*[-+nsew]?)\\s+((?:([-+nsew])\\s*)?\\d{1,3}(?:[^0-9.]+[0-5]?\\d)?[^0-9.]+[0-5]?\\d(?:\\.\\d+)?[^0-9.]*[-+nsew]?)$" ) );
match = separatorRx.match( string.trimmed() );
if ( match.hasMatch() )
{
posX = match.captured( 1 ).toDouble( &okX );
posY = match.captured( 2 ).toDouble( &okY );
posIsDms = true;
posX = QgsCoordinateUtils::dmsToDecimal( match.captured( 1 ), &okX );
posY = QgsCoordinateUtils::dmsToDecimal( match.captured( 3 ), &okY );
}
}

if ( okX && okY )
if ( okX && okY )
{
QVariantMap data;
QgsPointXY point( posX, posY );
data.insert( QStringLiteral( "point" ), point );

bool withinWgs84 = wgs84Crs.bounds().contains( point );
if ( !posIsDms && currentCrs != wgs84Crs )
{
QVariantMap data;
QgsPointXY point( posX, posY );
data.insert( QStringLiteral( "point" ), point );
QgsLocatorResult result;
result.filter = this;
result.displayString = tr( "Go to %1 %2 (Map CRS, %3)" ).arg( locale.toString( point.x(), 'g', 10 ), locale.toString( point.y(), 'g', 10 ), currentCrs.userFriendlyIdentifier() );
result.userData = data;
result.score = 0.9;
emit resultFetched( result );
}

bool withinWgs84 = wgs84Crs.bounds().contains( point );
if ( withinWgs84 )
{
if ( currentCrs != wgs84Crs )
{
QgsLocatorResult result;
result.filter = this;
result.displayString = tr( "Go to %1 %2 (Map CRS, %3)" ).arg( locale.toString( point.x(), 'g', 10 ), locale.toString( point.y(), 'g', 10 ), currentCrs.userFriendlyIdentifier() );
result.userData = data;
result.score = 0.9;
emit resultFetched( result );
}

if ( withinWgs84 )
{
if ( currentCrs != wgs84Crs )
QgsCoordinateTransform transform( wgs84Crs, currentCrs, QgsProject::instance()->transformContext() );
QgsPointXY transformedPoint;
try
{
QgsCoordinateTransform transform( wgs84Crs, currentCrs, QgsProject::instance()->transformContext() );
QgsPointXY transformedPoint;
try
{
transformedPoint = transform.transform( point );
}
catch ( const QgsException &e )
{
Q_UNUSED( e )
return;
}
data[QStringLiteral( "point" )] = transformedPoint;
transformedPoint = transform.transform( point );
}

QgsLocatorResult result;
result.filter = this;
result.displayString = tr( "Go to %1° %2° (%3)" ).arg( locale.toString( point.x(), 'g', 10 ), locale.toString( point.y(), 'g', 10 ), wgs84Crs.userFriendlyIdentifier() );
result.userData = data;
result.score = 1.0;
emit resultFetched( result );
catch ( const QgsException &e )
{
Q_UNUSED( e )
return;
}
data[QStringLiteral( "point" )] = transformedPoint;
}

QgsLocatorResult result;
result.filter = this;
result.displayString = tr( "Go to %1° %2° (%3)" ).arg( locale.toString( point.x(), 'g', 10 ), locale.toString( point.y(), 'g', 10 ), wgs84Crs.userFriendlyIdentifier() );
result.userData = data;
result.score = 1.0;
emit resultFetched( result );
}
return;
}
@@ -291,6 +291,12 @@ void TestQgsAppLocatorFilters::testGoto()
QCOMPARE( results.at( 0 ).displayString, QObject::tr( "Go to 1,234.56 789.012 (Map CRS, )" ) );
QCOMPARE( results.at( 0 ).userData.toMap()[QStringLiteral( "point" )].value<QgsPointXY>(), QgsPointXY( 1234.56, 789.012 ) );

// degree/minuste/second coordinates goto
results = gatherResults( &filter, QStringLiteral( "40deg 1' 0\" E 11deg 55' 0\" S" ), QgsLocatorContext() );
QCOMPARE( results.count(), 1 );
QCOMPARE( results.at( 0 ).displayString, QObject::tr( "Go to 40.01666667° -11.91666667° (EPSG:4326 - WGS 84)" ) );
QCOMPARE( results.at( 0 ).userData.toMap()[QStringLiteral( "point" )].value<QgsPointXY>(), QgsPointXY( 40.0166666667, -11.9166666667 ) );

// OSM/Leaflet/OpenLayers
results = gatherResults( &filter, QStringLiteral( "https://www.openstreetmap.org/#map=15/44.5546/6.4936" ), QgsLocatorContext() );
QCOMPARE( results.count(), 1 );

0 comments on commit e0de9c5

Please sign in to comment.
You can’t perform that action at this time.