QGIS API Documentation  2.99.0-Master (f867b65)
qgsdatumtransformstore.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatumtransformstore.cpp
3  ---------------------
4  begin : June 2014
5  copyright : (C) 2014 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsdatumtransformstore.h"
17 #include "qgscoordinatetransform.h"
18 #include "qgscrscache.h"
19 #include "qgslogger.h"
20 #include "qgsmaplayer.h"
21 
23  : mDestCRS( destCrs )
24 {
25 }
26 
28 {
29  mEntries.clear();
30 }
31 
33 {
34  mDestCRS = destCrs;
35  clear();
36 }
37 
38 void QgsDatumTransformStore::addEntry( const QString &layerId, const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform, int destDatumTransform )
39 {
40  Entry lt;
41  lt.srcAuthId = srcAuthId;
42  lt.destAuthId = destAuthId;
43  lt.srcDatumTransform = srcDatumTransform;
44  lt.destDatumTransform = destDatumTransform;
45  mEntries.insert( layerId, lt );
46 }
47 
49 {
50  return mEntries.contains( layer->id() );
51 }
52 
53 QgsCoordinateTransform QgsDatumTransformStore::transformation( const QgsMapLayer *layer, QString srcAuthId, QString dstAuthId ) const
54 {
55  if ( !layer )
56  return QgsCoordinateTransform();
57 
58  if ( srcAuthId.isEmpty() )
59  srcAuthId = layer->crs().authid();
60  if ( dstAuthId.isEmpty() )
61  dstAuthId = mDestCRS.authid();
62 
63  if ( srcAuthId == dstAuthId )
64  {
65  return QgsCoordinateTransform();
66  }
67 
68  QHash< QString, Entry >::const_iterator ctIt = mEntries.find( layer->id() );
69  if ( ctIt != mEntries.constEnd() && ctIt->srcAuthId == srcAuthId && ctIt->destAuthId == dstAuthId )
70  {
71  return QgsCoordinateTransformCache::instance()->transform( ctIt->srcAuthId, ctIt->destAuthId, ctIt->srcDatumTransform, ctIt->destDatumTransform );
72  }
73  else
74  {
75  return QgsCoordinateTransformCache::instance()->transform( srcAuthId, dstAuthId );
76  }
77 }
78 
79 void QgsDatumTransformStore::readXml( const QDomNode &parentNode )
80 {
81  clear();
82 
83  QDomElement layerCoordTransformInfoElem = parentNode.firstChildElement( QStringLiteral( "layer_coordinate_transform_info" ) );
84  if ( !layerCoordTransformInfoElem.isNull() )
85  {
86  QDomNodeList layerCoordinateTransformList = layerCoordTransformInfoElem.elementsByTagName( QStringLiteral( "layer_coordinate_transform" ) );
87  QDomElement layerCoordTransformElem;
88  for ( int i = 0; i < layerCoordinateTransformList.size(); ++i )
89  {
90  layerCoordTransformElem = layerCoordinateTransformList.at( i ).toElement();
91  QString layerId = layerCoordTransformElem.attribute( QStringLiteral( "layerid" ) );
92  if ( layerId.isEmpty() )
93  {
94  continue;
95  }
96 
97  addEntry( layerId,
98  layerCoordTransformElem.attribute( QStringLiteral( "srcAuthId" ) ),
99  layerCoordTransformElem.attribute( QStringLiteral( "destAuthId" ) ),
100  layerCoordTransformElem.attribute( QStringLiteral( "srcDatumTransform" ), QStringLiteral( "-1" ) ).toInt(),
101  layerCoordTransformElem.attribute( QStringLiteral( "destDatumTransform" ), QStringLiteral( "-1" ) ).toInt()
102  );
103  }
104  }
105 }
106 
107 void QgsDatumTransformStore::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
108 {
109  // layer coordinate transform infos
110  QDomElement layerCoordTransformInfo = doc.createElement( QStringLiteral( "layer_coordinate_transform_info" ) );
111 
112  for ( QHash< QString, Entry >::const_iterator coordIt = mEntries.constBegin(); coordIt != mEntries.constEnd(); ++coordIt )
113  {
114  QDomElement layerCoordTransformElem = doc.createElement( QStringLiteral( "layer_coordinate_transform" ) );
115  layerCoordTransformElem.setAttribute( QStringLiteral( "layerid" ), coordIt.key() );
116  layerCoordTransformElem.setAttribute( QStringLiteral( "srcAuthId" ), coordIt->srcAuthId );
117  layerCoordTransformElem.setAttribute( QStringLiteral( "destAuthId" ), coordIt->destAuthId );
118  layerCoordTransformElem.setAttribute( QStringLiteral( "srcDatumTransform" ), QString::number( coordIt->srcDatumTransform ) );
119  layerCoordTransformElem.setAttribute( QStringLiteral( "destDatumTransform" ), QString::number( coordIt->destDatumTransform ) );
120  layerCoordTransformInfo.appendChild( layerCoordTransformElem );
121  }
122  parentNode.appendChild( layerCoordTransformInfo );
123 }
Base class for all map layer types.
Definition: qgsmaplayer.h:54
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
void setDestinationCrs(const QgsCoordinateReferenceSystem &destCrs)
static QgsCoordinateTransformCache * instance()
Definition: qgscrscache.cpp:22
QgsDatumTransformStore(const QgsCoordinateReferenceSystem &destCrs)
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
void addEntry(const QString &layerId, const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform, int destDatumTransform)
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
void readXml(const QDomNode &parentNode)
QgsCoordinateReferenceSystem mDestCRS
QgsCoordinateTransform transform(const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform=-1, int destDatumTransform=-1)
Returns coordinate transformation.
Definition: qgscrscache.cpp:32
bool hasEntryForLayer(QgsMapLayer *layer) const
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QgsCoordinateTransform transformation(const QgsMapLayer *layer, QString srcAuthId=QString(), QString dstAuthId=QString()) const
Will return transform from layer&#39;s CRS to current destination CRS.
QHash< QString, Entry > mEntries
key = layer ID
QString authid() const
Returns the authority identifier for the CRS.