QGIS API Documentation  2.12.0-Lyon
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 {
24  static QgsCoordinateTransformCache mInstance;
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 {
40  mTransforms.values( qMakePair( srcAuthId, destAuthId ) );
41 
43  for ( ; valIt != values.constEnd(); ++valIt )
44  {
45  if ( *valIt &&
46  ( *valIt )->sourceDatumTransform() == srcDatumTransform &&
47  ( *valIt )->destinationDatumTransform() == destDatumTransform )
48  {
49  return *valIt;
50  }
51  }
52 
53  //not found, insert new value
54  const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
55  const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
56  QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
57  ct->setSourceDatumTransform( srcDatumTransform );
58  ct->setDestinationDatumTransform( destDatumTransform );
59  ct->initialise();
60  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
61  return ct;
62 }
63 
65 {
66  //get keys to remove first
67  QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator it = mTransforms.constBegin();
69 
70  for ( ; it != mTransforms.constEnd(); ++it )
71  {
72  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
73  {
74  updateList.append( it.key() );
75  }
76  }
77 
78  //and remove after
79  QList< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
80  for ( ; updateIt != updateList.constEnd(); ++updateIt )
81  {
82  mTransforms.remove( *updateIt );
83  }
84 }
85 
86 
88 {
89  static QgsCRSCache mInstance;
90  return &mInstance;
91 }
92 
94 {
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.
iterator insert(const Key &key, const T &value)
const Key key(const T &value) const
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
QString number(int n, int base)
void append(const T &value)
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:64
int remove(const Key &key)
const T value(const Key &key) const
iterator find(const Key &key)
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)
const_iterator constEnd() const
const_iterator constBegin() const
static QgsCRSCache * instance()
Definition: qgscrscache.cpp:87
Cache coordinate transform by authid of source/dest transformation to avoid the overhead of initialis...
Definition: qgscrscache.h:28