Wednesday 19 July 2017

pyqgis - GIS Action that loads a raster, creates pyramids and sets color


Using the code in "Developing QGIS Action which loads a raster".


from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem

s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useGlobal" )
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)

root = QgsProject.instance().layerTreeRoot()
group_name = "Raster layers"
group = root.findGroup(group_name)
if group == None:
group = root.addGroup("Raster layers")
else:
pass
fileName = '\\\\rvphnas02pw\Prodotti_Cartografici\\Ortofoto\\2012_AGEA\\TIFF_GBO\\[% A_CODICE %].tif'
fileInfo = QFileInfo(fileName)
baseName = '[% A_CODICE %]'

rlayer = QgsRasterLayer(fileName, baseName)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(3003)
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))

s.setValue( "/Projections/defaultBehaviour", oldValidation )

Is it possible to create automatically the image-pyramids and set color white as trasparent?




Answer



Finally I found the solution using the code in :


-"How to set transparency to multiple raster values with Python" to set the colors and this


-"How to correctly use gdaladdo in a Python program?" to create the pyramids


from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem
import gdal
import os
from subprocess import call


#Define gdaladdo
gdaladdoFile = 'C:\\Program Files\\QGIS 2.18\\bin\\gdaladdo.exe'


s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useProject" )
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()

group_name = "CTR_10000"
group = root.findGroup(group_name)
if group == None:
group = root.addGroup("CTR_10000")
else:
pass
fileName = 'D:\\GIS\\Dati\\IDT_CTRR_Raster\\[% A_CODICE %].tif'
fileInfo = QFileInfo(fileName)
baseName = '[% A_CODICE %]'
rlayer = QgsRasterLayer(fileName, baseName)


call([gdaladdoFile, '-ro', '--config', 'USE_RRD', 'YES', fileName, '2 4 8 16'])

raster_transparency = rlayer.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)

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

raster_transparency.setTransparentSingleValuePixelList(tr_list)
crs = QgsCoordinateReferenceSystem()

crs.createFromSrid(3003)
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))

s.setValue( "/Projections/defaultBehaviour", oldValidation )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...