Hi peter, Me again, sorry!
I've implemented the code following your suggestions, but it throws the following error: ================================================================== Traceback (most recent call last): File "C:\Documents and Settings\Benoit/.qgis/python/plugins\bcccoltbl\cdlgTBL.py", line 373, in accept self.theLayer.rasterShader().setColorRampItemList(theTBL) AttributeError: 'QgsRasterShader' object has no attribute 'setColorRampItemList' Python version: 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] QGIS version: 1.5.0-Tethys Tethys, 13923M ================================================================== The code snippet is as follows: 367 # try to load the selected table directly on the selected raster 368 theTBL = self.ParseTBL(LTBL) 369 if not theTBL: 370 return 371 # 372 self.theLayer.setColorShadingAlgorithm( "ColorRampShader" ) 373 self.theLayer.rasterShader().setColorRampItemList(theTBL) 374 self.theLayer.rasterShader().setColorRampType(QgsColorRampShader.ColorRamp_T YPE.DISCRETE) Problem is that I don't know the reason for the error! Can it be that: - theTBL is not valid? (see below how it is created) - a typo? - self.theLayer is truly a rasterLayer because the plugin will not load otherwise. - line 373 excecutes with no error - A look at the C++ code for the raster properties 'Apply' colormap section seems to indicate the code above is correct. So I'm at a loss here. Do I miss an object? Moreover, if I swap line 373 and 374 and run the code I do have the same error: ================================================================== Traceback (most recent call last): File "C:\Documents and Settings\Benoit/.qgis/python/plugins\bcccoltbl\cdlgTBL.py", line 373, in accept self.theLayer.rasterShader().setColorRampType(QgsColorRampShader.ColorRa mp_TYPE.DISCRETE) AttributeError: 'QgsRasterShader' object has no attribute 'setColorRampType' ================================================================== Could you sort me out? Code for reading the color ramp list: def ParseTBL(self, LTBL): """ Parse the colour table (QML file) to create a QgsColorRampShader::ColorRampItem Struct """ theTBLfile = os.path.join(self.FilePath,str(LTBL)) # full name to the colour table to load if not os.path.exists(theTBLfile): QMessageBox.critical(self,MSG_BOX_TITLE,"ERROR: colour table not found:\n"+str(theTBLfile)) return False else: sc = open(theTBLfile,'r') ar = sc.readlines() sc.close() # nC = 0 colorRampEntries = [] # for a in ar: if a.find(",") >= 0: #value,r,g,b,alpha #-0.65480690,1,1,1,255, br = a.split(",") red = int(br[1]) #red gre = int(br[2]) #green blu = int(br[3]) #blue alp = int(br[4]) #alpha colorRampEntries.append(QgsColorRampShader.ColorRampItem ()) colorRampEntries[nC].label = QString(" ") colorRampEntries[nC].value = float(br[0]) colorRampEntries[nC].color = QColor(red, gre, blu, alp) nC += 1 # return colorRampEntries The above code seems to work because on printing str(colorRampEntries) I've got: [<qgis.core.ColorRampItem object at 0x124D8810>, ..., <qgis.core.ColorRampItem object at 0x12575108>] Add printing str(theTBL[0].value)+" "+str(theTBL[0].color) produces: 1.0 <PyQt4.QtGui.QColor object at 0x125785E0> Sorry again for the length of this message, and take into consideration that I'm very new to Python as well! Thanks, Benoit > -----Original Message----- > From: qgis-developer-boun...@lists.osgeo.org > [mailto:qgis-developer-boun...@lists.osgeo.org]on Behalf Of Peter Ersts > Sent: Thursday, 05 August 2010 15:33 > To: qgis-developer@lists.osgeo.org > Subject: Re: [Qgis-developer] How to load a colour table on a > raster,programmatically (in python)? > > > On 08/05/2010 05:25 AM, Benoit de Cabissole wrote: > > Could you please point me to the function(s) that are used to > match a custom > > colour table (QgsColorRampShader?) to a raster? > > > > > > Howdy Benoit, > > First you will need to set the color shading algorithm for the raster, > >> YourRasterLayer.setColorShadingAlgorithm( "ColorRampShader" ) > This creates a new shader for the raster. > > Next you will need to build your color ramp (palette). This is just a > list of items of type QgsColorRampShader.ColorRampItem, Each item has > three variables, lable (QString), color (QColor) , and value (double) > > Once you have your list you will need to give that list to the shader. > >> YourRasterLayer.rasterShader().setColorRampItemList( YourColorRamp ) > > Then you need to set the color ramp type: > >> YourRasterLayer.rasterShader().setColorRampType( TheType ) > > Where TheType is, INTERPOLATED, DISCRETE or EXACT. If your color ramp > only had three entries: > Label1, Red, 0 > Label2, Yellow, 127 > Label3, Green, 255 > > exact - looks for an exact match at the time of shading, a value of 90 > would not be shaded. > > discrete - is a bin type approach, images pixels with values of 50, 70, > or 90 would all be assigned the same color > > interpolated - is as the name says, 90 will be an interpolated color > between Red and Yellow. In this case, more yellow than red as 90 is > closer to 127 than 0. > > > Hope that helps. > -pete > > -- > ==================================== > Peter J. Ersts, Software Developer > American Museum of Natural History > Center for Biodiversity and Conservation > Central Park West at 79th Street > New York, New York 10024 > Web: http://biodiversityinformatics.amnh.org > Web: http://cbc.amnh.org > > Open Source, > ...evolving through community cooperation to change the world bit by bit > > Quantum GIS Raster Development Team. > > _______________________________________________ > Qgis-developer mailing list > Qgis-developer@lists.osgeo.org > http://lists.osgeo.org/mailman/listinfo/qgis-developer _______________________________________________ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer