32#include <QRegularExpression>
35#include "moc_qgscoordinateutils.cpp"
37using namespace Qt::StringLiterals;
46 const bool automatic = project->
readBoolEntry( u
"PositionPrecision"_s, u
"/Automatic"_s );
65 dp =
static_cast<int>( std::ceil( -1.0 * std::log10( mapUnitsPerPixel ) ) );
82 dp = project->
readNumEntry( u
"PositionPrecision"_s, u
"/DecimalPlaces"_s );
99 const bool automatic = prj->
readBoolEntry( u
"PositionPrecision"_s, u
"/Automatic"_s );
102 return prj->
readNumEntry( u
"PositionPrecision"_s, u
"/DecimalPlaces"_s, 6 );
105 return calculateCoordinatePrecision( crs );
128 formatCoordinatePartsForProject( project, point, destCrs, precision, formattedX, formattedY );
130 if ( formattedX.isEmpty() || formattedY.isEmpty() )
168 x = QgsCoordinateFormatter::formatAsPair( point.
x(), precision );
169 y = QgsCoordinateFormatter::formatAsPair( point.
y(), precision );
179 if ( destCrs != crs )
184 p = ct.transform( point );
195 format->setNumberDecimalPlaces( precision );
199 x = format->formatDouble( p.
x(), context );
201 y = format->formatDouble( p.
y(), context );
206 x = QgsCoordinateFormatter::formatAsPair( p.
x(), precision );
207 y = QgsCoordinateFormatter::formatAsPair( p.
y(), precision );
215 return u
"%1 : %2"_s.arg( QgsCoordinateUtils::formatCoordinateForProject( project, p1, destCrs, precision ),
216 QgsCoordinateUtils::formatCoordinateForProject( project, p2, destCrs, precision ) );
219double QgsCoordinateUtils::degreeToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
221 const QString negative( u
"swSW"_s );
222 const QString easting( u
"eEwW"_s );
224 bool okValue =
false;
235 const QLocale locale;
236 QRegularExpression degreeWithSuffix( u
"^\\s*([-]?\\d{1,3}(?:[\\.\\%1]\\d+)?)\\s*([NSEWnsew])\\s*$"_s
237 .arg( locale.decimalPoint() ) );
238 QRegularExpressionMatch match = degreeWithSuffix.match(
string );
239 if ( match.hasMatch() )
241 const QString suffix = match.captured( 2 );
242 value = std::abs( match.captured( 1 ).toDouble( ok ) );
245 value = std::abs( locale.toDouble( match.captured( 1 ), ok ) );
249 value *= ( negative.contains( suffix ) ? -1 : 1 );
252 *isEasting = easting.contains( suffix );
259double QgsCoordinateUtils::dmsToDecimal(
const QString &
string,
bool *ok,
bool *isEasting )
261 const QString negative( u
"swSW-"_s );
262 const QString easting( u
"eEwW"_s );
264 bool okValue =
false;
275 const QLocale locale;
276 const QRegularExpression dms( u
"^\\s*(?:([-+nsew])\\s*)?(\\d{1,3})(?:[^0-9.]+([0-5]?\\d))?[^0-9.]+([0-5]?\\d(?:[\\.\\%1]\\d+)?)[^0-9.,]*?([-+nsew])?\\s*$"_s
277 .arg( locale.decimalPoint() ), QRegularExpression::CaseInsensitiveOption );
278 const QRegularExpressionMatch match = dms.match(
string.trimmed() );
279 if ( match.hasMatch() )
281 const QString dms1 = match.captured( 2 );
282 const QString dms2 = match.captured( 3 );
283 const QString dms3 = match.captured( 4 );
285 double v = dms3.toDouble( ok );
288 v = locale.toDouble( dms3, ok );
293 if ( !dms2.isEmpty() )
295 v = dms2.toInt( ok ) + v / 60.0;
299 v = dms1.toInt( ok ) + v / 60.0;
303 const QString sign1 = match.captured( 1 );
304 const QString sign2 = match.captured( 5 );
306 if ( sign1.isEmpty() )
308 value = !sign2.isEmpty() && negative.contains( sign2 ) ? -v : v;
311 *isEasting = easting.contains( sign2 );
314 else if ( sign2.isEmpty() )
316 value = !sign1.isEmpty() && negative.contains( sign1 ) ? -v : v;
319 *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).