32#include <QRegularExpression>
34#include "moc_qgscoordinateutils.cpp"
43 const bool automatic = project->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
62 dp =
static_cast<int>( std::ceil( -1.0 * std::log10( mapUnitsPerPixel ) ) );
79 dp = project->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ) );
96 const bool automatic = prj->
readBoolEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/Automatic" ) );
99 return prj->
readNumEntry( QStringLiteral(
"PositionPrecision" ), QStringLiteral(
"/DecimalPlaces" ), 6 );
102 return calculateCoordinatePrecision( crs );
125 formatCoordinatePartsForProject( project, point, destCrs, precision, formattedX, formattedY );
127 if ( formattedX.isEmpty() || formattedY.isEmpty() )
165 x = QgsCoordinateFormatter::formatAsPair( point.
x(), precision );
166 y = QgsCoordinateFormatter::formatAsPair( point.
y(), precision );
176 if ( destCrs != crs )
181 p = ct.transform( point );
192 format->setNumberDecimalPlaces( precision );
196 x = format->formatDouble( p.
x(), context );
198 y = format->formatDouble( p.
y(), context );
203 x = QgsCoordinateFormatter::formatAsPair( p.
x(), precision );
204 y = QgsCoordinateFormatter::formatAsPair( p.
y(), precision );
212 return QStringLiteral(
"%1 : %2" ).arg( QgsCoordinateUtils::formatCoordinateForProject( project, p1, destCrs, precision ),
213 QgsCoordinateUtils::formatCoordinateForProject( project, p2, destCrs, precision ) );
216double QgsCoordinateUtils::degreeToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
218 const QString negative( QStringLiteral(
"swSW" ) );
219 const QString easting( QStringLiteral(
"eEwW" ) );
221 bool okValue =
false;
232 const QLocale locale;
233 QRegularExpression degreeWithSuffix( QStringLiteral(
"^\\s*([-]?\\d{1,3}(?:[\\.\\%1]\\d+)?)\\s*([NSEWnsew])\\s*$" )
234 .arg( locale.decimalPoint() ) );
235 QRegularExpressionMatch match = degreeWithSuffix.match(
string );
236 if ( match.hasMatch() )
238 const QString suffix = match.captured( 2 );
239 value = std::abs( match.captured( 1 ).toDouble( ok ) );
242 value = std::abs( locale.toDouble( match.captured( 1 ), ok ) );
246 value *= ( negative.contains( suffix ) ? -1 : 1 );
249 *isEasting = easting.contains( suffix );
256double QgsCoordinateUtils::dmsToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
258 const QString negative( QStringLiteral(
"swSW-" ) );
259 const QString easting( QStringLiteral(
"eEwW" ) );
261 bool okValue =
false;
272 const QLocale locale;
273 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*$" )
274 .arg( locale.decimalPoint() ), QRegularExpression::CaseInsensitiveOption );
275 const QRegularExpressionMatch match = dms.match(
string.trimmed() );
276 if ( match.hasMatch() )
278 const QString dms1 = match.captured( 2 );
279 const QString dms2 = match.captured( 3 );
280 const QString dms3 = match.captured( 4 );
282 double v = dms3.toDouble( ok );
285 v = locale.toDouble( dms3, ok );
290 if ( !dms2.isEmpty() )
292 v = dms2.toInt( ok ) + v / 60.0;
296 v = dms1.toInt( ok ) + v / 60.0;
300 const QString sign1 = match.captured( 1 );
301 const QString sign2 = match.captured( 5 );
303 if ( sign1.isEmpty() )
305 value = !sign2.isEmpty() && negative.contains( sign2 ) ? -v : v;
308 *isEasting = easting.contains( sign2 );
311 else if ( sign2.isEmpty() )
313 value = !sign1.isEmpty() && negative.contains( sign1 ) ? -v : v;
316 *isEasting = easting.contains( sign1 );
@ 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.
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.
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.
#define BUILTIN_UNREACHABLE
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).