Skip to content

Commit

Permalink
Merge pull request #568 from 3nids/layer2map
Browse files Browse the repository at this point in the history
added layerToMapCoordinates for rectangles, and more detailed comments
  • Loading branch information
m-kuhn committed Apr 29, 2013
2 parents 33d13cc + 42fc689 commit c5177fc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
35 changes: 30 additions & 5 deletions python/core/qgsmaprenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,44 @@ class QgsMapRenderer : QObject
QSize outputSize();
QSizeF outputSizeF();

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

//! transform extent in output CRS to extent in layer's CRS
/**
* @brief transform bounding box from output CRS to layer's CRS
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
* @return a bounding box (aligned rectangle) containing the transformed extent
*/
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );

//! transform coordinates from layer's CRS to output CRS
/**
* @brief transform point coordinates from layer's CRS to output CRS
* @return the transformed point
*/
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );

//! transform coordinates from output CRS to layer's CRS
/**
* @brief transform rectangle from layer's CRS to output CRS
* @see layerExtentToOutputExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

/**
* @brief transform point coordinates from output CRS to layer's CRS
* @return the transformed point
*/
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );

//! transform rect's coordinates from output CRS to layer's CRS
/**
* @brief transform rectangle from output CRS to layer's CRS
* @see outputExtentToLayerExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

//! sets whether to use projections for this layer set
Expand Down
29 changes: 23 additions & 6 deletions src/core/qgsmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,7 @@ QgsPoint QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
}
}
else
Expand All @@ -888,6 +887,26 @@ QgsPoint QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint
return point;
}

QgsRectangle QgsMapRenderer::layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect )
{
if ( hasCrsTransformEnabled() )
{
try
{
rect = tr( theLayer )->transform( rect, QgsCoordinateTransform::ForwardTransform );
}
catch ( QgsCsException &cse )
{
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
}
}
else
{
// leave point without transformation
}
return rect;
}

QgsPoint QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point )
{
if ( hasCrsTransformEnabled() )
Expand All @@ -898,8 +917,7 @@ QgsPoint QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint
}
catch ( QgsCsException &cse )
{
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
throw cse; //let client classes know there was a transformation error
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
}
}
else
Expand All @@ -919,8 +937,7 @@ QgsRectangle QgsMapRenderer::mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRe
}
catch ( QgsCsException &cse )
{
QgsDebugMsg( QString( "Transform error caught: %1" ).arg( cse.what() ) );
throw cse; //let client classes know there was a transformation error
QgsMessageLog::logMessage( QString( "Transform error caught: %1" ).arg( cse.what() ) );
}
}
return rect;
Expand Down
35 changes: 30 additions & 5 deletions src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,44 @@ class CORE_EXPORT QgsMapRenderer : public QObject
QSize outputSize();
QSizeF outputSizeF();

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

//! transform extent in output CRS to extent in layer's CRS
/**
* @brief transform bounding box from output CRS to layer's CRS
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
* @return a bounding box (aligned rectangle) containing the transformed extent
*/
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );

//! transform coordinates from layer's CRS to output CRS
/**
* @brief transform point coordinates from layer's CRS to output CRS
* @return the transformed point
*/
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );

//! transform coordinates from output CRS to layer's CRS
/**
* @brief transform rectangle from layer's CRS to output CRS
* @see layerExtentToOutputExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

/**
* @brief transform point coordinates from output CRS to layer's CRS
* @return the transformed point
*/
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );

//! transform rect's coordinates from output CRS to layer's CRS
/**
* @brief transform rectangle from output CRS to layer's CRS
* @see outputExtentToLayerExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

//! sets whether to use projections for this layer set
Expand Down

0 comments on commit c5177fc

Please sign in to comment.