QGIS API Documentation  2.3.0-Master
 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 
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 }
const QgsCoordinateReferenceSystem & crsByAuthId(const QString &authid)
Returns the CRS for authid, e.g.
QMultiHash< QPair< QString, QString >, QgsCoordinateTransform * > mTransforms
Definition: qgscrscache.h:45
void initialise()
initialise is used to actually create the Transformer instance
static QgsCoordinateTransformCache * instance()
Definition: qgscrscache.cpp:22
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:37
static QgsCoordinateTransformCache * mInstance
Definition: qgscrscache.h:44
void invalidateCrs(const QString &crsAuthId)
Removes transformations where a changed crs is involved from the cache.
Definition: qgscrscache.cpp:62
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:99
QHash< QString, QgsCoordinateReferenceSystem > mCRS
Definition: qgscrscache.h:63
QgsCoordinateReferenceSystem mInvalidCRS
CRS that is not initialised (returned in case of error)
Definition: qgscrscache.h:65
static QgsCRSCache * instance()
Definition: qgscrscache.cpp:85
Cache coordinate transform by authid of source/dest transformation to avoid the overhead of initialis...
Definition: qgscrscache.h:28