QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscrscache.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscrscache.cpp
3  ---------------
4  begin : September 6th, 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscrscache.h"
19 #include "qgscoordinatetransform.h"
20 
22 {
23  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator tIt = mTransforms.constBegin();
24  for ( ; tIt != mTransforms.constEnd(); ++tIt )
25  {
26  delete tIt.value();
27  }
28 
29  mTransforms.clear();
30 }
31 
32 const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform, int destDatumTransform )
33 {
34  QList< QgsCoordinateTransform* > values =
35  mTransforms.values( qMakePair( srcAuthId, destAuthId ) );
36 
37  QList< QgsCoordinateTransform* >::const_iterator valIt = values.constBegin();
38  for ( ; valIt != values.constEnd(); ++valIt )
39  {
40  if ( *valIt &&
41  ( *valIt )->sourceDatumTransform() == srcDatumTransform &&
42  ( *valIt )->destinationDatumTransform() == destDatumTransform )
43  {
44  return *valIt;
45  }
46  }
47 
48  //not found, insert new value
49  const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
50  const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
51  QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
52  ct->setSourceDatumTransform( srcDatumTransform );
53  ct->setDestinationDatumTransform( destDatumTransform );
54  ct->initialise();
55  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
56  return ct;
57 }
58 
59 void QgsCoordinateTransformCache::invalidateCrs( const QString& crsAuthId )
60 {
61  //get keys to remove first
62  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator it = mTransforms.constBegin();
63  QList< QPair< QString, QString > > updateList;
64 
65  for ( ; it != mTransforms.constEnd(); ++it )
66  {
67  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
68  {
69  updateList.append( it.key() );
70  }
71  }
72 
73  //and remove after
74  QList< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
75  for ( ; updateIt != updateList.constEnd(); ++updateIt )
76  {
77  mTransforms.remove( *updateIt );
78  }
79 }
80 
81 
83 {
84  static QgsCRSCache mInstance;
85  return &mInstance;
86 }
87 
89 {
90 }
91 
93 {
94 }
95 
96 void QgsCRSCache::updateCRSCache( const QString& authid )
97 {
99  if ( s.createFromOgcWmsCrs( authid ) )
100  {
101  mCRS.insert( authid, s );
102  }
103  else
104  {
105  mCRS.remove( authid );
106  }
107 
108  QgsCoordinateTransformCache::instance()->invalidateCrs( authid );
109 }
110 
112 {
113  QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRS.find( authid );
114  if ( crsIt == mCRS.constEnd() )
115  {
117  if ( ! s.createFromOgcWmsCrs( authid ) )
118  {
119  return mInvalidCRS;
120  }
121  return mCRS.insert( authid, s ).value();
122  }
123  else
124  {
125  return crsIt.value();
126  }
127 }
128 
130 {
131  return crsByAuthId( "EPSG:" + QString::number( epsg ) );
132 }