如何使用Python将透明度设置为多个栅格值


11

使用以下代码,我可以将100%透明度设置为栅格值0:

map=None

for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
  if lyr.name() == "some_layer":
    map= lyr

tr=None

tr=QgsRasterTransparency()
tr.initializeTransparentPixelList(0)
map.renderer().setRasterTransparency(tr)
map.triggerRepaint()

如何定义两个值(0和8)为100%透明?

当我用栅格值8重复最后一个块的命令时,第一个值(0)从图层透明度中删除。

Answers:


10

我发现了另一个有用的线程。如何使用Python在QGIS 2.0中设置图层透明性?似乎应该有一种更短,更有效的方法,但是我对此进行了测试,它的工作原理是:

print 'Start'
active_layer = qgis.utils.iface.mapCanvas().currentLayer()
raster_transparency  = active_layer.renderer().rasterTransparency()
ltr = QgsRasterTransparency.TransparentSingleValuePixel()
ltr2 =  QgsRasterTransparency.TransparentSingleValuePixel()
tr_list = []
ltr.min = 0  # Or another value
ltr.max = 0  # Or another value
ltr.percentTransparent = 100  # Or another value
ltr2.min = 8  # Or another value
ltr2.max = 8  # Or another value
ltr2.percentTransparent = 100
tr_list.append(ltr)
tr_list.append(ltr2)

raster_transparency.setTransparentSingleValuePixelList(tr_list)

active_layer.triggerRepaint()  # Tried with iface.mapCanvas().refresh(), but it didn't work
print 'Finish'

2

需要Python吗?如果我了解您想要的内容,则可以在QGIS UI的栅格图层属性中执行以下操作: 在此处输入图片说明


是的,Python是必需的。我已经构建了一个插件,可以将地形图加载到QGIS项目中,该项目是从图纸线系统中选择的。
Stefan
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.