~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/qgsmaplayerset.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
    qgsmaplayerset.cpp  -  holds a set of layers
3
 
    ----------------------
4
 
    begin                : January 2006
5
 
    copyright            : (C) 2006 by Martin Dobias
6
 
    email                : wonder.sk at gmail dot com
7
 
 ***************************************************************************
8
 
 *                                                                         *
9
 
 *   This program is free software; you can redistribute it and/or modify  *
10
 
 *   it under the terms of the GNU General Public License as published by  *
11
 
 *   the Free Software Foundation; either version 2 of the License, or     *
12
 
 *   (at your option) any later version.                                   *
13
 
 *                                                                         *
14
 
 ***************************************************************************/
15
 
/* $Id: qgsmaplayerset.cpp 6800 2007-03-13 07:42:48Z g_j_m $ */
16
 
 
17
 
#include "qgslogger.h"
18
 
#include "qgsmaplayerset.h"
19
 
#include "qgsmaplayerregistry.h"
20
 
#include "qgsproject.h"
21
 
#include <string>
22
 
 
23
 
void QgsMapLayerSet::setLayerSet(const std::deque<QString>& layers)
24
 
{
25
 
  mLayerSet = layers;
26
 
  updateFullExtent();
27
 
}
28
 
 
29
 
 
30
 
void QgsMapLayerSet::updateFullExtent()
31
 
{
32
 
  QgsDebugMsg("QgsMapLayerSet::updateFullExtent() called !");
33
 
  QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance();
34
 
  bool projectionsEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);
35
 
  
36
 
  // reset the map canvas extent since the extent may now be smaller
37
 
  // We can't use a constructor since QgsRect normalizes the rectangle upon construction
38
 
  mFullExtent.setMinimal();
39
 
  
40
 
  // iterate through the map layers and test each layers extent
41
 
  // against the current min and max values
42
 
  std::deque<QString>::iterator it = mLayerSet.begin();
43
 
  while(it != mLayerSet.end())
44
 
  {
45
 
    QgsMapLayer * lyr = registry->mapLayer(*it);
46
 
    if (lyr == NULL)
47
 
    {
48
 
      QgsLogger::warning("WARNING: layer '" + (*it) + "' not found in map layer registry!");
49
 
    }
50
 
    else
51
 
    {
52
 
      QgsDebugMsg("Updating extent using " + lyr->name());
53
 
      QgsDebugMsg("Input extent: " + lyr->extent().stringRep());
54
 
 
55
 
      // Layer extents are stored in the coordinate system (CS) of the
56
 
      // layer. The extent must be projected to the canvas CS prior to passing
57
 
      // on to the updateFullExtent function
58
 
      if (projectionsEnabled)
59
 
      {
60
 
        try
61
 
        {
62
 
          if ( ! lyr->coordinateTransform() )
63
 
            throw QgsCsException( std::string("NO COORDINATE TRANSFORM FOUND FOR LAYER") );
64
 
              
65
 
          mFullExtent.unionRect(lyr->coordinateTransform()->transformBoundingBox(lyr->extent()));
66
 
        }
67
 
        catch (QgsCsException &cse)
68
 
        {
69
 
          QgsLogger::warning("Transform error caught in " + QString(__FILE__) + " line " +\
70
 
                             QString::number(__LINE__) + QString(cse.what()));
71
 
        }
72
 
      }
73
 
      else
74
 
      {
75
 
        mFullExtent.unionRect(lyr->extent());
76
 
      }
77
 
      
78
 
    }
79
 
    it++;
80
 
  } 
81
 
 
82
 
  if (mFullExtent.width() == 0.0 || mFullExtent.height() == 0.0)
83
 
  {
84
 
    // If all of the features are at the one point, buffer the
85
 
    // rectangle a bit. If they are all at zero, do something a bit
86
 
    // more crude.
87
 
 
88
 
    if (mFullExtent.xMin() == 0.0 && mFullExtent.xMax() == 0.0 &&
89
 
        mFullExtent.yMin() == 0.0 && mFullExtent.yMax() == 0.0)
90
 
    {
91
 
      mFullExtent.set(-1.0, -1.0, 1.0, 1.0);
92
 
    }
93
 
    else
94
 
    {
95
 
      const double padFactor = 1e-8;
96
 
      double widthPad = mFullExtent.xMin() * padFactor;
97
 
      double heightPad = mFullExtent.yMin() * padFactor;
98
 
      double xmin = mFullExtent.xMin() - widthPad;
99
 
      double xmax = mFullExtent.xMax() + widthPad;
100
 
      double ymin = mFullExtent.yMin() - heightPad;
101
 
      double ymax = mFullExtent.yMax() + heightPad;
102
 
      mFullExtent.set(xmin, ymin, xmax, ymax);
103
 
    }
104
 
  }
105
 
 
106
 
}