31#include <QRegularExpression>
40 const bool automatic = project->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
59 dp =
static_cast<int>( std::ceil( -1.0 * std::log10( mapUnitsPerPixel ) ) );
76 dp = project->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ) );
93 const bool automatic = prj->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
96 return prj->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ), 6 );
99 return calculateCoordinatePrecision(
crs );
122 formatCoordinatePartsForProject( project, point, destCrs,
precision, formattedX, formattedY );
124 if ( formattedX.isEmpty() || formattedY.isEmpty() )
171 if ( destCrs !=
crs )
176 p = ct.transform( point );
187 format->setNumberDecimalPlaces(
precision );
191 x = format->formatDouble( p.
x(), context );
193 y = format->formatDouble( p.
y(), context );
198 x = QgsCoordinateFormatter::formatAsPair( p.
x(),
precision );
199 y = QgsCoordinateFormatter::formatAsPair( p.
y(),
precision );
207 return QStringLiteral(
"%1 : %2" ).arg( QgsCoordinateUtils::formatCoordinateForProject( project, p1, destCrs,
precision ),
208 QgsCoordinateUtils::formatCoordinateForProject( project, p2, destCrs,
precision ) );
211double QgsCoordinateUtils::degreeToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
213 const QString negative( QStringLiteral(
"swSW" ) );
214 const QString easting( QStringLiteral(
"eEwW" ) );
216 bool okValue =
false;
227 const QLocale locale;
228 QRegularExpression degreeWithSuffix( QStringLiteral(
"^\\s*([-]?\\d{1,3}(?:[\\.\\%1]\\d+)?)\\s*([NSEWnsew])\\s*$" )
229 .arg( locale.decimalPoint() ) );
230 QRegularExpressionMatch match = degreeWithSuffix.match(
string );
231 if ( match.hasMatch() )
233 const QString suffix = match.captured( 2 );
234 value = std::abs( match.captured( 1 ).toDouble( ok ) );
237 value = std::abs( locale.toDouble( match.captured( 1 ), ok ) );
241 value *= ( negative.contains( suffix ) ? -1 : 1 );
244 *isEasting = easting.contains( suffix );
251double QgsCoordinateUtils::dmsToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
253 const QString negative( QStringLiteral(
"swSW-" ) );
254 const QString easting( QStringLiteral(
"eEwW" ) );
256 bool okValue =
false;
267 const QLocale locale;
268 const QRegularExpression dms( QStringLiteral(
"^\\s*(?:([-+nsew])\\s*)?(\\d{1,3})(?:[^0-9.]+([0-5]?\\d))?[^0-9.]+([0-5]?\\d(?:[\\.\\%1]\\d+)?)[^0-9.,]*?([-+nsew])?\\s*$" )
269 .arg( locale.decimalPoint() ), QRegularExpression::CaseInsensitiveOption );
270 const QRegularExpressionMatch match = dms.match(
string.trimmed() );
271 if ( match.hasMatch() )
273 const QString dms1 = match.captured( 2 );
274 const QString dms2 = match.captured( 3 );
275 const QString dms3 = match.captured( 4 );
277 double v = dms3.toDouble( ok );
280 v = locale.toDouble( dms3, ok );
285 if ( !dms2.isEmpty() )
287 v = dms2.toInt( ok ) + v / 60.0;
291 v = dms1.toInt( ok ) + v / 60.0;
295 const QString sign1 = match.captured( 1 );
296 const QString sign2 = match.captured( 5 );
298 if ( sign1.isEmpty() )
300 value = !sign2.isEmpty() && negative.contains( sign2 ) ? -v : v;
303 *isEasting = easting.contains( sign2 );
306 else if ( sign2.isEmpty() )
308 value = !sign1.isEmpty() && negative.contains( sign1 ) ? -v : v;
311 *isEasting = easting.contains( sign2 );
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic)
DistanceUnit
Units of distance.
@ Degrees
Degrees, for planar geographic CRS distance measurements.
CoordinateOrder
Order of coordinates.
@ XY
Easting/Northing (or Longitude/Latitude for geographic CRS)
@ Default
Respect the default axis ordering for the CRS, as defined in the CRS's parameters.
@ YX
Northing/Easting (or Latitude/Longitude for geographic CRS)
static Qgis::CoordinateOrder defaultCoordinateOrderForCrs(const QgsCoordinateReferenceSystem &crs)
Returns the default coordinate order to use for the specified crs.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Qgis::DistanceUnit mapUnits
Custom exception class for Coordinate Reference System related exceptions.
A context for numeric formats.
void setInterpretation(Interpretation interpretation)
Sets the interpretation of the numbers being converted.
@ Latitude
Latitude values.
@ Longitude
Longitude values.
A class to represent a 2D point.
const QgsGeographicCoordinateNumericFormat * geographicCoordinateFormat() const
Returns the project's geographic coordinate format, which controls how geographic coordinates associa...
QgsCoordinateReferenceSystem coordinateCustomCrs
Qgis::CoordinateOrder coordinateAxisOrder
Qgis::CoordinateDisplayType coordinateType
QgsCoordinateReferenceSystem coordinateCrs
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsProjectDisplaySettings * displaySettings
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Reads a boolean from the specified scope and key.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
#define BUILTIN_UNREACHABLE
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
const QgsCoordinateReferenceSystem & crs