Skip to content
Permalink
Browse files

QgsMapToPixel API cleanup

* overload toMapCoordinates to also take double (use different PyName)
* deprecate toMapPoint which is strictly equivalent to toMapCoordinates
  • Loading branch information
3nids committed Sep 29, 2018
1 parent 65bdc83 commit f1daabfafb58161838b898dc019bc6119f12d9a5
@@ -95,8 +95,11 @@ transform.


QgsPointXY toMapCoordinates( int x, int y ) const;
%Docstring
Transform device coordinates to map (world) coordinates
%End

QgsPointXY toMapCoordinatesF( double x, double y ) const;
QgsPointXY toMapCoordinates( double x, double y ) const /PyName=toMapCoordinatesF/;
%Docstring
Transform device coordinates to map (world) coordinates
%End
@@ -110,7 +113,12 @@ Transform device coordinates to map (world) coordinates
:return: QgsPointXY in map coorndiates
%End

QgsPointXY toMapPoint( double x, double y ) const;
QgsPointXY toMapPoint( double x, double y ) const;
%Docstring
Transform device coordinates to map (world) coordinates

.. deprecated:: since QGIS 3.4 use toMapCoordinates instead
%End

void setMapUnitsPerPixel( double mapUnitsPerPixel );
%Docstring
@@ -350,10 +350,10 @@ QPolygonF QgsMapSettings::visiblePolygon() const
const QSize &sz = outputSize();
const QgsMapToPixel &m2p = mapToPixel();

poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();

return poly;
}
@@ -88,32 +88,31 @@ bool QgsMapToPixel::updateMatrix()
return true;
}

QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
QgsPointXY QgsMapToPixel::toMapCoordinates( double x, double y ) const
{
bool invertible;
QTransform matrix = mMatrix.inverted( &invertible );
assert( invertible );
qreal mx, my;
qreal x_qreal = x, y_qreal = y;
matrix.map( x_qreal, y_qreal, &mx, &my );
//QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
return QgsPointXY( mx, my );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( QPoint p ) const
{
QgsPointXY mapPt = toMapPoint( p.x(), p.y() );
QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
return QgsPointXY( mapPt );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( int x, int y ) const
{
return toMapPoint( x, y );
return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
}

QgsPointXY QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
{
return toMapPoint( x, y );
return toMapCoordinates( x, y );
}

void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
@@ -117,10 +117,11 @@ class CORE_EXPORT QgsMapToPixel
}
#endif

//! Transform device coordinates to map (world) coordinates
QgsPointXY toMapCoordinates( int x, int y ) const;

//! Transform device coordinates to map (world) coordinates
QgsPointXY toMapCoordinatesF( double x, double y ) const;
QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF );

/**
* Transform device coordinates to map (world) coordinates
@@ -129,7 +130,11 @@ class CORE_EXPORT QgsMapToPixel
*/
QgsPointXY toMapCoordinates( QPoint p ) const;

QgsPointXY toMapPoint( double x, double y ) const;
/**
* Transform device coordinates to map (world) coordinates
* \deprecated since QGIS 3.4 use toMapCoordinates instead
*/
Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const;

/**
* Set map units per pixel
@@ -188,10 +188,10 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
mRasterViewPort->mBottomRightPoint.setY( std::ceil( mRasterViewPort->mBottomRightPoint.y() ) );
// recalc myRasterExtent to aligned values
myRasterExtent.set(
mapToPixel.toMapCoordinatesF( mRasterViewPort->mTopLeftPoint.x(),
mRasterViewPort->mBottomRightPoint.y() ),
mapToPixel.toMapCoordinatesF( mRasterViewPort->mBottomRightPoint.x(),
mRasterViewPort->mTopLeftPoint.y() )
mapToPixel.toMapCoordinates( mRasterViewPort->mTopLeftPoint.x(),
mRasterViewPort->mBottomRightPoint.y() ),
mapToPixel.toMapCoordinates( mRasterViewPort->mBottomRightPoint.x(),
mRasterViewPort->mTopLeftPoint.y() )
);

//raster viewport top left / bottom right are already rounded to int
@@ -407,7 +407,7 @@ void QgsHighlight::updateRect()
// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
const QgsMapToPixel &m2p = mMapCanvas->mapSettings().mapToPixel();
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
double res = m2p.mapUnitsPerPixel();
QSizeF imageSize = mMapCanvas->mapSettings().outputSize();
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + imageSize.width()*res, topLeft.y() - imageSize.height()*res );
@@ -681,7 +681,7 @@ QgsRectangle QgsMapCanvas::imageRect( const QImage &img, const QgsMapSettings &m
// This is a hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
const QgsMapToPixel &m2p = mapSettings.mapToPixel();
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
double res = m2p.mapUnitsPerPixel();
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + img.width()*res, topLeft.y() - img.height()*res );
return rect;
@@ -1558,7 +1558,7 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )

// zoom map to mouse cursor by scaling
QgsPointXY oldCenter = center();
QgsPointXY mousePos( getCoordinateTransform()->toMapPoint( e->x(), e->y() ) );
QgsPointXY mousePos( getCoordinateTransform()->toMapCoordinates( e->x(), e->y() ) );
QgsPointXY newCenter( mousePos.x() + ( ( oldCenter.x() - mousePos.x() ) * signedWheelFactor ),
mousePos.y() + ( ( oldCenter.y() - mousePos.y() ) * signedWheelFactor ) );

@@ -1599,7 +1599,7 @@ void QgsMapCanvas::zoomWithCenter( int x, int y, bool zoomIn )
else
{
// transform the mouse pos to map coordinates
QgsPointXY center = getCoordinateTransform()->toMapPoint( x, y );
QgsPointXY center = getCoordinateTransform()->toMapCoordinates( x, y );
QgsRectangle r = mapSettings().visibleExtent();
r.scale( scaleFactor, &center );
setExtent( r, true );
@@ -81,7 +81,7 @@ void QgsMapToolPan::canvasReleaseEvent( QgsMapMouseEvent *e )
else // add pan to mouse cursor
{
// transform the mouse pos to map coordinates
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
mCanvas->setCenter( center );
mCanvas->refresh();
}
@@ -127,7 +127,7 @@ void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
QPoint pos = gesture->centerPoint().toPoint();
pos = mCanvas->mapFromGlobal( pos );
// transform the mouse pos to map coordinates
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
QgsRectangle r = mCanvas->extent();
r.scale( 1 / gesture->totalScaleFactor(), &center );
mCanvas->setExtent( r );
@@ -524,7 +524,7 @@ void QgsRubberBand::updateRect()
// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
qreal res = m2p.mapUnitsPerPixel();
QgsPointXY topLeft = m2p.toMapPoint( r.xMinimum(), r.yMinimum() );
QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width()*res, topLeft.y() - r.height()*res );

setRect( rect );
@@ -121,7 +121,7 @@ QgsPoint QgsQuickMapSettings::screenToCoordinate( const QPointF &point ) const
// use floating point precision with mapToCoordinatesF
// this is to avoid rounding errors with an odd screen width or height
// and the point being set to the exact center of it
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinatesF( point.x(), point.y() );
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x(), point.y() );
return QgsPoint( pp );
}

@@ -28,7 +28,7 @@ class TestQgsMapToPixel: public QObject
void rotation();
void getters();
void fromScale();
void toMapPoint();
void toMapCoordinates();
};

void TestQgsMapToPixel::rotation()
@@ -40,7 +40,7 @@ void TestQgsMapToPixel::rotation()
QCOMPARE( d.x(), 5.0 ); // center doesn't move
QCOMPARE( d.y(), 5.0 );

QgsPointXY b = m2p.toMapCoordinatesF( d.x(), d.y() ); // transform back
QgsPointXY b = m2p.toMapCoordinates( d.x(), d.y() ); // transform back
QCOMPARE( p, b );

m2p.transform( &p ); // in place transform
@@ -106,16 +106,16 @@ void TestQgsMapToPixel::fromScale()
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.000265, 0.000001 );
}

void TestQgsMapToPixel::toMapPoint()
void TestQgsMapToPixel::toMapCoordinates()
{
QgsMapToPixel m2p( 1, 5, 5, 10, 10, 90 );
QgsPointXY p = m2p.toMapPoint( 5, 5 );
QCOMPARE( p, QgsPointXY( 5, 5 ) );

p = m2p.toMapPoint( 10, 10 );
p = m2p.toMapCoordinates( 10, 10 );
QCOMPARE( p, QgsPointXY( 10, 10 ) );

p = m2p.toMapPoint( 20, 20 );
p = m2p.toMapCoordinates( 20, 20 );
QCOMPARE( p, QgsPointXY( 20, 20 ) );
}

0 comments on commit f1daabf

Please sign in to comment.
You can’t perform that action at this time.