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