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 );
117 formatCoordinatePartsForProject( project, point, destCrs,
precision, formattedX, formattedY );
119 if ( formattedX.isEmpty() || formattedY.isEmpty() )
166 if ( destCrs !=
crs )
171 p = ct.transform( point );
182 format->setNumberDecimalPlaces(
precision );
186 x = format->formatDouble( p.
x(), context );
188 y = format->formatDouble( p.
y(), context );
193 x = QgsCoordinateFormatter::formatAsPair( p.
x(),
precision );
194 y = QgsCoordinateFormatter::formatAsPair( p.
y(),
precision );
202 return QStringLiteral(
"%1 : %2" ).arg( QgsCoordinateUtils::formatCoordinateForProject( project, p1, destCrs,
precision ),
203 QgsCoordinateUtils::formatCoordinateForProject( project, p2, destCrs,
precision ) );
206double QgsCoordinateUtils::degreeToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
208 const QString negative( QStringLiteral(
"swSW" ) );
209 const QString easting( QStringLiteral(
"eEwW" ) );
211 bool okValue =
false;
222 const QLocale locale;
223 QRegularExpression degreeWithSuffix( QStringLiteral(
"^\\s*([-]?\\d{1,3}(?:[\\.\\%1]\\d+)?)\\s*([NSEWnsew])\\s*$" )
224 .arg( locale.decimalPoint() ) );
225 QRegularExpressionMatch match = degreeWithSuffix.match(
string );
226 if ( match.hasMatch() )
228 const QString suffix = match.captured( 2 );
229 value = std::abs( match.captured( 1 ).toDouble( ok ) );
232 value = std::abs( locale.toDouble( match.captured( 1 ), ok ) );
236 value *= ( negative.contains( suffix ) ? -1 : 1 );
239 *isEasting = easting.contains( suffix );
246double QgsCoordinateUtils::dmsToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
248 const QString negative( QStringLiteral(
"swSW-" ) );
249 const QString easting( QStringLiteral(
"eEwW" ) );
251 bool okValue =
false;
262 const QLocale locale;
263 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*$" )
264 .arg( locale.decimalPoint() ), QRegularExpression::CaseInsensitiveOption );
265 const QRegularExpressionMatch match = dms.match(
string.trimmed() );
266 if ( match.hasMatch() )
268 const QString dms1 = match.captured( 2 );
269 const QString dms2 = match.captured( 3 );
270 const QString dms3 = match.captured( 4 );
272 double v = dms3.toDouble( ok );
275 v = locale.toDouble( dms3, ok );
280 if ( !dms2.isEmpty() )
282 v = dms2.toInt( ok ) + v / 60.0;
286 v = dms1.toInt( ok ) + v / 60.0;
290 const QString sign1 = match.captured( 1 );
291 const QString sign2 = match.captured( 5 );
293 if ( sign1.isEmpty() )
295 value = !sign2.isEmpty() && negative.contains( sign2 ) ? -v : v;
298 *isEasting = easting.contains( sign2 );
301 else if ( sign2.isEmpty() )
303 value = !sign1.isEmpty() && negative.contains( sign1 ) ? -v : v;
306 *isEasting = easting.contains( sign2 );
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic)
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.
Q_GADGET QgsUnitTypes::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 yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
DistanceUnit
Units of distance.
@ DistanceDegrees
Degrees, for planar geographic CRS distance measurements.
#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