19#include "moc_qgscoordinateutils.cpp"
32#include <QRegularExpression>
41 const bool automatic = project->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
60 dp =
static_cast<int>( std::ceil( -1.0 * std::log10( mapUnitsPerPixel ) ) );
77 dp = project->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ) );
94 const bool automatic = prj->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
97 return prj->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ), 6 );
100 return calculateCoordinatePrecision(
crs );
123 formatCoordinatePartsForProject( project, point, destCrs,
precision, formattedX, formattedY );
125 if ( formattedX.isEmpty() || formattedY.isEmpty() )
172 if ( destCrs !=
crs )
177 p = ct.transform( point );
188 format->setNumberDecimalPlaces(
precision );
192 x = format->formatDouble( p.
x(), context );
194 y = format->formatDouble( p.
y(), context );
199 x = QgsCoordinateFormatter::formatAsPair( p.
x(),
precision );
200 y = QgsCoordinateFormatter::formatAsPair( p.
y(),
precision );
208 return QStringLiteral(
"%1 : %2" ).arg( QgsCoordinateUtils::formatCoordinateForProject( project, p1, destCrs,
precision ),
209 QgsCoordinateUtils::formatCoordinateForProject( project, p2, destCrs,
precision ) );
212double QgsCoordinateUtils::degreeToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
214 const QString negative( QStringLiteral(
"swSW" ) );
215 const QString easting( QStringLiteral(
"eEwW" ) );
217 bool okValue =
false;
228 const QLocale locale;
229 QRegularExpression degreeWithSuffix( QStringLiteral(
"^\\s*([-]?\\d{1,3}(?:[\\.\\%1]\\d+)?)\\s*([NSEWnsew])\\s*$" )
230 .arg( locale.decimalPoint() ) );
231 QRegularExpressionMatch match = degreeWithSuffix.match(
string );
232 if ( match.hasMatch() )
234 const QString suffix = match.captured( 2 );
235 value = std::abs( match.captured( 1 ).toDouble( ok ) );
238 value = std::abs( locale.toDouble( match.captured( 1 ), ok ) );
242 value *= ( negative.contains( suffix ) ? -1 : 1 );
245 *isEasting = easting.contains( suffix );
252double QgsCoordinateUtils::dmsToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
254 const QString negative( QStringLiteral(
"swSW-" ) );
255 const QString easting( QStringLiteral(
"eEwW" ) );
257 bool okValue =
false;
268 const QLocale locale;
269 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*$" )
270 .arg( locale.decimalPoint() ), QRegularExpression::CaseInsensitiveOption );
271 const QRegularExpressionMatch match = dms.match(
string.trimmed() );
272 if ( match.hasMatch() )
274 const QString dms1 = match.captured( 2 );
275 const QString dms2 = match.captured( 3 );
276 const QString dms3 = match.captured( 4 );
278 double v = dms3.toDouble( ok );
281 v = locale.toDouble( dms3, ok );
286 if ( !dms2.isEmpty() )
288 v = dms2.toInt( ok ) + v / 60.0;
292 v = dms1.toInt( ok ) + v / 60.0;
296 const QString sign1 = match.captured( 1 );
297 const QString sign2 = match.captured( 5 );
299 if ( sign1.isEmpty() )
301 value = !sign2.isEmpty() && negative.contains( sign2 ) ? -v : v;
304 *isEasting = easting.contains( sign2 );
307 else if ( sign2.isEmpty() )
309 value = !sign1.isEmpty() && negative.contains( sign1 ) ? -v : v;
312 *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