Mittwoch, 31. Oktober 2012

Basics: Setting a custom symbology for QGIS-layers

So you want to use a custom symbology for your layers and achieve that through Python.

Let me tell you first: The easy way is the WRONG way!
Because QGIS crashes with a "Win32RuntimeException" if you try the easy way.
So DON'T GO THERE and brace yourself for a detour!

Instead of creating a new Symbol, which contains all the fancy SymbologyLayers with your amazing Arrowheads for PolyLines and whatnot, you create just your beautiful SymbologyLayers and add it to the pre-existing Symbol.

This example shows a function which you can use to set a symbologystyle for a PolyLine-Layer with two styles to choose from: Awesome Arrowheads for your lines, or ugly dots.



 # adds a LineLayerStyle (Arrow, dotted)  
   def setLineLayerStyle(self, layer, style):  
     if(style == "arrow"):  
       sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("LineDecoration").createSymbolLayer({ 'width' : '0.26', 'color' : '0,0,0' })  
       symbollist = layer.rendererV2().symbols()  
       symbol = symbollist[0]  
       symbol.appendSymbolLayer(sl)  
     elif(style == "dotted"):  
       sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("MarkerLine").createSymbolLayer({ 'width' : '0.26', 'color' : '0,0,0' })  
       symbollist = layer.rendererV2().symbols()  
       symbol = symbollist[0]  
       symbol.appendSymbolLayer(sl)  
 self.canvas.refresh()  

Done.

What can i use instead of bloody arrows or dots you ask?! Read the pyqgis-cookbook or something.

or don't....because you wouldn't find it there in the first place.

Keine Kommentare:

Kommentar veröffentlichen