QGIS API Documentation  2.15.0-Master (972fc9f)
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
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 {
118  return crsByOgcWmsCrs( authid );
119 }
120 
122 {
124  if ( crsIt == mCRS.constEnd() )
125  {
127  if ( ! s.createFromOgcWmsCrs( ogcCrs ) )
128  {
129  return mCRS.insert( ogcCrs, mInvalidCRS ).value();
130  }
131  return mCRS.insert( ogcCrs, s ).value();
132  }
133  else
134  {
135  return crsIt.value();
136  }
137 }
138 
140 {
141  return crsByOgcWmsCrs( "EPSG:" + QString::number( epsg ) );
142 }
143 
145 {
147  if ( crsIt == mCRSProj4.constEnd() )
148  {
150  if ( ! s.createFromProj4( proj4 ) )
151  {
152  return mCRSProj4.insert( proj4, mInvalidCRS ).value();
153  }
154  return mCRSProj4.insert( proj4, s ).value();
155  }
156  else
157  {
158  return crsIt.value();
159  }
160 }
QgsCoordinateReferenceSystem crsByProj4(const QString &proj4) const
Returns the CRS from a proj4 style formatted string.
const Key key(const T &value) const
void append(const T &value)
Q_DECL_DEPRECATED QgsCoordinateReferenceSystem crsByAuthId(const QString &authid)
Returns the CRS for authid, e.g.
const_iterator constEnd() const
bool createFromOgcWmsCrs(const QString &theCrs)
Sets this CRS to the given OGC WMS-format Coordinate Reference Systems.
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
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)
QgsCoordinateReferenceSystem crsByOgcWmsCrs(const QString &ogcCrs) const
Returns the CRS from a given OGC WMS-format Coordinate Reference System string.
const_iterator constEnd() const
int remove(const Key &key)
QgsCoordinateReferenceSystem crsByEpsgId(long epsg) const
Returns the CRS from a given EPSG ID.
void invalidateCrs(const QString &crsAuthId)
Removes transformations where a changed crs is involved from the cache.
Definition: qgscrscache.cpp:68
Caches QgsCoordinateReferenceSystem construction, which may be expensive.
Definition: qgscrscache.h:65
const T value(const Key &key) const
iterator find(const Key &key)
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)
Updates the cached definition of a CRS.
const_iterator constEnd() const
const_iterator constBegin() const
static QgsCRSCache * instance()
Returns a pointer to the QgsCRSCache singleton.
Definition: qgscrscache.cpp:91
bool createFromProj4(const QString &theProjString)
Set up this CRS by passing it a proj4 style formatted string.
Cache coordinate transform by authid of source/dest transformation to avoid the overhead of initializ...
Definition: qgscrscache.h:28