QGIS API Documentation  2.99.0-Master (7d4f81d)
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 sInstance;
25  return &sInstance;
26 }
27 
29 {
30 }
31 
32 QgsCoordinateTransform QgsCoordinateTransformCache::transform( const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform, int destDatumTransform )
33 {
34  QList< QgsCoordinateTransform > values =
35  mTransforms.values( qMakePair( srcAuthId, destAuthId ) );
36 
37  QList< QgsCoordinateTransform >::const_iterator valIt = values.constBegin();
38  for ( ; valIt != values.constEnd(); ++valIt )
39  {
40  if ( ( *valIt ).isValid() &&
41  ( *valIt ).sourceDatumTransform() == srcDatumTransform &&
42  ( *valIt ).destinationDatumTransform() == destDatumTransform )
43  {
44  return *valIt;
45  }
46  }
47 
48  //not found, insert new value
51  QgsCoordinateTransform ct = QgsCoordinateTransform( srcCrs, destCrs );
52  ct.setSourceDatumTransform( srcDatumTransform );
53  ct.setDestinationDatumTransform( destDatumTransform );
54  ct.initialize();
55  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
56  return ct;
57 }
58 
59 void QgsCoordinateTransformCache::invalidateCrs( const QString &crsAuthId )
60 {
61  //get keys to remove first
62  QHash< QPair< QString, QString >, QgsCoordinateTransform >::const_iterator it = mTransforms.constBegin();
63  QVector< QPair< QString, QString > > updateList;
64 
65  for ( ; it != mTransforms.constEnd(); ++it )
66  {
67  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
68  {
69  updateList.append( it.key() );
70  }
71  }
72 
73  //and remove after
74  QVector< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
75  for ( ; updateIt != updateList.constEnd(); ++updateIt )
76  {
77  mTransforms.remove( *updateIt );
78  }
79 }
QgsCoordinateTransformCache(const QgsCoordinateTransformCache &rh)=delete
QgsCoordinateTransformCache cannot be copied.
static QgsCoordinateTransformCache * instance()
Definition: qgscrscache.cpp:22
void invalidateCrs(const QString &crsAuthId)
Removes transformations where a changed crs is involved from the cache.
Definition: qgscrscache.cpp:59
QgsCoordinateTransform transform(const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform=-1, int destDatumTransform=-1)
Returns coordinate transformation.
Definition: qgscrscache.cpp:32
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
void initialize()
initialize is used to actually create the Transformer instance
Cache coordinate transform by authid of source/dest transformation to avoid the overhead of initializ...
Definition: qgscrscache.h:31