Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove coordinate formatting methods from QgsPointXY
Use QgsCoordinateFormatter instead
  • Loading branch information
nyalldawson committed Nov 15, 2017
1 parent 0d7b828 commit 95765a1
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 984 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -1963,6 +1963,7 @@ QgsPoint {#qgis_api_break_3_0_QgsPoint}
--------

- onSegment() has been removed. Use sqrDistToSegment() instead for a more precise test.
- toDegreesMinutesSeconds() and toDegreesMinutes() have been removed. Use QgsCoordinateFormatter instead.


QgsPointDisplacementRenderer {#qgis_api_break_3_0_QgsPointDisplacementRenderer}
Expand Down
15 changes: 15 additions & 0 deletions python/core/qgscoordinateformatter.sip
Expand Up @@ -74,6 +74,18 @@ class QgsCoordinateFormatter
:rtype: str
%End

static QString format( QgsPointXY point, Format format, int precision = 12, FormatFlags flags = FlagDegreesUseStringSuffix );
%Docstring
Formats a ``point`` according to the specified parameters.

The ``format`` argument indicates the desired display format for the coordinate.

The ``precision`` argument gives the number of decimal places to include for coordinates.

Optional ``flags`` can be specified to control the output format.
:rtype: str
%End

static QString asPair( double x, double y, int precision = 12 );
%Docstring
Formats coordinates as an "``x``,``y``" pair, with optional decimal ``precision`` (number
Expand All @@ -83,6 +95,9 @@ class QgsCoordinateFormatter

};

QFlags<QgsCoordinateFormatter::FormatFlag> operator|(QgsCoordinateFormatter::FormatFlag f1, QFlags<QgsCoordinateFormatter::FormatFlag> f2);


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
37 changes: 2 additions & 35 deletions python/core/qgspointxy.sip
Expand Up @@ -105,45 +105,12 @@ Sets the x and y value of the point
:rtype: QPointF
%End

QString toString() const;
QString toString( int precision = 12 ) const;
%Docstring
String representation of the point (x,y)
Returns a string representation of the point (x, y) with a preset ``precision``.
:rtype: str
%End

QString toString( int precision ) const;
%Docstring
As above but with precision for string representation of a point
:rtype: str
%End

QString toDegreesMinutesSeconds( int precision, const bool useSuffix = true, const bool padded = false ) const;
%Docstring
Return a string representation as degrees minutes seconds.
Its up to the calling function to ensure that this point can
be meaningfully represented in this form.
\param precision number of decimal points to use for seconds
\param useSuffix set to true to include a direction suffix (e.g., 'N'),
set to false to use a "-" prefix for west and south coordinates
\param padded set to true to force minutes and seconds to use two decimals,
e.g., '05' instead of '5'.
:rtype: str
%End

QString toDegreesMinutes( int precision, const bool useSuffix = true, const bool padded = false ) const;
%Docstring
Return a string representation as degrees minutes.
Its up to the calling function to ensure that this point can
be meaningfully represented in this form.
\param precision number of decimal points to use for minutes
\param useSuffix set to true to include a direction suffix (e.g., 'N'),
set to false to use a "-" prefix for west and south coordinates
\param padded set to true to force minutes to use two decimals,
e.g., '05' instead of '5'.
:rtype: str
%End


QString wellKnownText() const;
%Docstring
Return the well known text representation for the point.
Expand Down
85 changes: 46 additions & 39 deletions src/core/composer/qgscomposermapgrid.cpp
Expand Up @@ -33,7 +33,7 @@
#include "qgsexpressioncontext.h"
#include "qgsexception.h"
#include "qgssettings.h"

#include "qgscoordinateformatter.h"
#include <QPainter>
#include <QPen>

Expand Down Expand Up @@ -1484,49 +1484,56 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
return mGridAnnotationExpression->evaluate( &expressionContext ).toString();
}

QgsPointXY p;
p.setX( coord == QgsComposerMapGrid::Longitude ? value : 0 );
p.setY( coord == QgsComposerMapGrid::Longitude ? 0 : value );

QString annotationString;
if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinute )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision );
}
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinuteNoSuffix )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision, false );
}
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinutePadded )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision, true, true );
}
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinuteSecond )
QgsCoordinateFormatter::Format format = QgsCoordinateFormatter::FormatDecimalDegrees;
QgsCoordinateFormatter::FormatFlags flags = 0;
switch ( mGridAnnotationFormat )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision );
}
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinuteSecondNoSuffix )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision, false );
}
else if ( mGridAnnotationFormat == QgsComposerMapGrid::DegreeMinuteSecondPadded )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision, true, true );
}
case Decimal:
case DecimalWithSuffix:
case CustomFormat:
break; // already handled above

QStringList split = annotationString.split( ',' );
if ( coord == QgsComposerMapGrid::Longitude )
{
return split.at( 0 );
case DegreeMinute:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix;
break;

case DegreeMinuteSecond:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix;
break;

case DegreeMinuteNoSuffix:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = 0;
break;

case DegreeMinutePadded:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix | QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds;
break;

case DegreeMinuteSecondNoSuffix:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = 0;
break;

case DegreeMinuteSecondPadded:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix | QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds;
break;
}
else

switch ( coord )
{
if ( split.size() < 2 )
{
return QLatin1String( "" );
}
return split.at( 1 );
case Longitude:
return QgsCoordinateFormatter::formatX( value, format, flags );

case Latitude:
return QgsCoordinateFormatter::formatY( value, format, flags );
}

return QString(); // no warnings
}

int QgsComposerMapGrid::xGridLines( QList< QPair< double, QLineF > > &lines ) const
Expand Down
84 changes: 46 additions & 38 deletions src/core/layout/qgslayoutitemmapgrid.cpp
Expand Up @@ -33,6 +33,7 @@
#include "qgsexpressioncontext.h"
#include "qgsexception.h"
#include "qgssettings.h"
#include "qgscoordinateformatter.h"

#include <QPainter>
#include <QPen>
Expand Down Expand Up @@ -1410,49 +1411,56 @@ QString QgsLayoutItemMapGrid::gridAnnotationString( double value, QgsLayoutItemM
return mGridAnnotationExpression->evaluate( &expressionContext ).toString();
}

QgsPointXY p;
p.setX( coord == QgsLayoutItemMapGrid::Longitude ? value : 0 );
p.setY( coord == QgsLayoutItemMapGrid::Longitude ? 0 : value );

QString annotationString;
if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinute )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision );
}
else if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinuteNoSuffix )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision, false );
}
else if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinutePadded )
{
annotationString = p.toDegreesMinutes( mGridAnnotationPrecision, true, true );
}
else if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinuteSecond )
QgsCoordinateFormatter::Format format = QgsCoordinateFormatter::FormatDecimalDegrees;
QgsCoordinateFormatter::FormatFlags flags = 0;
switch ( mGridAnnotationFormat )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision );
}
else if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinuteSecondNoSuffix )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision, false );
}
else if ( mGridAnnotationFormat == QgsLayoutItemMapGrid::DegreeMinuteSecondPadded )
{
annotationString = p.toDegreesMinutesSeconds( mGridAnnotationPrecision, true, true );
}
case Decimal:
case DecimalWithSuffix:
case CustomFormat:
break; // already handled above

QStringList split = annotationString.split( ',' );
if ( coord == QgsLayoutItemMapGrid::Longitude )
{
return split.at( 0 );
case DegreeMinute:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix;
break;

case DegreeMinuteSecond:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix;
break;

case DegreeMinuteNoSuffix:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = 0;
break;

case DegreeMinutePadded:
format = QgsCoordinateFormatter::FormatDegreesMinutes;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix | QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds;
break;

case DegreeMinuteSecondNoSuffix:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = 0;
break;

case DegreeMinuteSecondPadded:
format = QgsCoordinateFormatter::FormatDegreesMinutesSeconds;
flags = QgsCoordinateFormatter::FlagDegreesUseStringSuffix | QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds;
break;
}
else

switch ( coord )
{
if ( split.size() < 2 )
{
return QLatin1String( "" );
}
return split.at( 1 );
case Longitude:
return QgsCoordinateFormatter::formatX( value, format, flags );

case Latitude:
return QgsCoordinateFormatter::formatY( value, format, flags );
}

return QString(); // no warnings
}

int QgsLayoutItemMapGrid::xGridLines( QList< QPair< double, QLineF > > &lines ) const
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgscoordinateformatter.cpp
Expand Up @@ -58,6 +58,12 @@ QString QgsCoordinateFormatter::formatY( double y, QgsCoordinateFormatter::Forma
return QString(); //avoid warnings
}

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

QString QgsCoordinateFormatter::asPair( double x, double y, int precision )
{
QString s = formatAsPair( x, precision );
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgscoordinateformatter.h
Expand Up @@ -19,6 +19,8 @@
#define QGSCOORDINATEFORMATTER_H

#include <QString>
#include "qgis.h"
#include "qgspointxy.h"

/**
* \ingroup core
Expand Down Expand Up @@ -86,6 +88,17 @@ class CORE_EXPORT QgsCoordinateFormatter
*/
static QString formatY( double y, Format format, int precision = 12, FormatFlags flags = FlagDegreesUseStringSuffix );

/**
* Formats a \a point according to the specified parameters.
*
* The \a format argument indicates the desired display format for the coordinate.
*
* The \a precision argument gives the number of decimal places to include for coordinates.
*
* Optional \a flags can be specified to control the output format.
*/
static QString format( QgsPointXY point, Format format, int precision = 12, FormatFlags flags = FlagDegreesUseStringSuffix );

/**
* Formats coordinates as an "\a x,\a y" pair, with optional decimal \a precision (number
* of decimal places to include).
Expand All @@ -106,4 +119,6 @@ class CORE_EXPORT QgsCoordinateFormatter
static QString formatYAsDegrees( double val, int precision, FormatFlags flags );
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsCoordinateFormatter::FormatFlags )

#endif // QGSCOORDINATEFORMATTER_H
10 changes: 5 additions & 5 deletions src/core/qgscoordinateutils.cpp
Expand Up @@ -21,7 +21,7 @@
#include "qgsproject.h"
#include "qgis.h"
#include "qgsexception.h"

#include "qgscoordinateformatter.h"
///@cond NOT_STABLE_API

int QgsCoordinateUtils::calculateCoordinatePrecision( double mapUnitsPerPixel, const QgsCoordinateReferenceSystem &mapCrs )
Expand Down Expand Up @@ -85,16 +85,16 @@ QString QgsCoordinateUtils::formatCoordinateForProject( const QgsPointXY &point,
}

if ( format == QLatin1String( "DM" ) )
return geo.toDegreesMinutes( precision, true, true );
return QgsCoordinateFormatter::format( geo, QgsCoordinateFormatter::FormatDegreesMinutes, precision, QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds | QgsCoordinateFormatter::FlagDegreesUseStringSuffix );
else if ( format == QLatin1String( "DMS" ) )
return geo.toDegreesMinutesSeconds( precision, true, true );
return QgsCoordinateFormatter::format( geo, QgsCoordinateFormatter::FormatDegreesMinutesSeconds, precision, QgsCoordinateFormatter::FlagDegreesPadMinutesSeconds | QgsCoordinateFormatter::FlagDegreesUseStringSuffix );
else
return geo.toString( precision );
return QgsCoordinateFormatter::asPair( geo.x(), geo.y(), precision );
}
else
{
// coordinates in map units
return point.toString( precision );
return QgsCoordinateFormatter::asPair( point.x(), point.y(), precision );
}
}

Expand Down

0 comments on commit 95765a1

Please sign in to comment.