Skip to content

Commit f200496

Browse files
committedOct 31, 2013
Added utility functions to QgsMapSettings
1 parent 159f135 commit f200496

File tree

8 files changed

+283
-79
lines changed

8 files changed

+283
-79
lines changed
 

‎src/app/maprenderertest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <QTimer>
77
#include <QMouseEvent>
88

9-
#include "qgsmaprendererv2.h"
9+
#include "qgsmapsettings.h"
1010
#include "qgsmaplayer.h"
1111

1212
#include "qgsmaprendererjob.h"

‎src/app/qgsmaptoollabel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ void QgsMapToolLabel::createRubberBands( )
9494
{
9595
if ( mCanvas )
9696
{
97-
QgsMapRenderer* r = mCanvas->mapRenderer();
98-
if ( r && r->hasCrsTransformEnabled() )
97+
const QgsMapSettings& s = mCanvas->mapSettings();
98+
if ( s.hasCrsTransformEnabled() )
9999
{
100-
fixPoint = r->mapToLayerCoordinates( vlayer, fixPoint );
100+
fixPoint = s.mapToLayerCoordinates( vlayer, fixPoint );
101101
}
102102
}
103103

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
SET(QGIS_CORE_SRCS
55

6-
qgsmaprendererv2.cpp
6+
qgsmapsettings.cpp
77
qgsmaprendererjob.cpp
88

99
gps/qgsgpsconnection.cpp
@@ -297,7 +297,7 @@ ADD_BISON_FILES(QGIS_CORE_SRCS qgsexpressionparser.yy)
297297

298298
SET(QGIS_CORE_MOC_HDRS
299299

300-
qgsmaprendererv2.h
300+
qgsmapsettings.h
301301
qgsmaprendererjob.h
302302

303303
qgsapplication.h

‎src/core/qgsmaprendererjob.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "qgsrendercontext.h"
88

9-
#include "qgsmaprendererv2.h"
9+
#include "qgsmapsettings.h"
1010

1111
/** abstract base class renderer jobs that asynchronously start map rendering */
1212
class QgsMapRendererJob : public QObject
@@ -109,7 +109,6 @@ class QgsMapRendererCustomPainterJob : public QgsMapRendererJob
109109

110110
virtual void start();
111111
virtual void cancel();
112-
//virtual QImage renderedImage();
113112

114113
protected slots:
115114
void futureFinished();

‎src/core/qgsmaprendererv2.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

‎src/core/qgsmaprendererv2.cpp renamed to ‎src/core/qgsmapsettings.cpp

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11

2-
#include "qgsmaprendererv2.h"
2+
#include "qgsmapsettings.h"
33

44
#include "qgsscalecalculator.h"
55
#include "qgsmaprendererjob.h"
66
#include "qgsmaptopixel.h"
77
#include "qgslogger.h"
88

9+
#include "qgscrscache.h"
10+
#include "qgsmessagelog.h"
11+
#include "qgsmaplayer.h"
12+
913
/*
1014
1115
usage in QgsMapCanvas - upon pan / zoom - in QgsMapCanvasMap:
@@ -155,6 +159,16 @@ void QgsMapSettings::setLayers(const QStringList& layers)
155159
mLayers = layers;
156160
}
157161

162+
void QgsMapSettings::setProjectionsEnabled( bool enabled )
163+
{
164+
mProjectionsEnabled = enabled;
165+
}
166+
167+
bool QgsMapSettings::hasCrsTransformEnabled() const
168+
{
169+
return mProjectionsEnabled;
170+
}
171+
158172

159173
bool QgsMapSettings::hasValidSettings() const
160174
{
@@ -175,3 +189,143 @@ double QgsMapSettings::scale() const
175189
{
176190
return mScale;
177191
}
192+
193+
194+
195+
196+
197+
const QgsCoordinateTransform* QgsMapSettings::coordTransform( QgsMapLayer *layer )
198+
{
199+
if ( !layer )
200+
{
201+
return 0;
202+
}
203+
return QgsCoordinateTransformCache::instance()->transform( layer->crs().authid(), mDestCRS.authid() );
204+
}
205+
206+
207+
208+
QgsRectangle QgsMapSettings::layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent )
209+
{
210+
QgsDebugMsg( QString( "sourceCrs = " + coordTransform( theLayer )->sourceCrs().authid() ) );
211+
QgsDebugMsg( QString( "destCRS = " + coordTransform( theLayer )->destCRS().authid() ) );
212+
QgsDebugMsg( QString( "extent = " + extent.toString() ) );
213+
if ( hasCrsTransformEnabled() )
214+
{
215+
try
216+
{
217+
extent = coordTransform( theLayer )->transformBoundingBox( extent );
218+
}
219+
catch ( QgsCsException &cse )
220+
{
221+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
222+
}
223+
}
224+
225+
QgsDebugMsg( QString( "proj extent = " + extent.toString() ) );
226+
227+
return extent;
228+
}
229+
230+
231+
QgsRectangle QgsMapSettings::outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent )
232+
{
233+
QgsDebugMsg( QString( "layer sourceCrs = " + coordTransform( theLayer )->sourceCrs().authid() ) );
234+
QgsDebugMsg( QString( "layer destCRS = " + coordTransform( theLayer )->destCRS().authid() ) );
235+
QgsDebugMsg( QString( "extent = " + extent.toString() ) );
236+
if ( hasCrsTransformEnabled() )
237+
{
238+
try
239+
{
240+
extent = coordTransform( theLayer )->transformBoundingBox( extent, QgsCoordinateTransform::ReverseTransform );
241+
}
242+
catch ( QgsCsException &cse )
243+
{
244+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
245+
}
246+
}
247+
248+
QgsDebugMsg( QString( "proj extent = " + extent.toString() ) );
249+
250+
return extent;
251+
}
252+
253+
254+
QgsPoint QgsMapSettings::layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point )
255+
{
256+
if ( hasCrsTransformEnabled() )
257+
{
258+
try
259+
{
260+
point = coordTransform( theLayer )->transform( point, QgsCoordinateTransform::ForwardTransform );
261+
}
262+
catch ( QgsCsException &cse )
263+
{
264+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
265+
}
266+
}
267+
else
268+
{
269+
// leave point without transformation
270+
}
271+
return point;
272+
}
273+
274+
275+
QgsRectangle QgsMapSettings::layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect )
276+
{
277+
if ( hasCrsTransformEnabled() )
278+
{
279+
try
280+
{
281+
rect = coordTransform( theLayer )->transform( rect, QgsCoordinateTransform::ForwardTransform );
282+
}
283+
catch ( QgsCsException &cse )
284+
{
285+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
286+
}
287+
}
288+
else
289+
{
290+
// leave point without transformation
291+
}
292+
return rect;
293+
}
294+
295+
296+
QgsPoint QgsMapSettings::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point )
297+
{
298+
if ( hasCrsTransformEnabled() )
299+
{
300+
try
301+
{
302+
point = coordTransform( theLayer )->transform( point, QgsCoordinateTransform::ReverseTransform );
303+
}
304+
catch ( QgsCsException &cse )
305+
{
306+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
307+
}
308+
}
309+
else
310+
{
311+
// leave point without transformation
312+
}
313+
return point;
314+
}
315+
316+
317+
QgsRectangle QgsMapSettings::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect )
318+
{
319+
if ( hasCrsTransformEnabled() )
320+
{
321+
try
322+
{
323+
rect = coordTransform( theLayer )->transform( rect, QgsCoordinateTransform::ReverseTransform );
324+
}
325+
catch ( QgsCsException &cse )
326+
{
327+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ), "CRS" );
328+
}
329+
}
330+
return rect;
331+
}

‎src/core/qgsmapsettings.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#ifndef QGSMAPSETTINGS_H
2+
#define QGSMAPSETTINGS_H
3+
4+
#include <QSize>
5+
#include <QStringList>
6+
7+
#include "qgscoordinatereferencesystem.h"
8+
#include "qgsrectangle.h"
9+
#include "qgsscalecalculator.h"
10+
11+
class QPainter;
12+
13+
class QgsCoordinateTransform;
14+
class QgsScaleCalculator;
15+
class QgsMapRendererJob;
16+
class QgsMapLayer;
17+
18+
19+
struct QgsMapSettings
20+
{
21+
// TODO
22+
23+
QgsRectangle extent() const;
24+
void setExtent(const QgsRectangle& rect);
25+
26+
QSize outputSize() const;
27+
void setOutputSize(const QSize& size);
28+
29+
double outputDpi() const;
30+
void setOutputDpi(double dpi);
31+
32+
QStringList layers() const;
33+
void setLayers(const QStringList& layers);
34+
35+
//! sets whether to use projections for this layer set
36+
void setProjectionsEnabled( bool enabled );
37+
//! returns true if projections are enabled for this layer set
38+
bool hasCrsTransformEnabled() const;
39+
40+
void updateDerived(); // TODO: should be protected, called automatically
41+
42+
bool hasValidSettings() const;
43+
QgsRectangle visibleExtent() const;
44+
double mapUnitsPerPixel() const;
45+
double scale() const;
46+
47+
48+
49+
// TODO: utility functions
50+
51+
52+
/**
53+
* @brief transform bounding box from layer's CRS to output CRS
54+
* @see layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect ) if you want to transform a rectangle
55+
* @return a bounding box (aligned rectangle) containing the transformed extent
56+
*/
57+
QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
58+
59+
/**
60+
* @brief transform bounding box from output CRS to layer's CRS
61+
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
62+
* @return a bounding box (aligned rectangle) containing the transformed extent
63+
*/
64+
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
65+
66+
/**
67+
* @brief transform point coordinates from layer's CRS to output CRS
68+
* @return the transformed point
69+
*/
70+
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
71+
72+
/**
73+
* @brief transform rectangle from layer's CRS to output CRS
74+
* @see layerExtentToOutputExtent() if you want to transform a bounding box
75+
* @return the transformed rectangle
76+
*/
77+
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
78+
79+
/**
80+
* @brief transform point coordinates from output CRS to layer's CRS
81+
* @return the transformed point
82+
*/
83+
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
84+
85+
/**
86+
* @brief transform rectangle from output CRS to layer's CRS
87+
* @see outputExtentToLayerExtent() if you want to transform a bounding box
88+
* @return the transformed rectangle
89+
*/
90+
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
91+
92+
93+
protected:
94+
95+
double mDpi;
96+
97+
QSize mSize;
98+
99+
QgsRectangle mExtent;
100+
101+
QStringList mLayers;
102+
103+
bool mProjectionsEnabled;
104+
QgsCoordinateReferenceSystem mDestCRS;
105+
106+
// derived properties
107+
bool mValid; //!< whether the actual settings are valid (set in updateDerived())
108+
QgsRectangle mVisibleExtent; //!< extent with some additional white space that matches the output aspect ratio
109+
double mMapUnitsPerPixel;
110+
double mScale;
111+
112+
113+
// utiity stuff
114+
QgsScaleCalculator mScaleCalculator;
115+
116+
const QgsCoordinateTransform* coordTransform( QgsMapLayer *layer );
117+
};
118+
119+
120+
#endif // QGSMAPSETTINGS_H

‎src/gui/qgsmapcanvasmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "qgsmapcanvas.h"
1818
#include "qgsmapcanvasmap.h"
1919
#include "qgsmaprenderer.h"
20-
#include "qgsmaprendererv2.h"
20+
#include "qgsmapsettings.h"
2121
#include "qgsmaplayer.h"
2222

2323
#include <QPainter>

0 commit comments

Comments
 (0)
Please sign in to comment.