Skip to main content
added 940 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

A broader example involves retrieving the actual max and min values from the raster using the bandStatistics() method of QgsRasterDataProvider and passing them to the QgsColorRampShader constructor:

rl = iface.activeLayer()
prov = rl.dataProvider()
stats = prov.bandStatistics(1, QgsRasterBandStats.All, rl.extent(), 0)
min = stats.minimumValue
max = stats.maximumValue
color_ramp = QgsStyle().defaultStyle().colorRamp('Viridis')
ramp_shader = QgsColorRampShader(min, max, color_ramp)
ramp_shader.classifyColorRamp()# Add this line
raster_shader = QgsRasterShader()
raster_shader.setRasterShaderFunction(ramp_shader)
renderer = QgsSingleBandPseudoColorRenderer(rl.dataProvider(), 1, raster_shader)
rl.setRenderer(renderer)
rl.triggerRepaint()

Results on a test DEM layer:

enter image description here

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

A broader example involves retrieving the actual max and min values from the raster using the bandStatistics() method of QgsRasterDataProvider and passing them to the QgsColorRampShader constructor:

rl = iface.activeLayer()
prov = rl.dataProvider()
stats = prov.bandStatistics(1, QgsRasterBandStats.All, rl.extent(), 0)
min = stats.minimumValue
max = stats.maximumValue
color_ramp = QgsStyle().defaultStyle().colorRamp('Viridis')
ramp_shader = QgsColorRampShader(min, max, color_ramp)
ramp_shader.classifyColorRamp()# Add this line
raster_shader = QgsRasterShader()
raster_shader.setRasterShaderFunction(ramp_shader)
renderer = QgsSingleBandPseudoColorRenderer(rl.dataProvider(), 1, raster_shader)
rl.setRenderer(renderer)
rl.triggerRepaint()

Results on a test DEM layer:

enter image description here

deleted 158 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

By the way, a big +1 for using a minimal plugin file which is properly structured and for posting a path to a sample raster which made testing super easy!

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

By the way, a big +1 for using a minimal plugin file which is properly structured and for posting a path to a sample raster which made testing super easy!

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

added 6 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

By the way, a big +1 for using a minimal plugin file which is properly structured and for posting a path to a sample fileraster which made testing super easy!

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

By the way, a big +1 for using a minimal plugin file which is properly structured and posting a path to a sample file which made testing super easy!

I think you are just missing one line needed to create classifications from the color ramp:

ramp_shader.classifyColorRamp()

So, your modified run() method would look like:

def run(self):
    # not my actual file, but one which seems to reproduce the problem
    path = "/vsicurl/http://oin-hotosm.s3.amazonaws.com/59d33df023c8440011d7b26d/0/b378087a-c2a5-43a0-abec-71fcfb051150.tif"
    layer_intensity = self.iface.addRasterLayer(path, 'intensity')

    color_ramp = qgis.core.QgsStyle().defaultStyle().colorRamp('Viridis')
    ramp_shader = qgis.core.QgsColorRampShader(1, 255, color_ramp)
    ramp_shader.classifyColorRamp()# Add this line
    raster_shader = qgis.core.QgsRasterShader()
    raster_shader.setRasterShaderFunction(ramp_shader)

    renderer = qgis.core.QgsSingleBandPseudoColorRenderer(layer_intensity.dataProvider(), 1, raster_shader)

    layer_intensity.setRenderer(renderer)
    layer_intensity.triggerRepaint()

When I copy the logic from the method above and run directly in the QGIS Python console (after removing references to self) I get the following result:

enter image description here

By the way, a big +1 for using a minimal plugin file which is properly structured and for posting a path to a sample raster which made testing super easy!

Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading