3

I'll try to rendering single band raster (GEBCO bathymetry) by using PyQGIS.. This is my script so far

bathymetry_file = "E:/byth_gebco.tif"

layer_bathymetry = QgsRasterLayer(bathymetry_file, 'Raster Byth')


s = QgsRasterShader()
c = QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
c.setClassificationMode(CONTINUOS)
c.setMaximumValue(625)
c.setMinimumValue(-6244)
i = [
    QgsColorRampShader.ColorRampItem(0, QtGui.QColor('#f7fbff'), '0'),
    QgsColorRampShader.ColorRampItem(-200, QtGui.QColor('#deebf7'), '-200'),
    QgsColorRampShader.ColorRampItem(-1000, QtGui.QColor('#c6dbef'), '-1000'),
    QgsColorRampShader.ColorRampItem(-2000, QtGui.QColor('#9ecae1'), '-2000'),
    QgsColorRampShader.ColorRampItem(-3000, QtGui.QColor('#6baed6'), '-3000'),
    QgsColorRampShader.ColorRampItem(-4000, QtGui.QColor('#4292c6'), '-4000'),
    QgsColorRampShader.ColorRampItem(-5000, QtGui.QColor('#2171b5'), '-5000'),
    QgsColorRampShader.ColorRampItem(-6000, QtGui.QColor('#08306b'), '-6000')
]
c.setColorRampItemList(i)
s.setRasterShaderFunction(c)
ps = QgsSingleBandPseudoColorRenderer(layer_bathymetry.dataProvider(), 1, s)
layer_bathymetry.setRenderer(ps)
QgsMapLayerRegistry.instance().addMapLayer(layer_bathymetry)

This is the expected result: enter image description here

But this is what i've got from that script enter image description here

It seems like QGIS API can't handle the negative pixel value, what should i do to render that bathymetry remind that bathymetry has negative elevation?

  • Are the negative values shown in the Layer > Properties > Style interface? Does it work if you do it manually? – Joseph Jun 16 '17 at 13:16
  • @Joseph Yes it's works.. But i need to do this in python – Faizalprabowo Jun 16 '17 at 15:22
  • what happens if you reverse the order of the ColorRampItem objects in your list i, so that the values increase? (so -6000,-5000,-4000...,0) – Steven Kay Jun 17 '17 at 10:16
  • the result is still the same as example above @StevenKay – Faizalprabowo Jun 19 '17 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.