QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 && ( *valIt )->sourceDatumTransform() == srcDatumTransform && ( *valIt )->destinationDatumTransform() == destDatumTransform )
41  {
42  return *valIt;
43  }
44  }
45 
46  //not found, insert new value
47  const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
48  const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
49  QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
50  ct->setSourceDatumTransform( srcDatumTransform );
51  ct->setDestinationDatumTransform( destDatumTransform );
52  ct->initialise();
53  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
54  return ct;
55 }
56 
57 void QgsCoordinateTransformCache::invalidateCrs( const QString& crsAuthId )
58 {
59  //get keys to remove first
60  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator it = mTransforms.constBegin();
61  QList< QPair< QString, QString > > updateList;
62 
63  for ( ; it != mTransforms.constEnd(); ++it )
64  {
65  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
66  {
67  updateList.append( it.key() );
68  }
69  }
70 
71  //and remove after
72  QList< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
73  for ( ; updateIt != updateList.constEnd(); ++updateIt )
74  {
75  mTransforms.remove( *updateIt );
76  }
77 }
78 
79 
81 {
82  static QgsCRSCache mInstance;
83  return &mInstance;
84 }
85 
87 {
88 }
89 
91 {
92 }
93 
94 void QgsCRSCache::updateCRSCache( const QString& authid )
95 {
97  if ( s.createFromOgcWmsCrs( authid ) )
98  {
99  mCRS.insert( authid, s );
100  }
101  else
102  {
103  mCRS.remove( authid );
104  }
105 
107 }
108 
110 {
111  QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRS.find( authid );
112  if ( crsIt == mCRS.constEnd() )
113  {
115  if ( ! s.createFromOgcWmsCrs( authid ) )
116  {
117  return mInvalidCRS;
118  }
119  return mCRS.insert( authid, s ).value();
120  }
121  else
122  {
123  return crsIt.value();
124  }
125 }
126 
128 {
129  return crsByAuthId( "EPSG:" + QString::number( epsg ) );
130 }
const QgsCoordinateReferenceSystem & crsByAuthId(const QString &authid)
Returns the CRS for authid, e.g.
static QgsCoordinateTransformCache * instance()
Definition: qgssingleton.h:23
void initialise()
initialise is used to actually create the Transformer instance
bool createFromOgcWmsCrs(QString theCrs)
Set up this CRS from the given OGC CRS.
const QgsCoordinateTransform * transform(const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform=-1, int destDatumTransform=-1)
Returns coordinate transformation.
Definition: qgscrscache.cpp:32
void invalidateCrs(const QString &crsAuthId)
Removes transformations where a changed crs is involved from the cache.
Definition: qgscrscache.cpp:57
const QgsCoordinateReferenceSystem & crsByEpsgId(long epsg)
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
void updateCRSCache(const QString &authid)
Definition: qgscrscache.cpp:94
static QgsCRSCache * instance()
Definition: qgscrscache.cpp:80