QGIS API Documentation  2.17.0-Master (973e4b0)
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  : mCRSLock( QReadWriteLock::Recursive )
99  , mCRSProj4Lock( QReadWriteLock::Recursive )
100  , mCRSSrsIdLock( QReadWriteLock::Recursive )
101 {
102 }
103 
105 {
106  QWriteLocker lock( &mCRSLock );
108  if ( s.createFromOgcWmsCrs( authid ) )
109  {
110  mCRS.insert( authid, s );
111  }
112  else
113  {
114  mCRS.remove( authid );
115  }
116 
118 }
119 
121 {
122  return crsByOgcWmsCrs( authid );
123 }
124 
126 {
127  mCRSLock.lockForRead();
129  if ( crsIt == mCRS.constEnd() )
130  {
131  mCRSLock.unlock();
132  mCRSLock.lockForWrite();
134  if ( ! s.createFromOgcWmsCrs( ogcCrs ) )
135  {
136  QgsCoordinateReferenceSystem result = mCRS.insert( ogcCrs, mInvalidCRS ).value();
137  mCRSLock.unlock();
138  return result;
139  }
140  else
141  {
142  QgsCoordinateReferenceSystem result = mCRS.insert( ogcCrs, s ).value();
143  mCRSLock.unlock();
144  return result;
145  }
146  }
147  else
148  {
149  QgsCoordinateReferenceSystem result = crsIt.value();
150  mCRSLock.unlock();
151  return result;
152  }
153 }
154 
156 {
157  return crsByOgcWmsCrs( "EPSG:" + QString::number( epsg ) );
158 }
159 
161 {
162  mCRSProj4Lock.lockForRead();
164  if ( crsIt == mCRSProj4.constEnd() )
165  {
166  mCRSProj4Lock.unlock();
167  mCRSProj4Lock.lockForWrite();
169  if ( ! s.createFromProj4( proj4 ) )
170  {
171  QgsCoordinateReferenceSystem result = mCRSProj4.insert( proj4, mInvalidCRS ).value();
172  mCRSProj4Lock.unlock();
173  return result;
174  }
175  else
176  {
177  QgsCoordinateReferenceSystem result = mCRSProj4.insert( proj4, s ).value();
178  mCRSProj4Lock.unlock();
179  return result;
180  }
181  }
182  else
183  {
184  QgsCoordinateReferenceSystem result = crsIt.value();
185  mCRSProj4Lock.unlock();
186  return result;
187  }
188 }
189 
191 {
192  mCRSWktLock.lockForRead();
194  if ( crsIt == mCRSWkt.constEnd() )
195  {
196  mCRSWktLock.unlock();
197  mCRSWktLock.lockForWrite();
199  if ( ! s.createFromWkt( wkt ) )
200  {
201  QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, mInvalidCRS ).value();
202  mCRSWktLock.unlock();
203  return result;
204  }
205  else
206  {
207  QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, s ).value();
208  mCRSWktLock.unlock();
209  return result;
210  }
211  }
212  else
213  {
214  QgsCoordinateReferenceSystem result = crsIt.value();
215  mCRSWktLock.unlock();
216  return result;
217  }
218 }
219 
221 {
222  mCRSSrsIdLock.lockForRead();
224  if ( crsIt == mCRSSrsId.constEnd() )
225  {
226  mCRSSrsIdLock.unlock();
227  mCRSSrsIdLock.lockForWrite();
229  if ( ! s.createFromSrsId( srsId ) )
230  {
231  QgsCoordinateReferenceSystem result = mCRSSrsId.insert( srsId, mInvalidCRS ).value();
232  mCRSSrsIdLock.unlock();
233  return result;
234  }
235  else
236  {
237  QgsCoordinateReferenceSystem result = mCRSSrsId.insert( srsId, s ).value();
238  mCRSSrsIdLock.unlock();
239  return result;
240  }
241  }
242  else
243  {
244  QgsCoordinateReferenceSystem result = crsIt.value();
245  mCRSSrsIdLock.unlock();
246  return result;
247  }
248 }
void lockForWrite()
QgsCoordinateReferenceSystem crsByProj4(const QString &proj4) const
Returns the CRS from a proj4 style formatted string.
iterator insert(const Key &key, const T &value)
void lockForRead()
const Key key(const T &value) const
bool createFromWkt(const QString &theWkt)
Set up this CRS using a WKT spatial ref sys definition.
QgsCoordinateReferenceSystem crsBySrsId(long srsId) const
Returns the CRS from a specified QGIS SRS ID.
void append(const T &value)
QgsCoordinateReferenceSystem crsByWkt(const QString &wkt) const
Returns the CRS from a WKT spatial ref sys definition string.
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)
bool createFromSrsId(const long theSrsId)
Set up this CRS by fetching the appropriate information from the sqlite backend.
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
int remove(const Key &key)
Caches QgsCoordinateReferenceSystem construction, which may be expensive.
Definition: qgscrscache.h:67
const T value(const Key &key) const
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:30