Skip to content

Commit b3f36dc

Browse files
authoredOct 2, 2018
Merge pull request #8056 from qgis/3nids-patch-3
use float precision for screen coordinates
2 parents ad95820 + d9c57cd commit b3f36dc

16 files changed

+72
-41
lines changed
 

‎doc/api_break.dox

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ This page tries to maintain a list with incompatible changes that happened in pr
1818

1919

2020

21+
QGIS 3.4 {#qgis_api_break_3_4}
22+
========
23+
24+
QgsMapToPixel {#qgis_api_break_3_4_qgsmaptopixel}
25+
-------------
26+
27+
- toMapCoordinatesF have been renamed to toMapCoordinates (as a double overload) for C++ only (Python keeps toMapCoordinatesF).
28+
- toMapPoint has been deprecated in favor of toMapCoordinates.
29+
30+
31+
2132
QGIS 3.0 {#qgis_api_break_3_0}
2233
========
2334

‎python/core/auto_generated/qgsmaptopixel.sip.in

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ transform.
9595

9696

9797
QgsPointXY toMapCoordinates( int x, int y ) const;
98+
%Docstring
99+
Transform device coordinates to map (world) coordinates
100+
%End
98101

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

113-
QgsPointXY toMapPoint( double x, double y ) const;
116+
QgsPointXY toMapPoint( double x, double y ) const;
117+
%Docstring
118+
Transform device coordinates to map (world) coordinates
119+
120+
.. deprecated:: since QGIS 3.4 use toMapCoordinates instead
121+
%End
114122

115123
void setMapUnitsPerPixel( double mapUnitsPerPixel );
116124
%Docstring

‎src/app/qgsmaptoolselectutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ QgsRectangle QgsMapToolSelectUtils::expandSelectRectangle( QgsPointXY mapPoint,
8282

8383
const QgsMapToPixel *transform = canvas->getCoordinateTransform();
8484
QgsPointXY point = transform->transform( mapPoint );
85-
QgsPointXY ll = transform->toMapCoordinates( point.x() - boxSize, point.y() + boxSize );
86-
QgsPointXY ur = transform->toMapCoordinates( point.x() + boxSize, point.y() - boxSize );
85+
QgsPointXY ll = transform->toMapCoordinates( static_cast<int>( point.x() - boxSize ), static_cast<int>( point.y() + boxSize ) );
86+
QgsPointXY ur = transform->toMapCoordinates( static_cast<int>( point.x() + boxSize ), static_cast<int>( point.y() - boxSize ) );
8787
return QgsRectangle( ll, ur );
8888
}
8989

‎src/app/qgspointmarkeritem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ void QgsPointMarkerItem::updateSize()
108108
mMarkerSymbol->startRender( rc, mFeature.fields() );
109109
QRectF bounds = mMarkerSymbol->bounds( mLocation, rc, mFeature );
110110
mMarkerSymbol->stopRender( rc );
111-
QgsRectangle r( mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( bounds.x(), bounds.y() ),
112-
mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( bounds.x() + bounds.width() * 2, bounds.y() + bounds.height() * 2 ) );
111+
QgsRectangle r( mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( bounds.x() ),
112+
static_cast<int>( bounds.y() ) ),
113+
mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( bounds.x() + bounds.width() * 2 ),
114+
static_cast<int>( bounds.y() + bounds.height() * 2 ) ) );
113115
setRect( r );
114116
}
115117

‎src/core/qgsmapsettings.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ QPolygonF QgsMapSettings::visiblePolygon() const
350350
const QSize &sz = outputSize();
351351
const QgsMapToPixel &m2p = mapToPixel();
352352

353-
poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
354-
poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
355-
poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
356-
poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
353+
poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
354+
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
355+
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
356+
poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();
357357

358358
return poly;
359359
}

‎src/core/qgsmaptopixel.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,31 @@ bool QgsMapToPixel::updateMatrix()
8888
return true;
8989
}
9090

91-
QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
91+
QgsPointXY QgsMapToPixel::toMapCoordinates( double x, double y ) const
9292
{
9393
bool invertible;
9494
QTransform matrix = mMatrix.inverted( &invertible );
9595
assert( invertible );
9696
qreal mx, my;
9797
qreal x_qreal = x, y_qreal = y;
9898
matrix.map( x_qreal, y_qreal, &mx, &my );
99-
//QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
10099
return QgsPointXY( mx, my );
101100
}
102101

103102
QgsPointXY QgsMapToPixel::toMapCoordinates( QPoint p ) const
104103
{
105-
QgsPointXY mapPt = toMapPoint( p.x(), p.y() );
104+
QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
106105
return QgsPointXY( mapPt );
107106
}
108107

109108
QgsPointXY QgsMapToPixel::toMapCoordinates( int x, int y ) const
110109
{
111-
return toMapPoint( x, y );
110+
return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
112111
}
113112

114-
QgsPointXY QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
113+
QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
115114
{
116-
return toMapPoint( x, y );
115+
return toMapCoordinates( x, y );
117116
}
118117

119118
void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )

‎src/core/qgsmaptopixel.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ class CORE_EXPORT QgsMapToPixel
117117
}
118118
#endif
119119

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

122123
//! Transform device coordinates to map (world) coordinates
123-
QgsPointXY toMapCoordinatesF( double x, double y ) const;
124+
QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF );
124125

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

132-
QgsPointXY toMapPoint( double x, double y ) const;
133+
/**
134+
* Transform device coordinates to map (world) coordinates
135+
* \deprecated since QGIS 3.4 use toMapCoordinates instead
136+
*/
137+
Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const;
133138

134139
/**
135140
* Set map units per pixel

‎src/core/qgsvectorlayerlabelprovider.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ QgsGeometry QgsVectorLayerLabelProvider::getPointObstacleGeometry( QgsFeature &f
319319
//TODO - remove when labeling is refactored to use screen units
320320
for ( int i = 0; i < boundLineString->numPoints(); ++i )
321321
{
322-
QgsPointXY point = context.mapToPixel().toMapCoordinates( boundLineString->xAt( i ), boundLineString->yAt( i ) );
322+
QgsPointXY point = context.mapToPixel().toMapCoordinates( static_cast<int>( boundLineString->xAt( i ) ),
323+
static_cast<int>( boundLineString->yAt( i ) ) );
323324
boundLineString->setXAt( i, point.x() );
324325
boundLineString->setYAt( i, point.y() );
325326
}

‎src/core/raster/qgsrasterlayerrenderer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
7979
// TODO: provide a method of QgsMapToPixel to fetch map center
8080
// in geographical units
8181
QgsPointXY center = mapToPixel.toMapCoordinates(
82-
mapToPixel.mapWidth() / 2.0,
83-
mapToPixel.mapHeight() / 2.0
82+
static_cast<int>( mapToPixel.mapWidth() / 2.0 ),
83+
static_cast<int>( mapToPixel.mapHeight() / 2.0 )
8484
);
8585
mapToPixel.setMapRotation( 0, center.x(), center.y() );
8686
}
@@ -188,10 +188,10 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
188188
mRasterViewPort->mBottomRightPoint.setY( std::ceil( mRasterViewPort->mBottomRightPoint.y() ) );
189189
// recalc myRasterExtent to aligned values
190190
myRasterExtent.set(
191-
mapToPixel.toMapCoordinatesF( mRasterViewPort->mTopLeftPoint.x(),
192-
mRasterViewPort->mBottomRightPoint.y() ),
193-
mapToPixel.toMapCoordinatesF( mRasterViewPort->mBottomRightPoint.x(),
194-
mRasterViewPort->mTopLeftPoint.y() )
191+
mapToPixel.toMapCoordinates( mRasterViewPort->mTopLeftPoint.x(),
192+
mRasterViewPort->mBottomRightPoint.y() ),
193+
mapToPixel.toMapCoordinates( mRasterViewPort->mBottomRightPoint.x(),
194+
mRasterViewPort->mTopLeftPoint.y() )
195195
);
196196

197197
//raster viewport top left / bottom right are already rounded to int

‎src/gui/qgshighlight.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void QgsHighlight::updateRect()
407407
// This is an hack to pass QgsMapCanvasItem::setRect what it
408408
// expects (encoding of position and size of the item)
409409
const QgsMapToPixel &m2p = mMapCanvas->mapSettings().mapToPixel();
410-
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
410+
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
411411
double res = m2p.mapUnitsPerPixel();
412412
QSizeF imageSize = mMapCanvas->mapSettings().outputSize();
413413
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + imageSize.width()*res, topLeft.y() - imageSize.height()*res );

‎src/gui/qgsmapcanvas.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ QgsRectangle QgsMapCanvas::imageRect( const QImage &img, const QgsMapSettings &m
681681
// This is a hack to pass QgsMapCanvasItem::setRect what it
682682
// expects (encoding of position and size of the item)
683683
const QgsMapToPixel &m2p = mapSettings.mapToPixel();
684-
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
684+
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
685685
double res = m2p.mapUnitsPerPixel();
686686
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + img.width()*res, topLeft.y() - img.height()*res );
687687
return rect;
@@ -1558,7 +1558,7 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )
15581558

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

@@ -1599,7 +1599,7 @@ void QgsMapCanvas::zoomWithCenter( int x, int y, bool zoomIn )
15991599
else
16001600
{
16011601
// transform the mouse pos to map coordinates
1602-
QgsPointXY center = getCoordinateTransform()->toMapPoint( x, y );
1602+
QgsPointXY center = getCoordinateTransform()->toMapCoordinates( x, y );
16031603
QgsRectangle r = mapSettings().visibleExtent();
16041604
r.scale( scaleFactor, &center );
16051605
setExtent( r, true );

‎src/gui/qgsmaptoolpan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void QgsMapToolPan::canvasReleaseEvent( QgsMapMouseEvent *e )
8181
else // add pan to mouse cursor
8282
{
8383
// transform the mouse pos to map coordinates
84-
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
84+
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
8585
mCanvas->setCenter( center );
8686
mCanvas->refresh();
8787
}
@@ -127,7 +127,7 @@ void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
127127
QPoint pos = gesture->centerPoint().toPoint();
128128
pos = mCanvas->mapFromGlobal( pos );
129129
// transform the mouse pos to map coordinates
130-
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
130+
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
131131
QgsRectangle r = mCanvas->extent();
132132
r.scale( 1 / gesture->totalScaleFactor(), &center );
133133
mCanvas->setExtent( r );

‎src/gui/qgsrubberband.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void QgsRubberBand::updateRect()
538538
// This is an hack to pass QgsMapCanvasItem::setRect what it
539539
// expects (encoding of position and size of the item)
540540
qreal res = m2p.mapUnitsPerPixel();
541-
QgsPointXY topLeft = m2p.toMapPoint( r.xMinimum(), r.yMinimum() );
541+
QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
542542
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width()*res, topLeft.y() - r.height()*res );
543543

544544
setRect( rect );

‎src/gui/qgssnaptogridcanvasitem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ void QgsSnapToGridCanvasItem::updateZoomFactor()
148148
const QgsPointXY centerPoint = mMapCanvas->extent().center();
149149
const QPointF canvasCenter = toCanvasCoordinates( centerPoint );
150150

151-
const QgsPointXY pt1 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( canvasCenter.x() - threshold, canvasCenter.y() - threshold );
152-
const QgsPointXY pt2 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( canvasCenter.x() + threshold, canvasCenter.y() + threshold );
151+
const QgsPointXY pt1 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( canvasCenter.x() - threshold ),
152+
static_cast<int>( canvasCenter.y() - threshold ) );
153+
const QgsPointXY pt2 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( canvasCenter.x() + threshold ),
154+
static_cast<int>( canvasCenter.y() + threshold ) );
153155

154156
const QgsPointXY layerPt1 = mTransform.transform( pt1, QgsCoordinateTransform::ReverseTransform );
155157
const QgsPointXY layerPt2 = mTransform.transform( pt2, QgsCoordinateTransform::ReverseTransform );

‎src/quickgui/qgsquickmapsettings.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ QPointF QgsQuickMapSettings::coordinateToScreen( const QgsPoint &point ) const
118118

119119
QgsPoint QgsQuickMapSettings::screenToCoordinate( const QPointF &point ) const
120120
{
121-
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.toPoint() );
121+
// use floating point precision with mapToCoordinates (i.e. do not use QPointF::toPoint)
122+
// this is to avoid rounding errors with an odd screen width or height
123+
// and the point being set to the exact center of it
124+
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x(), point.y() );
122125
return QgsPoint( pp );
123126
}
124127

‎tests/src/core/testqgsmaptopixel.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestQgsMapToPixel: public QObject
2828
void rotation();
2929
void getters();
3030
void fromScale();
31-
void toMapPoint();
31+
void toMapCoordinates();
3232
};
3333

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

43-
QgsPointXY b = m2p.toMapCoordinatesF( d.x(), d.y() ); // transform back
43+
QgsPointXY b = m2p.toMapCoordinates( d.x(), d.y() ); // transform back
4444
QCOMPARE( p, b );
4545

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

109-
void TestQgsMapToPixel::toMapPoint()
109+
void TestQgsMapToPixel::toMapCoordinates()
110110
{
111111
QgsMapToPixel m2p( 1, 5, 5, 10, 10, 90 );
112-
QgsPointXY p = m2p.toMapPoint( 5, 5 );
112+
QgsPointXY p = m2p.toMapCoordinates( 5, 5 );
113113
QCOMPARE( p, QgsPointXY( 5, 5 ) );
114114

115-
p = m2p.toMapPoint( 10, 10 );
115+
p = m2p.toMapCoordinates( 10, 10 );
116116
QCOMPARE( p, QgsPointXY( 10, 10 ) );
117117

118-
p = m2p.toMapPoint( 20, 20 );
118+
p = m2p.toMapCoordinates( 20, 20 );
119119
QCOMPARE( p, QgsPointXY( 20, 20 ) );
120120
}
121121

0 commit comments

Comments
 (0)
Please sign in to comment.