QGIS API Documentation
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 #include <QVector>
21 
23 {
24  static QgsCoordinateTransformCache mInstance;
25  return &mInstance;
26 }
27 
28 QgsCoordinateTransformCache::QgsCoordinateTransformCache()
29 {
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 }
40 
41 const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform, int destDatumTransform )
42 {
44  mTransforms.values( qMakePair( srcAuthId, destAuthId ) );
45 
47  for ( ; valIt != values.constEnd(); ++valIt )
48  {
49  if ( *valIt &&
50  ( *valIt )->sourceDatumTransform() == srcDatumTransform &&
51  ( *valIt )->destinationDatumTransform() == destDatumTransform )
52  {
53  return *valIt;
54  }
55  }
56 
57  //not found, insert new value
58  const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
59  const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
60  QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
61  ct->setSourceDatumTransform( srcDatumTransform );
62  ct->setDestinationDatumTransform( destDatumTransform );
63  ct->initialise();
64  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
65  return ct;
66 }
67 
69 {
70  //get keys to remove first
71  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator it = mTransforms.constBegin();
73 
74  for ( ; it != mTransforms.constEnd(); ++it )
75  {
76  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
77  {
78  updateList.append( it.key() );
79  }
80  }
81 
82  //and remove after
83  QVector< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
84  for ( ; updateIt != updateList.constEnd(); ++updateIt )
85  {
86  mTransforms.remove( *updateIt );
87  }
88 }
89 
90 
92 {
93  static QgsCRSCache mInstance;
94  return &mInstance;
95 }
96 
98 {
99 }
100 
102 {
104  if ( s.createFromOgcWmsCrs( authid ) )
105  {
106  mCRS.insert( authid, s );
107  }
108  else
109  {
110  mCRS.remove( authid );
111  }
112 
114 }
115 
117 {
119  if ( crsIt == mCRS.constEnd() )
120  {
122  if ( ! s.createFromOgcWmsCrs( authid ) )
123  {
124  return mInvalidCRS;
125  }
126  return mCRS.insert( authid, s ).value();
127  }
128  else
129  {
130  return crsIt.value();
131  }
132 }
133 
135 {
136  return crsByAuthId( "EPSG:" + QString::number( epsg ) );
137 }
const QgsCoordinateReferenceSystem & crsByAuthId(const QString &authid)
Returns the CRS for authid, e.g.
const Key key(const T &value) const
void append(const T &value)
const_iterator constEnd() const
void initialise()
initialize is used to actually create the Transformer instance
const_iterator constFind(const Key &key) const
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:41
QString number(int n, int base)
const_iterator constEnd() const
int remove(const Key &key)
void invalidateCrs(const QString &crsAuthId)
Removes transformations where a changed crs is involved from the cache.
Definition: qgscrscache.cpp:68
const T value(const Key &key) const
const QgsCoordinateReferenceSystem & crsByEpsgId(long epsg)
const_iterator constBegin() const
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
void updateCRSCache(const QString &authid)
const_iterator constEnd() const
const_iterator constBegin() const
static QgsCRSCache * instance()
Definition: qgscrscache.cpp:91
Cache coordinate transform by authid of source/dest transformation to avoid the overhead of initializ...
Definition: qgscrscache.h:28