Skip to content
Permalink
Browse files
Merge pull request #43756 from elpaso/localization-coordinate-formatter
Coordinate formatter localization
  • Loading branch information
elpaso committed Jun 21, 2021
2 parents 310f5f3 + 71f350c commit 00536e869f2a528752d45fdbd2978f1bbd219929
@@ -87,6 +87,14 @@ Optional ``flags`` can be specified to control the output format.
%Docstring
Formats coordinates as an "``x``,``y``" pair, with optional decimal ``precision`` (number
of decimal places to include).
%End

static QChar separator( );
%Docstring
Returns the character used as X/Y separator, this is a `,` on locales that do not use
`,` as decimal separator, it is a space otherwise.

.. versionadded:: 3.20
%End

};
@@ -19,6 +19,7 @@
#include "qgis.h"

#include <QObject> // for tr()
#include <QLocale>

QString QgsCoordinateFormatter::formatX( double x, QgsCoordinateFormatter::Format format, int precision, FormatFlags flags )
{
@@ -60,21 +61,27 @@ QString QgsCoordinateFormatter::formatY( double y, QgsCoordinateFormatter::Forma

QString QgsCoordinateFormatter::format( const QgsPointXY &point, QgsCoordinateFormatter::Format format, int precision, FormatFlags flags )
{
return QStringLiteral( "%1,%2" ).arg( formatX( point.x(), format, precision, flags ),
formatY( point.y(), format, precision, flags ) );
return QStringLiteral( "%1%2%3" ).arg( formatX( point.x(), format, precision, flags ),
QgsCoordinateFormatter::separator(),
formatY( point.y(), format, precision, flags ) );
}

QString QgsCoordinateFormatter::asPair( double x, double y, int precision )
{
QString s = formatAsPair( x, precision );
s += ',';
s += QgsCoordinateFormatter::separator();
s += formatAsPair( y, precision );
return s;
}

QChar QgsCoordinateFormatter::separator()
{
return QLocale().decimalPoint() == QLatin1Char( ',' ) ? QLatin1Char( ' ' ) : QLatin1Char( ',' );
}

QString QgsCoordinateFormatter::formatAsPair( double val, int precision )
{
return std::isfinite( val ) ? QString::number( val, 'f', precision ) : QObject::tr( "infinite" );
return std::isfinite( val ) ? QLocale().toString( val, 'f', precision ) : QObject::tr( "infinite" );
}

QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int precision, FormatFlags flags )
@@ -120,7 +127,7 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -143,17 +150,17 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutesSeconds( double val, int
//pad with leading digits if required
if ( flags.testFlag( FlagDegreesPadMinutesSeconds ) )
{
minutesX = QString( "%1" ).arg( intMinutesX, 2, 10, QChar( '0' ) );
minutesX = QString( "%L1" ).arg( intMinutesX, 2, 10, QChar( '0' ) );
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
strSecondsX = QString( "%1" ).arg( secondsX, digits, 'f', precision, QChar( '0' ) );
strSecondsX = QString( "%L1" ).arg( secondsX, digits, 'f', precision, QChar( '0' ) );
}
else
{
minutesX = QString::number( intMinutesX );
strSecondsX = QString::number( secondsX, 'f', precision );
minutesX = QLocale().toString( intMinutesX );
strSecondsX = QLocale().toString( secondsX, 'f', precision );
}

return sign + QString::number( degreesX ) + QChar( 176 ) +
return sign + QLocale().toString( degreesX ) + QChar( 176 ) +
minutesX + QChar( 0x2032 ) +
strSecondsX + QChar( 0x2033 ) +
hemisphere;
@@ -202,7 +209,7 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutesSeconds( double val, int
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -219,17 +226,17 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutesSeconds( double val, int
//pad with leading digits if required
if ( flags.testFlag( FlagDegreesPadMinutesSeconds ) )
{
strMinutesY = QString( "%1" ).arg( intMinutesY, 2, 10, QChar( '0' ) );
strMinutesY = QString( "%L1" ).arg( intMinutesY, 2, 10, QChar( '0' ) );
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
strSecondsY = QString( "%1" ).arg( secondsY, digits, 'f', precision, QChar( '0' ) );
strSecondsY = QString( "%L1" ).arg( secondsY, digits, 'f', precision, QChar( '0' ) );
}
else
{
strMinutesY = QString::number( intMinutesY );
strSecondsY = QString::number( secondsY, 'f', precision );
strMinutesY = QLocale().toString( intMinutesY );
strSecondsY = QLocale().toString( secondsY, 'f', precision );
}

return sign + QString::number( degreesY ) + QChar( 176 ) +
return sign + QLocale().toString( degreesY ) + QChar( 176 ) +
strMinutesY + QChar( 0x2032 ) +
strSecondsY + QChar( 0x2033 ) +
hemisphere;
@@ -271,7 +278,7 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutes( double val, int precisi
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -291,9 +298,9 @@ QString QgsCoordinateFormatter::formatXAsDegreesMinutes( double val, int precisi
//pad minutes with leading digits if required
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
QString strMinutesX = flags.testFlag( FlagDegreesPadMinutesSeconds ) ? QString( "%1" ).arg( floatMinutesX, digits, 'f', precision, QChar( '0' ) )
: QString::number( floatMinutesX, 'f', precision );
: QLocale().toString( floatMinutesX, 'f', precision );

return sign + QString::number( degreesX ) + QChar( 176 ) +
return sign + QLocale().toString( degreesX ) + QChar( 176 ) +
strMinutesX + QChar( 0x2032 ) +
hemisphere;
}
@@ -334,7 +341,7 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutes( double val, int precisi
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -349,9 +356,9 @@ QString QgsCoordinateFormatter::formatYAsDegreesMinutes( double val, int precisi
//pad minutes with leading digits if required
int digits = 2 + ( precision == 0 ? 0 : 1 + precision ); //1 for decimal place if required
QString strMinutesY = flags.testFlag( FlagDegreesPadMinutesSeconds ) ? QString( "%1" ).arg( floatMinutesY, digits, 'f', precision, QChar( '0' ) )
: QString::number( floatMinutesY, 'f', precision );
: QLocale().toString( floatMinutesY, 'f', precision );

return sign + QString::number( degreesY ) + QChar( 176 ) +
return sign + QLocale().toString( degreesY ) + QChar( 176 ) +
strMinutesY + QChar( 0x2032 ) +
hemisphere;
}
@@ -384,7 +391,7 @@ QString QgsCoordinateFormatter::formatXAsDegrees( double val, int precision, For
{
if ( wrappedX < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -402,7 +409,7 @@ QString QgsCoordinateFormatter::formatXAsDegrees( double val, int precision, For
hemisphere.clear();
}

return sign + QString::number( absX, 'f', precision ) + QChar( 176 ) + hemisphere;
return sign + QLocale().toString( absX, 'f', precision ) + QChar( 176 ) + hemisphere;
}

QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, FormatFlags flags )
@@ -433,7 +440,7 @@ QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, For
{
if ( wrappedY < 0 )
{
sign = QObject::tr( "-" );
sign = QLocale().negativeSign();
}
}
//check if coordinate is all zeros for the specified precision, and if so,
@@ -444,5 +451,5 @@ QString QgsCoordinateFormatter::formatYAsDegrees( double val, int precision, For
hemisphere.clear();
}

return sign + QString::number( absY, 'f', precision ) + QChar( 176 ) + hemisphere;
return sign + QLocale().toString( absY, 'f', precision ) + QChar( 176 ) + hemisphere;
}
@@ -105,6 +105,13 @@ class CORE_EXPORT QgsCoordinateFormatter
*/
static QString asPair( double x, double y, int precision = 12 );

/**
* Returns the character used as X/Y separator, this is a `,` on locales that do not use
* `,` as decimal separator, it is a space otherwise.
* \since QGIS 3.20
*/
static QChar separator( );

private:

static QString formatAsPair( double val, int precision );
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsapplication.h"
#include "qgscoordinateformatter.h"
#include "qgsdistancearea.h"
#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
@@ -748,13 +749,13 @@ QString QgsMapToolIdentify::formatCoordinate( const QgsPointXY &canvasPoint ) co
QString QgsMapToolIdentify::formatXCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 0 );
return coordinate.split( QgsCoordinateFormatter::separator() ).at( 0 );
}

QString QgsMapToolIdentify::formatYCoordinate( const QgsPointXY &canvasPoint ) const
{
QString coordinate = formatCoordinate( canvasPoint );
return coordinate.split( ',' ).at( 1 );
return coordinate.split( QgsCoordinateFormatter::separator() ).at( 1 );
}

QMap< QString, QString > QgsMapToolIdentify::featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint )
Loading

0 comments on commit 00536e8

Please sign in to comment.