Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix GetMap tile_buffer when labels are rotated
  • Loading branch information
troopa81 authored and github-actions[bot] committed Sep 14, 2021
1 parent 91cdf3a commit 816757d
Show file tree
Hide file tree
Showing 9 changed files with 1,586 additions and 329 deletions.
10 changes: 10 additions & 0 deletions python/core/auto_generated/qgsmapsettings.sip.in
Expand Up @@ -486,6 +486,16 @@ Returns the visible area as a polygon (may be rotated)

.. versionadded:: 2.8
%End

QPolygonF visiblePolygonWithBuffer() const;
%Docstring
Returns the visible area as a polygon (may be rotated) with extent buffer included

.. seealso:: :py:func:`extentBuffer`

.. versionadded:: 3.20
%End

double mapUnitsPerPixel() const;
%Docstring
Returns the distance in geographical coordinates that equals to one pixel in the map
Expand Down
8 changes: 5 additions & 3 deletions src/core/labeling/qgslabelingengine.cpp
Expand Up @@ -297,8 +297,11 @@ void QgsLabelingEngine::solve( QgsRenderContext &context )

QPainter *painter = context.painter();

QgsGeometry extentGeom = QgsGeometry::fromRect( mMapSettings.visibleExtent() );
QPolygonF visiblePoly = mMapSettings.visiblePolygon();
QgsRectangle r1 = mMapSettings.visibleExtent();
r1.grow( mMapSettings.extentBuffer() );
QgsGeometry extentGeom = QgsGeometry::fromRect( r1 );

QPolygonF visiblePoly = mMapSettings.visiblePolygonWithBuffer();
visiblePoly.append( visiblePoly.at( 0 ) ); //close polygon

// get map label boundary geometry - if one hasn't been explicitly set, we use the whole of the map's visible polygon
Expand Down Expand Up @@ -773,4 +776,3 @@ QgsLabeling::LinePlacementFlags QgsLabelingUtils::decodeLinePlacementFlags( cons
flags |= QgsLabeling::LinePlacementFlag::MapOrientation;
return flags;
}

2 changes: 1 addition & 1 deletion src/core/qgsmaprendererjob.cpp
Expand Up @@ -619,7 +619,7 @@ LabelRenderJob QgsMapRendererJob::prepareLabelingJob( QPainter *painter, QgsLabe

QgsRectangle r1 = mSettings.visibleExtent();
r1.grow( mSettings.extentBuffer() );
job.context.setExtent( mSettings.visibleExtent() );
job.context.setExtent( r1 );

job.context.setFeatureFilterProvider( mFeatureFilterProvider );

Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -388,6 +388,28 @@ QPolygonF QgsMapSettings::visiblePolygon() const
return poly;
}

QPolygonF QgsMapSettings::visiblePolygonWithBuffer() const
{
QPolygonF poly;

const QSize &sz = outputSize();
const QgsMapToPixel &m2p = mapToPixel();

// Transform tilebuffer in pixel.
// Original tilebuffer is in pixel and transformed only according
// extent width (see QgsWmsRenderContext::mapTileBuffer)

const double mapUnitsPerPixel = mExtent.width() / sz.width();
const double buffer = mExtentBuffer / mapUnitsPerPixel;

poly << m2p.toMapCoordinates( -buffer, -buffer ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), -buffer ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), static_cast<double>( sz.height() + buffer ) ).toQPointF();
poly << m2p.toMapCoordinates( -buffer, static_cast<double>( sz.height() + buffer ) ).toQPointF();

return poly;
}

double QgsMapSettings::mapUnitsPerPixel() const
{
return mMapUnitsPerPixel;
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsmapsettings.h
Expand Up @@ -449,6 +449,14 @@ class CORE_EXPORT QgsMapSettings : public QgsTemporalRangeObject
* \since QGIS 2.8
*/
QPolygonF visiblePolygon() const;

/**
* Returns the visible area as a polygon (may be rotated) with extent buffer included
* \see extentBuffer()
* \since QGIS 3.20
*/
QPolygonF visiblePolygonWithBuffer() const;

//! Returns the distance in geographical coordinates that equals to one pixel in the map
double mapUnitsPerPixel() const;

Expand Down
19 changes: 19 additions & 0 deletions tests/src/python/test_qgsserver_wms_getmap.py
Expand Up @@ -1806,6 +1806,25 @@ def test_wms_getmap_tile_buffer(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Tiled_Labels_True")

# Check with rotated labels and tiled=true
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(
os.path.join(self.testdata_path, 'wms_tile_buffer.qgs')),
"SERVICE": "WMS",
"VERSION": "1.3.0",
"REQUEST": "GetMap",
"BBOX": "317654,6163276,327603,6173244",
"CRS": "EPSG:3857",
"WIDTH": "512",
"HEIGHT": "512",
"LAYERS": "wms_tile_buffer_labels_rotated",
"FORMAT": "image/png",
"TILED": "true"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Tiled_Rotated_Labels_True")

@unittest.skipIf(os.getenv('QGIS_CONTINUOUS_INTEGRATION_RUN'), "This tests fails on GH workflow")
def test_mode8bit_with_transparency(self):
# 8 bits
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,846 changes: 1,521 additions & 325 deletions tests/testdata/qgis_server/wms_tile_buffer.qgs

Large diffs are not rendered by default.

0 comments on commit 816757d

Please sign in to comment.