QGIS API Documentation  2.5.0-Master
 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 }
27 
29 {
30  mEntries.clear();
31 }
32 
34 {
35  mDestCRS = destCrs;
36  clear();
37 }
38 
39 void QgsDatumTransformStore::addEntry( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform, int destDatumTransform )
40 {
41  Entry lt;
42  lt.srcAuthId = srcAuthId;
43  lt.destAuthId = destAuthId;
44  lt.srcDatumTransform = srcDatumTransform;
45  lt.destDatumTransform = destDatumTransform;
46  mEntries.insert( layerId, lt );
47 }
48 
50 {
51  return mEntries.contains( layer->id() );
52 }
53 
55 {
56  QString srcAuthId = layer->crs().authid();
57  QString dstAuthId = mDestCRS.authid();
58 
59  if ( !layer || srcAuthId == dstAuthId )
60  {
61  return 0;
62  }
63 
64  QHash< QString, Entry >::const_iterator ctIt = mEntries.find( layer->id() );
65  if ( ctIt != mEntries.constEnd() && ctIt->srcAuthId == srcAuthId && ctIt->destAuthId == dstAuthId )
66  {
67  return QgsCoordinateTransformCache::instance()->transform( ctIt->srcAuthId, ctIt->destAuthId, ctIt->srcDatumTransform, ctIt->destDatumTransform );
68  }
69  else
70  {
71  return QgsCoordinateTransformCache::instance()->transform( srcAuthId, dstAuthId, -1, -1 );
72  }
73 }
74 
75 void QgsDatumTransformStore::readXML( const QDomNode& parentNode )
76 {
77  clear();
78 
79  QDomElement layerCoordTransformInfoElem = parentNode.firstChildElement( "layer_coordinate_transform_info" );
80  if ( !layerCoordTransformInfoElem.isNull() )
81  {
82  QDomNodeList layerCoordinateTransformList = layerCoordTransformInfoElem.elementsByTagName( "layer_coordinate_transform" );
83  QDomElement layerCoordTransformElem;
84  for ( int i = 0; i < layerCoordinateTransformList.size(); ++i )
85  {
86  layerCoordTransformElem = layerCoordinateTransformList.at( i ).toElement();
87  QString layerId = layerCoordTransformElem.attribute( "layerid" );
88  if ( layerId.isEmpty() )
89  {
90  continue;
91  }
92 
93  Entry lct;
94  lct.srcAuthId = layerCoordTransformElem.attribute( "srcAuthId" );
95  lct.destAuthId = layerCoordTransformElem.attribute( "destAuthId" );
96  lct.srcDatumTransform = layerCoordTransformElem.attribute( "srcDatumTransform", "-1" ).toInt();
97  lct.destDatumTransform = layerCoordTransformElem.attribute( "destDatumTransform", "-1" ).toInt();
98  mEntries.insert( layerId, lct );
99  }
100  }
101 }
102 
103 void QgsDatumTransformStore::writeXML( QDomNode& parentNode, QDomDocument& theDoc ) const
104 {
105  // layer coordinate transform infos
106  QDomElement layerCoordTransformInfo = theDoc.createElement( "layer_coordinate_transform_info" );
107 
108  for ( QHash< QString, Entry >::const_iterator coordIt = mEntries.constBegin(); coordIt != mEntries.constEnd(); ++coordIt )
109  {
110  QDomElement layerCoordTransformElem = theDoc.createElement( "layer_coordinate_transform" );
111  layerCoordTransformElem.setAttribute( "layerid", coordIt.key() );
112  layerCoordTransformElem.setAttribute( "srcAuthId", coordIt->srcAuthId );
113  layerCoordTransformElem.setAttribute( "destAuthId", coordIt->destAuthId );
114  layerCoordTransformElem.setAttribute( "srcDatumTransform", QString::number( coordIt->srcDatumTransform ) );
115  layerCoordTransformElem.setAttribute( "destDatumTransform", QString::number( coordIt->destDatumTransform ) );
116  layerCoordTransformInfo.appendChild( layerCoordTransformElem );
117  }
118  parentNode.appendChild( layerCoordTransformInfo );
119 }
Base class for all map layer types.
Definition: qgsmaplayer.h:48
bool hasEntryForLayer(QgsMapLayer *layer) const
void setDestinationCrs(const QgsCoordinateReferenceSystem &destCrs)
static QgsCoordinateTransformCache * instance()
Definition: qgscrscache.cpp:22
QgsDatumTransformStore(const QgsCoordinateReferenceSystem &destCrs)
const QgsCoordinateTransform * transform(const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform=-1, int destDatumTransform=-1)
Returns coordinate transformation.
Definition: qgscrscache.cpp:37
void addEntry(const QString &layerId, const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform, int destDatumTransform)
void writeXML(QDomNode &parentNode, QDomDocument &theDoc) const
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
const QgsCoordinateTransform * transformation(QgsMapLayer *layer) const
will return transform from layer's CRS to current destination CRS.
QgsCoordinateReferenceSystem mDestCRS
void readXML(const QDomNode &parentNode)
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
QHash< QString, Entry > mEntries
key = layer ID