6

I have a code that takes a polygon layers and returns a temp point layer, I want to change the symbology for the point layer to the "topo pop capital" point symbol through Python.

fn = "C:/Users/Public/Desktop/OSGeo4W/bigBoi.shp"

layer = QgsVectorLayer (fn)

feats = layer.getFeatures()

vl = QgsVectorLayer("Point", "temp", "memory")
pr = vl.dataProvider()


for feat in feats:
    geom =feat.geometry()
    geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
    if geom.type() == QgsWkbTypes.PolygonGeometry:
        if geomSingleType :
            x = geom.asPolygon()
            for i in x:
                for y in i:
                    for q in y:
                        f = QgsFeature()
                        f.setGeometry(QgsGeometry.fromPointXY(q))
                        pr.addFeature(f)
                        vl.updateExtents() 
                        QgsProject.instance().addMapLayer(vl)       

        else:
            x = geom.asMultiPolygon()
            for i in x:
                for y in i:
                    for q in y:                         
                        print(q)                   
                        f = QgsFeature()
                        f.setGeometry(QgsGeometry.fromPointXY(q))
                        pr.addFeature(f)
                        vl.updateExtents() 
                        QgsProject.instance().addMapLayer(vl)

I found the setSymbol fucntion that takes a symbol and renders it on a layer like so:

symbol = QgsMarkerSymbolV2.createSimple({'name': 'square', 'color': 'red'})
layer.rendererV2().setSymbol(symbol)

I also found that you can "copy symbol" by right clicking on an existing layer with the desired symbol, but that returns an HTML-like code which for some reason I can't paste here.

Is there a way to create the symbol with the copy symbol output as an object? Is the topo pop capital a symbol layer? And if so how do I appendSymbolLayer() if I don't know how to reference it. Can I call to the symbols from the single symbol favorites? Or maybe export the symbol to get it in Python template?

3

You could use the following to set the layer style to an existing one in the style database:

layer = iface.activeLayer()
get_styles = QgsStyle.defaultStyle()
style = get_styles.symbol('topo pop capital')

# Apply style and refresh symbology
layer.renderer().setSymbol(style)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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