Skip to content

Commit 42fc689

Browse files
committedApr 29, 2013
added layerToMapCoordinates for rectangles, and more detailed comments
1 parent 33d13cc commit 42fc689

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed
 

‎python/core/qgsmaprenderer.sip

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,44 @@ class QgsMapRenderer : QObject
152152
QSize outputSize();
153153
QSizeF outputSizeF();
154154

155-
//! transform extent in layer's CRS to extent in output CRS
155+
/**
156+
* @brief transform bounding box from layer's CRS to output CRS
157+
* @see layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect ) if you want to transform a rectangle
158+
* @return a bounding box (aligned rectangle) containing the transformed extent
159+
*/
156160
QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
157161

158-
//! transform extent in output CRS to extent in layer's CRS
162+
/**
163+
* @brief transform bounding box from output CRS to layer's CRS
164+
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
165+
* @return a bounding box (aligned rectangle) containing the transformed extent
166+
*/
159167
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
160168

161-
//! transform coordinates from layer's CRS to output CRS
169+
/**
170+
* @brief transform point coordinates from layer's CRS to output CRS
171+
* @return the transformed point
172+
*/
162173
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
163174

164-
//! transform coordinates from output CRS to layer's CRS
175+
/**
176+
* @brief transform rectangle from layer's CRS to output CRS
177+
* @see layerExtentToOutputExtent() if you want to transform a bounding box
178+
* @return the transformed rectangle
179+
*/
180+
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
181+
182+
/**
183+
* @brief transform point coordinates from output CRS to layer's CRS
184+
* @return the transformed point
185+
*/
165186
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
166187

167-
//! transform rect's coordinates from output CRS to layer's CRS
188+
/**
189+
* @brief transform rectangle from output CRS to layer's CRS
190+
* @see outputExtentToLayerExtent() if you want to transform a bounding box
191+
* @return the transformed rectangle
192+
*/
168193
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
169194

170195
//! sets whether to use projections for this layer set

‎src/core/qgsmaprenderer.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,7 @@ QgsPoint QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint
877877
}
878878
catch ( QgsCsException &cse )
879879
{
880-
Q_UNUSED( cse );
881-
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
880+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
882881
}
883882
}
884883
else
@@ -888,6 +887,26 @@ QgsPoint QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint
888887
return point;
889888
}
890889

890+
QgsRectangle QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect )
891+
{
892+
if ( hasCrsTransformEnabled() )
893+
{
894+
try
895+
{
896+
rect = tr( theLayer )->transform( rect, QgsCoordinateTransform::ForwardTransform );
897+
}
898+
catch ( QgsCsException &cse )
899+
{
900+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
901+
}
902+
}
903+
else
904+
{
905+
// leave point without transformation
906+
}
907+
return rect;
908+
}
909+
891910
QgsPoint QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point )
892911
{
893912
if ( hasCrsTransformEnabled() )
@@ -898,8 +917,7 @@ QgsPoint QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint
898917
}
899918
catch ( QgsCsException &cse )
900919
{
901-
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
902-
throw cse; //let client classes know there was a transformation error
920+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
903921
}
904922
}
905923
else
@@ -919,8 +937,7 @@ QgsRectangle QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRe
919937
}
920938
catch ( QgsCsException &cse )
921939
{
922-
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
923-
throw cse; //let client classes know there was a transformation error
940+
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
924941
}
925942
}
926943
return rect;

‎src/core/qgsmaprenderer.h

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,44 @@ class CORE_EXPORT QgsMapRenderer : public QObject
190190
QSize outputSize();
191191
QSizeF outputSizeF();
192192

193-
//! transform extent in layer's CRS to extent in output CRS
193+
/**
194+
* @brief transform bounding box from layer's CRS to output CRS
195+
* @see layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect ) if you want to transform a rectangle
196+
* @return a bounding box (aligned rectangle) containing the transformed extent
197+
*/
194198
QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
195199

196-
//! transform extent in output CRS to extent in layer's CRS
200+
/**
201+
* @brief transform bounding box from output CRS to layer's CRS
202+
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
203+
* @return a bounding box (aligned rectangle) containing the transformed extent
204+
*/
197205
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
198206

199-
//! transform coordinates from layer's CRS to output CRS
207+
/**
208+
* @brief transform point coordinates from layer's CRS to output CRS
209+
* @return the transformed point
210+
*/
200211
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
201212

202-
//! transform coordinates from output CRS to layer's CRS
213+
/**
214+
* @brief transform rectangle from layer's CRS to output CRS
215+
* @see layerExtentToOutputExtent() if you want to transform a bounding box
216+
* @return the transformed rectangle
217+
*/
218+
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
219+
220+
/**
221+
* @brief transform point coordinates from output CRS to layer's CRS
222+
* @return the transformed point
223+
*/
203224
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
204225

205-
//! transform rect's coordinates from output CRS to layer's CRS
226+
/**
227+
* @brief transform rectangle from output CRS to layer's CRS
228+
* @see outputExtentToLayerExtent() if you want to transform a bounding box
229+
* @return the transformed rectangle
230+
*/
206231
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
207232

208233
//! sets whether to use projections for this layer set

0 commit comments

Comments
 (0)
Please sign in to comment.