1

I have a Python application and it uses PyQGIS to - read NetCDF file, convert it to GeoTIFF by writing the rasterlayer with rasterpipe, QgsRasterFileWriter and rasterShader related methods.

However, there is too much pixelization in the TIFF when I zoom in.

input_nc_filename = "input.nc"
rlayer = QgsRasterLayer(input_nc_filename, "east")

resampleFilter = rlayer.resampleFilter()
resampleFilter.setZoomedInResampler(QgsCubicRasterResampler())
resampleFilter.setZoomedOutResampler(QgsCubicRasterResampler())
resampleFilter.setZoomedOutResampler(None)

extent = rlayer.extent()
provider = rlayer.dataProvider()
has_stats = provider.hasStatistics(1, QgsRasterBandStats.All)
stats = provider.bandStatistics(1, QgsRasterBandStats.All, extent, 0)

if has_stats is not False:
    logger.debug("minimumValue = {}".format(stats.minimumValue))
    logger.debug("maximumValue = {}".format(stats.maximumValue))

if stats.minimumValue < 0:
    stats_min = 0
else:
    stats_min = stats.minimumValue
stats_max = stats.maximumValue


width, height = rlayer.width(), rlayer.height()
renderer = rlayer.renderer()
provider = rlayer.dataProvider()

pipe = QgsRasterPipe()
pipe.set(provider.clone())
pipe.set(renderer.clone())

output_filename = "qgis_cubic.tif"
file_writer = QgsRasterFileWriter(output_filename)

file_writer.writeRaster(pipe, width, height, extent, rlayer.crs())

When I tried setting the re-sampling algorithm to Cubic in QGIS desktop application, it worked perfectly. Now I am trying to do the same in my Python application. I came across few Python classes related to re-sampling -

  1. QgsRasterResampleFilter
  2. QgsRasterResampler, QgsRasterResamplerV2
  3. QgsBilinearRasterResampler, QgsCubicRasterResampler

Unfortunately, I could not get a breakthrough to use these classes while generating the GeoTIFF. We use GeoTIFF in a webapp developed in Mapbox GL JS and Angular and not in QGIS hosted environment.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.