Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rendering artifacts on the edges of resampled raster tiles
We now buffer the request for the input to a resample filter by a variable number of pixels (depending on the resampling type) in order to fetch the neighbouring pixels to the edges of individual raster blocks. This allows the resampling to utilise these neighbouring pixels when resampling the edges of tiles, avoiding discrepancies and rendering artifacts over the borders of raster tiles. Fixes #30152
- Loading branch information
1 parent
f53e137
commit a19cdf2
Showing
11 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsRasterResampler. | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
from builtins import str | ||
|
||
__author__ = 'Nyall Dawson' | ||
__date__ = '14/11/2019' | ||
__copyright__ = 'Copyright 2019, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
import os | ||
|
||
from qgis.PyQt.QtGui import qRed | ||
|
||
from qgis.core import (QgsRasterLayer, | ||
QgsRectangle, | ||
QgsRasterResampleFilter, | ||
QgsSingleBandGrayRenderer, | ||
QgsCubicRasterResampler, | ||
QgsBilinearRasterResampler | ||
) | ||
from utilities import unitTestDataPath | ||
from qgis.testing import start_app, unittest | ||
|
||
# Convenience instances in case you may need them | ||
# not used in this test | ||
start_app() | ||
|
||
|
||
class TestQgsRasterResampler(unittest.TestCase): | ||
|
||
def checkBlockContents(self, block, expected): | ||
res = [] | ||
for r in range(block.height()): | ||
res.append([qRed(block.color(r, c)) for c in range(block.width())]) | ||
self.assertEqual(res, expected) | ||
|
||
def testBilinearResample(self): | ||
path = os.path.join(unitTestDataPath(), 'landsat.tif') | ||
raster_layer = QgsRasterLayer(path, 'test') | ||
self.assertTrue(raster_layer.isValid()) | ||
|
||
extent = QgsRectangle(785994.37761193525511771, | ||
3346249.2209800467826426, | ||
786108.49096253968309611, | ||
3346362.94137834152206779) | ||
|
||
renderer = QgsSingleBandGrayRenderer(raster_layer.dataProvider(), 1) | ||
filter = QgsRasterResampleFilter(renderer) | ||
|
||
# default (nearest neighbour) resampling | ||
block = filter.block(1, extent, 2, 2) | ||
self.checkBlockContents(block, [[124, 127], [125, 126]]) | ||
|
||
block = filter.block(1, extent, 4, 4) | ||
self.checkBlockContents(block, [[124, 124, 127, 127], | ||
[124, 124, 127, 127], | ||
[125, 125, 126, 126], | ||
[125, 125, 126, 126]] | ||
) | ||
|
||
block = filter.block(1, extent, 8, 8) | ||
self.checkBlockContents(block, [[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126]]) | ||
|
||
# with resampling | ||
filter.setZoomedInResampler(QgsBilinearRasterResampler()) | ||
block = filter.block(1, extent, 2, 2) | ||
self.checkBlockContents(block, [[124, 127], [125, 126]]) | ||
|
||
block = filter.block(1, extent, 4, 4) | ||
self.checkBlockContents(block, | ||
[[124, 124, 126, 126], | ||
[124, 124, 125, 126], | ||
[124, 124, 125, 126], | ||
[125, 125, 125, 126]] | ||
) | ||
|
||
block = filter.block(1, extent, 8, 8) | ||
self.checkBlockContents(block, | ||
[[124, 124, 124, 125, 125, 126, 126, 126], | ||
[124, 124, 124, 125, 125, 126, 126, 126], | ||
[124, 124, 124, 124, 125, 125, 126, 126], | ||
[124, 124, 124, 124, 125, 125, 126, 126], | ||
[124, 124, 124, 124, 125, 125, 126, 126], | ||
[124, 124, 124, 124, 125, 125, 126, 126], | ||
[125, 125, 125, 125, 125, 125, 126, 126], | ||
[125, 125, 125, 125, 125, 125, 126, 126]] | ||
) | ||
|
||
def testCubicResample(self): | ||
path = os.path.join(unitTestDataPath(), 'landsat.tif') | ||
raster_layer = QgsRasterLayer(path, 'test') | ||
self.assertTrue(raster_layer.isValid()) | ||
|
||
extent = QgsRectangle(785994.37761193525511771, | ||
3346249.2209800467826426, | ||
786108.49096253968309611, | ||
3346362.94137834152206779) | ||
|
||
renderer = QgsSingleBandGrayRenderer(raster_layer.dataProvider(), 1) | ||
filter = QgsRasterResampleFilter(renderer) | ||
|
||
# default (nearest neighbour) resampling | ||
block = filter.block(1, extent, 2, 2) | ||
self.checkBlockContents(block, [[124, 127], [125, 126]]) | ||
|
||
block = filter.block(1, extent, 4, 4) | ||
self.checkBlockContents(block, [[124, 124, 127, 127], | ||
[124, 124, 127, 127], | ||
[125, 125, 126, 126], | ||
[125, 125, 126, 126]] | ||
) | ||
|
||
block = filter.block(1, extent, 8, 8) | ||
self.checkBlockContents(block, [[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[124, 124, 124, 124, 127, 127, 127, 127], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126]]) | ||
|
||
# with resampling | ||
filter.setZoomedInResampler(QgsCubicRasterResampler()) | ||
block = filter.block(1, extent, 2, 2) | ||
self.checkBlockContents(block, [[124, 127], [125, 126]]) | ||
|
||
block = filter.block(1, extent, 4, 4) | ||
self.checkBlockContents(block, | ||
[[124, 125, 127, 127], | ||
[124, 125, 126, 127], | ||
[125, 125, 126, 126], | ||
[125, 125, 126, 126]] | ||
) | ||
|
||
block = filter.block(1, extent, 8, 8) | ||
self.checkBlockContents(block, | ||
[[124, 124, 124, 125, 126, 127, 127, 127], | ||
[124, 124, 124, 125, 126, 127, 127, 127], | ||
[124, 124, 124, 125, 126, 127, 127, 127], | ||
[125, 124, 124, 125, 126, 126, 127, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126], | ||
[125, 125, 125, 125, 126, 126, 126, 126]] | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |