1
/***************************************************************************
2
qgsmaplayerset.cpp - holds a set of layers
5
copyright : (C) 2006 by Martin Dobias
6
email : wonder.sk at gmail dot com
7
***************************************************************************
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. *
14
***************************************************************************/
15
/* $Id: qgsmaplayerset.cpp 6800 2007-03-13 07:42:48Z g_j_m $ */
17
#include "qgslogger.h"
18
#include "qgsmaplayerset.h"
19
#include "qgsmaplayerregistry.h"
20
#include "qgsproject.h"
23
void QgsMapLayerSet::setLayerSet(const std::deque<QString>& layers)
30
void QgsMapLayerSet::updateFullExtent()
32
QgsDebugMsg("QgsMapLayerSet::updateFullExtent() called !");
33
QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance();
34
bool projectionsEnabled = (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0);
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();
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())
45
QgsMapLayer * lyr = registry->mapLayer(*it);
48
QgsLogger::warning("WARNING: layer '" + (*it) + "' not found in map layer registry!");
52
QgsDebugMsg("Updating extent using " + lyr->name());
53
QgsDebugMsg("Input extent: " + lyr->extent().stringRep());
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)
62
if ( ! lyr->coordinateTransform() )
63
throw QgsCsException( std::string("NO COORDINATE TRANSFORM FOUND FOR LAYER") );
65
mFullExtent.unionRect(lyr->coordinateTransform()->transformBoundingBox(lyr->extent()));
67
catch (QgsCsException &cse)
69
QgsLogger::warning("Transform error caught in " + QString(__FILE__) + " line " +\
70
QString::number(__LINE__) + QString(cse.what()));
75
mFullExtent.unionRect(lyr->extent());
82
if (mFullExtent.width() == 0.0 || mFullExtent.height() == 0.0)
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
88
if (mFullExtent.xMin() == 0.0 && mFullExtent.xMax() == 0.0 &&
89
mFullExtent.yMin() == 0.0 && mFullExtent.yMax() == 0.0)
91
mFullExtent.set(-1.0, -1.0, 1.0, 1.0);
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);