28#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
30bool qMapLessThanKey<QPair<QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem>>(
const QPair<QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem> &key1,
31 const QPair<QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem> &key2 )
33 const QPair< QString, QString > key1String = qMakePair(
crsToKey( key1.first ),
crsToKey( key1.second ) );
34 const QPair< QString, QString > key2String = qMakePair(
crsToKey( key2.first ),
crsToKey( key2.second ) );
35 return key1String < key2String;
40 : d( new QgsCoordinateTransformContextPrivate() )
60 d->mLock.lockForRead();
61 rhs.d->mLock.lockForRead();
62 const bool equal = d->mSourceDestDatumTransforms == rhs.d->mSourceDestDatumTransforms;
64 rhs.d->mLock.unlock();
72 d->mLock.lockForWrite();
73 d->mSourceDestDatumTransforms.clear();
84 d->mLock.lockForRead();
85 auto res = d->mSourceDestDatumTransforms;
88 QMap<QPair<QString, QString>, QString> results;
89 for (
auto it = res.constBegin(); it != res.constEnd(); ++it )
90 results.insert( qMakePair( it.key().first.authid(), it.key().second.authid() ), it.value().operation );
99 Q_UNUSED( sourceTransform )
100 Q_UNUSED( destinationTransform )
109 d->mLock.lockForWrite();
110 QgsCoordinateTransformContextPrivate::OperationDetails details;
111 details.operation = coordinateOperationProjString;
112 details.allowFallback = allowFallback;
113 d->mSourceDestDatumTransforms.insert( qMakePair( sourceCrs, destinationCrs ), details );
125 d->mSourceDestDatumTransforms.remove( qMakePair( sourceCrs, destinationCrs ) );
137 Q_UNUSED( destination )
146 d->mLock.lockForRead();
148 auto it = d->mSourceDestDatumTransforms.constFind( qMakePair( source, destination ) );
149 if ( it == d->mSourceDestDatumTransforms.constEnd() )
152 it = d->mSourceDestDatumTransforms.constFind( qMakePair( destination, source ) );
155 const QString result = it == d->mSourceDestDatumTransforms.constEnd() ? QString() : it.value().operation;
165 d->mLock.lockForRead();
166 QgsCoordinateTransformContextPrivate::OperationDetails res = d->mSourceDestDatumTransforms.value( qMakePair( source, destination ), QgsCoordinateTransformContextPrivate::OperationDetails() );
167 if ( res.operation.isEmpty() )
170 res = d->mSourceDestDatumTransforms.value( qMakePair( destination, source ), QgsCoordinateTransformContextPrivate::OperationDetails() );
173 return res.allowFallback;
181 d->mLock.lockForRead();
182 QgsCoordinateTransformContextPrivate::OperationDetails res = d->mSourceDestDatumTransforms.value( qMakePair( source, destination ), QgsCoordinateTransformContextPrivate::OperationDetails() );
183 if ( !res.operation.isEmpty() )
189 res = d->mSourceDestDatumTransforms.value( qMakePair( destination, source ), QgsCoordinateTransformContextPrivate::OperationDetails() );
190 if ( !res.operation.isEmpty() )
203 d->mLock.lockForWrite();
205 d->mSourceDestDatumTransforms.clear();
207 const QDomNodeList contextNodes = element.elementsByTagName( QStringLiteral(
"transformContext" ) );
208 if ( contextNodes.count() < 1 )
214 missingTransforms.clear();
217 const QDomElement contextElem = contextNodes.at( 0 ).toElement();
220 const QDomNodeList srcDestNodes = contextElem.elementsByTagName( QStringLiteral(
"srcDest" ) );
221 for (
int i = 0; i < srcDestNodes.size(); ++i )
223 const QDomElement transformElem = srcDestNodes.at( i ).toElement();
225 const QDomElement srcElem = transformElem.firstChildElement( QStringLiteral(
"src" ) );
226 const QDomElement destElem = transformElem.firstChildElement( QStringLiteral(
"dest" ) );
230 if ( !srcElem.isNull() && !destElem.isNull() )
238 const QString key1 = transformElem.attribute( QStringLiteral(
"source" ) );
239 const QString key2 = transformElem.attribute( QStringLiteral(
"dest" ) );
247 const QString coordinateOp = transformElem.attribute( QStringLiteral(
"coordinateOp" ) );
248 const bool allowFallback = transformElem.attribute( QStringLiteral(
"allowFallback" ), QStringLiteral(
"1" ) ).toInt();
258 QgsCoordinateTransformContextPrivate::OperationDetails deets;
259 deets.operation = coordinateOp;
260 deets.allowFallback = allowFallback;
261 d->mSourceDestDatumTransforms.insert( qMakePair( srcCrs, destCrs ), deets );
270 d->mLock.lockForRead();
272 QDomDocument doc = element.ownerDocument();
274 QDomElement contextElem = doc.createElement( QStringLiteral(
"transformContext" ) );
277 for (
auto it = d->mSourceDestDatumTransforms.constBegin(); it != d->mSourceDestDatumTransforms.constEnd(); ++ it )
279 QDomElement transformElem = doc.createElement( QStringLiteral(
"srcDest" ) );
280 QDomElement srcElem = doc.createElement( QStringLiteral(
"src" ) );
281 QDomElement destElem = doc.createElement( QStringLiteral(
"dest" ) );
283 it.key().first.writeXml( srcElem, doc );
284 it.key().second.writeXml( destElem, doc );
286 transformElem.appendChild( srcElem );
287 transformElem.appendChild( destElem );
289 transformElem.setAttribute( QStringLiteral(
"coordinateOp" ), it.value().operation );
290 transformElem.setAttribute( QStringLiteral(
"allowFallback" ), it.value().allowFallback ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
291 contextElem.appendChild( transformElem );
294 element.appendChild( contextElem );
301 d->mLock.lockForWrite();
303 d->mSourceDestDatumTransforms.clear();
306 settings.
beginGroup( QStringLiteral(
"/Projections" ) );
307 const QStringList projectionKeys = settings.
allKeys();
310 QMap< QPair< QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem >, QgsCoordinateTransformContextPrivate::OperationDetails > transforms;
311 QStringList::const_iterator pkeyIt = projectionKeys.constBegin();
312 for ( ; pkeyIt != projectionKeys.constEnd(); ++pkeyIt )
314 if ( pkeyIt->contains( QLatin1String(
"coordinateOp" ) ) )
316 const QStringList split = pkeyIt->split(
'/' );
317 QString srcAuthId, destAuthId;
318 if ( ! split.isEmpty() )
320 srcAuthId = split.at( 0 );
322 if ( split.size() > 1 )
324 destAuthId = split.at( 1 ).split(
'_' ).at( 0 );
327 if ( srcAuthId.isEmpty() || destAuthId.isEmpty() )
330 const QString proj = settings.
value( *pkeyIt ).toString();
331 const bool allowFallback = settings.
value( QStringLiteral(
"%1//%2_allowFallback" ).arg( srcAuthId, destAuthId ) ).toBool();
332 QgsCoordinateTransformContextPrivate::OperationDetails deets;
333 deets.operation = proj;
334 deets.allowFallback = allowFallback;
340 auto transformIt = transforms.constBegin();
341 for ( ; transformIt != transforms.constEnd(); ++transformIt )
343 d->mSourceDestDatumTransforms.insert( transformIt.key(), transformIt.value() );
353 settings.
beginGroup( QStringLiteral(
"/Projections" ) );
354 const QStringList groupKeys = settings.
allKeys();
355 QStringList::const_iterator groupKeyIt = groupKeys.constBegin();
356 for ( ; groupKeyIt != groupKeys.constEnd(); ++groupKeyIt )
358 if ( groupKeyIt->contains( QLatin1String(
"srcTransform" ) ) || groupKeyIt->contains( QLatin1String(
"destTransform" ) ) || groupKeyIt->contains( QLatin1String(
"coordinateOp" ) ) )
360 settings.
remove( *groupKeyIt );
364 for (
auto transformIt = d->mSourceDestDatumTransforms.constBegin(); transformIt != d->mSourceDestDatumTransforms.constEnd(); ++transformIt )
366 const QString srcAuthId = transformIt.key().first.authid();
367 const QString destAuthId = transformIt.key().second.authid();
369 if ( srcAuthId.isEmpty() || destAuthId.isEmpty() )
372 const QString proj = transformIt.value().operation;
373 const bool allowFallback = transformIt.value().allowFallback;
374 settings.
setValue( srcAuthId +
"//" + destAuthId +
"_coordinateOp", proj );
375 settings.
setValue( srcAuthId +
"//" + destAuthId +
"_allowFallback", allowFallback );
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Contains information about the context in which a coordinate transform is executed.
void clear()
Clears all stored transform information from the context.
bool allowFallbackTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if approximate "ballpark" transforms may be used when transforming between a source and ...
QString calculateCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the Proj coordinate operation string to use when transforming from the specified source CRS t...
void readSettings()
Reads the context's state from application settings.
void writeSettings()
Write the context's state to application settings.
Q_DECL_DEPRECATED bool addSourceDestinationDatumTransform(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, int sourceTransformId, int destinationTransformId)
Adds a new sourceTransform and destinationTransform to use when projecting coordinates from the speci...
QMap< QPair< QString, QString >, QString > coordinateOperations() const
Returns the stored mapping for source to destination CRS pairs to associated coordinate operation to ...
void writeXml(QDomElement &element, const QgsReadWriteContext &context) const
Writes the context's state to a DOM element.
Q_DECL_DEPRECATED QgsDatumTransform::TransformPair calculateDatumTransforms(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the pair of source and destination datum transforms to use for a transform from the specified...
Q_DECL_DEPRECATED QMap< QPair< QString, QString >, QgsDatumTransform::TransformPair > sourceDestinationDatumTransforms() const
Returns the stored mapping for source to destination CRS pairs to associated datum transforms to use.
~QgsCoordinateTransformContext()
bool readXml(const QDomElement &element, const QgsReadWriteContext &context, QStringList &missingTransforms)
Reads the context's state from a DOM element.
bool addCoordinateOperation(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &coordinateOperationProjString, bool allowFallback=true)
Adds a new coordinateOperationProjString to use when projecting coordinates from the specified source...
void removeCoordinateOperation(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs)
Removes the coordinate operation for the specified sourceCrs and destinationCrs.
QgsCoordinateTransformContext()
Constructor for QgsCoordinateTransformContext.
bool hasTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if the context has a valid coordinate operation to use when transforming from the specif...
bool mustReverseCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if the coordinate operation returned by calculateCoordinateOperation() for the source to...
bool operator==(const QgsCoordinateTransformContext &rhs) const
Q_DECL_DEPRECATED void removeSourceDestinationDatumTransform(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs)
Removes the source to destination datum transform pair for the specified sourceCrs and destinationCrs...
QgsCoordinateTransformContext & operator=(const QgsCoordinateTransformContext &rhs)
static bool coordinateOperationIsAvailable(const QString &projDef)
Returns true if a coordinate operation (specified via proj string) is available.
The class is used as a container of context for various read/write operations on other objects.
This class is a composition of two QSettings instances:
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
QString crsToKey(const QgsCoordinateReferenceSystem &crs)
const QgsCoordinateReferenceSystem & crs