QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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 
54 {
55  if ( !layer )
56  return 0;
57 
58  QString srcAuthId = layer->crs().authid();
59  QString dstAuthId = mDestCRS.authid();
60 
61  if ( srcAuthId == dstAuthId )
62  {
63  return 0;
64  }
65 
66  QHash< QString, Entry >::const_iterator ctIt = mEntries.find( layer->id() );
67  if ( ctIt != mEntries.constEnd() && ctIt->srcAuthId == srcAuthId && ctIt->destAuthId == dstAuthId )
68  {
69  return QgsCoordinateTransformCache::instance()->transform( ctIt->srcAuthId, ctIt->destAuthId, ctIt->srcDatumTransform, ctIt->destDatumTransform );
70  }
71  else
72  {
73  return QgsCoordinateTransformCache::instance()->transform( srcAuthId, dstAuthId );
74  }
75 }
76 
77 void QgsDatumTransformStore::readXML( const QDomNode& parentNode )
78 {
79  clear();
80 
81  QDomElement layerCoordTransformInfoElem = parentNode.firstChildElement( "layer_coordinate_transform_info" );
82  if ( !layerCoordTransformInfoElem.isNull() )
83  {
84  QDomNodeList layerCoordinateTransformList = layerCoordTransformInfoElem.elementsByTagName( "layer_coordinate_transform" );
85  QDomElement layerCoordTransformElem;
86  for ( int i = 0; i < layerCoordinateTransformList.size(); ++i )
87  {
88  layerCoordTransformElem = layerCoordinateTransformList.at( i ).toElement();
89  QString layerId = layerCoordTransformElem.attribute( "layerid" );
90  if ( layerId.isEmpty() )
91  {
92  continue;
93  }
94 
95  addEntry( layerId,
96  layerCoordTransformElem.attribute( "srcAuthId" ),
97  layerCoordTransformElem.attribute( "destAuthId" ),
98  layerCoordTransformElem.attribute( "srcDatumTransform", "-1" ).toInt(),
99  layerCoordTransformElem.attribute( "destDatumTransform", "-1" ).toInt()
100  );
101  }
102  }
103 }
104 
105 void QgsDatumTransformStore::writeXML( QDomNode& parentNode, QDomDocument& theDoc ) const
106 {
107  // layer coordinate transform infos
108  QDomElement layerCoordTransformInfo = theDoc.createElement( "layer_coordinate_transform_info" );
109 
110  for ( QHash< QString, Entry >::const_iterator coordIt = mEntries.constBegin(); coordIt != mEntries.constEnd(); ++coordIt )
111  {
112  QDomElement layerCoordTransformElem = theDoc.createElement( "layer_coordinate_transform" );
113  layerCoordTransformElem.setAttribute( "layerid", coordIt.key() );
114  layerCoordTransformElem.setAttribute( "srcAuthId", coordIt->srcAuthId );
115  layerCoordTransformElem.setAttribute( "destAuthId", coordIt->destAuthId );
116  layerCoordTransformElem.setAttribute( "srcDatumTransform", QString::number( coordIt->srcDatumTransform ) );
117  layerCoordTransformElem.setAttribute( "destDatumTransform", QString::number( coordIt->destDatumTransform ) );
118  layerCoordTransformInfo.appendChild( layerCoordTransformElem );
119  }
120  parentNode.appendChild( layerCoordTransformInfo );
121 }