QGIS API Documentation  2.99.0-Master (25b0421)
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 
28 QgsCoordinateTransform QgsCoordinateTransformCache::transform( const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform, int destDatumTransform )
29 {
30  QList< QgsCoordinateTransform > values =
31  mTransforms.values( qMakePair( srcAuthId, destAuthId ) );
32 
33  QList< QgsCoordinateTransform >::const_iterator valIt = values.constBegin();
34  for ( ; valIt != values.constEnd(); ++valIt )
35  {
36  if ( ( *valIt ).isValid() &&
37  ( *valIt ).sourceDatumTransform() == srcDatumTransform &&
38  ( *valIt ).destinationDatumTransform() == destDatumTransform )
39  {
40  return *valIt;
41  }
42  }
43 
44  //not found, insert new value
47  QgsCoordinateTransform ct = QgsCoordinateTransform( srcCrs, destCrs );
48  ct.setSourceDatumTransform( srcDatumTransform );
49  ct.setDestinationDatumTransform( destDatumTransform );
50  ct.initialize();
51  mTransforms.insertMulti( qMakePair( srcAuthId, destAuthId ), ct );
52  return ct;
53 }
54 
55 void QgsCoordinateTransformCache::invalidateCrs( const QString &crsAuthId )
56 {
57  //get keys to remove first
58  QHash< QPair< QString, QString >, QgsCoordinateTransform >::const_iterator it = mTransforms.constBegin();
59  QVector< QPair< QString, QString > > updateList;
60 
61  for ( ; it != mTransforms.constEnd(); ++it )
62  {
63  if ( it.key().first == crsAuthId || it.key().second == crsAuthId )
64  {
65  updateList.append( it.key() );
66  }
67  }
68 
69  //and remove after
70  QVector< QPair< QString, QString > >::const_iterator updateIt = updateList.constBegin();
71  for ( ; updateIt != updateList.constEnd(); ++updateIt )
72  {
73  mTransforms.remove( *updateIt );
74  }
75 }
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:55
QgsCoordinateTransform transform(const QString &srcAuthId, const QString &destAuthId, int srcDatumTransform=-1, int destDatumTransform=-1)
Returns coordinate transformation.
Definition: qgscrscache.cpp:28
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:32