QGIS API Documentation  2.0.1-Dufour
 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 
24 {
25  if ( !mInstance )
26  {
28  }
29  return mInstance;
30 }
31 
33 {
34  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator tIt = mTransforms.constBegin();
35  for ( ; tIt != mTransforms.constEnd(); ++tIt )
36  {
37  delete tIt.value();
38  }
39  delete mInstance;
40 }
41 
42 const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId )
43 {
44  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator ctIt =
45  mTransforms.find( qMakePair( srcAuthId, destAuthId ) );
46  if ( ctIt == mTransforms.constEnd() )
47  {
48  const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
49  const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
50  QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
51  mTransforms.insert( qMakePair( srcAuthId, destAuthId ), ct );
52  return ct;
53  }
54  else
55  {
56  return ctIt.value();
57  }
58 }
59 
60 void QgsCoordinateTransformCache::invalidateCrs( const QString& crsAuthId )
61 {
62  //get keys to remove first
63  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator it = mTransforms.constBegin();
64  QList< QPair< QString, QString > > updateList;
65 
66  for ( ; it != mTransforms.constEnd(); ++it )
67  {
68  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
69  {
70  updateList.append( it.key() );
71  }
72  }
73 
74  //and remove after
75  QList< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
76  for ( ; updateIt != updateList.constEnd(); ++updateIt )
77  {
78  mTransforms.remove( *updateIt );
79  }
80 }
81 
83 
85 {
86  if ( !mInstance )
87  {
88  mInstance = new QgsCRSCache();
89  }
90  return mInstance;
91 }
92 
94 {
95 }
96 
98 {
99  delete mInstance;
100 }
101 
102 void QgsCRSCache::updateCRSCache( const QString& authid )
103 {
105  if ( s.createFromOgcWmsCrs( authid ) )
106  {
107  mCRS.insert( authid, s );
108  }
109  else
110  {
111  mCRS.remove( authid );
112  }
113 
114  QgsCoordinateTransformCache::instance()->invalidateCrs( authid );
115 }
116 
118 {
119  QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRS.find( authid );
120  if ( crsIt == mCRS.constEnd() )
121  {
123  if ( ! s.createFromOgcWmsCrs( authid ) )
124  {
125  return mInvalidCRS;
126  }
127  return mCRS.insert( authid, s ).value();
128  }
129  else
130  {
131  return crsIt.value();
132  }
133 }
134 
136 {
137  return crsByAuthId( "EPSG:" + QString::number( epsg ) );
138 }