Im trying to create a new layer and change it to SVG marker and set blue-marker.svg as the marker but its not working.

Here is what i have:

        iface.addVectorLayer( "Point", "marker_layer", "memory")
        mlayer = self.iface.activeLayer()
        symbol = QgsSymbolV2.defaultSymbol(mlayer.geometryType())
        svgStyle = {}
        svgStyle['name'] = '/usr/share/qgis/svg/symbol/blue-marker.svg'
        svgStyle['outline'] = '#000000'
        svgStyle['outline-width'] = '0'
        svgStyle['size'] = '6'
        symbol_layer = QgsSvgMarkerSymbolLayerV2.create(svgStyle)
        symbol.changeSymbolLayer(0, symbol_layer)

Im not getting any errors but the new layer is not set as SVG neither is the default marker to blue-marker.svg

EDIT: here is the solution:

        layer = self.iface.activeLayer()
        svgStyle = {}
        svgStyle['name'] = '/usr/share/qgis/svg/symbol/blue-marker.svg'
        svgStyle['outline'] = '#000000'
        svgStyle['outline-width'] = '0'
        svgStyle['size'] = '12'
        symbol_layer = QgsSvgMarkerSymbolLayerV2.create(svgStyle)
        layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer)

Based on this approach Using SVG for layer symbols

First, we'll define the path to the shapefile:

Copy  
src = "/Users/joellawhead/qgis_data/NYC_MUSEUMS_GEO/NYC_MUSEUMS_GEO.shp"

Next, we'll load the layer:

Copy  
lyr = QgsVectorLayer(src, "Museums", "ogr")

Now, we define the properties of the symbol, including the location of the SVG file as a Python dictionary:

Copy
svgStyle = {}
svgStyle['fill'] = '#0000ff'
svgStyle['name'] = 'landmark/tourism=museum.svg'
svgStyle['outline'] = '#000000'
svgStyle['outline-width'] = '6.8'
svgStyle['size'] = '6'

Then, we create an SVG symbol layer:

Copy
symLyr1...
  • Isnt the layer already loaded? the solution you are giving is the same im using but you'r loading a layer when i want to use an existing layer (marker_layer) – José Gabriel Nov 8 '17 at 14:55
  • Try loading the layer and see how it works out first. – whyzar Nov 8 '17 at 15:04
  • i dont have that layer – José Gabriel Nov 8 '17 at 15:08
  • Try loading (marker_layer) – whyzar Nov 8 '17 at 16:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.