0

I'm trying to use Python to add a GML to my QGIS 3.4 project, apply styling to it, and then export the layer as a shapefile.

It exports the layer, but it does so before applying the styling, which uses Rule-based symbology, which filters some data away that I want separated in different shapefiles.

I think it has to do with layer it exports, but I need it to export the layer after it added the styling to it,

writer5 = QgsVectorFileWriter.writeAsVectorFormat(layer5,directory + shpmap + "/road1.shp","utf-8",driverName="ESRI Shapefile")

Does anyone have an idea how to do this?

import os

from qgis.core import QgsProject
directory = QgsProject.instance().readPath("./")
print(directory)
root = QgsProject.instance().layerTreeRoot()


styles = "/Styles"
importfolder = "/Import"
shpfolder = "/Shpexport"
dxffolder = "/Dxfexport"

group1 ="Group"
group2 = "Analyse"
group3 = "Analyse2"

file_to_open5 = directory + importfolder +"/road1.gml"
file_to_open6 = directory + importfolder + "/road2.gml"

style5 = directory + styles +"/road1.qml"
style6 = directory + styles + "/road2.qml"


group1 = root.addGroup(group1)
group2 = root.addGroup(group2)
group3 = root.addGroup(group3)

layer5 = QgsVectorLayer(file_to_open5, "road1", "ogr")
if not layer5.isValid():
    print ("road1 failed to load!")
else:
    print ("road1 was loaded successfully!")
    layer5.loadNamedStyle(style5)
group1.addLayer(layer5)

layer6 = QgsVectorLayer(file_to_open5, "road2", "ogr")
if not layer6.isValid():
    print ("road2 failed to load!")
else:
    print ("road2 was loaded successfully!")
    layer6.loadNamedStyle(style6)
group1.addLayer(layer6)


writer5 = QgsVectorFileWriter.writeAsVectorFormat(layer5,directory + shpmap + "/road1.shp","utf-8",driverName="ESRI Shapefile")
writer6 = QgsVectorFileWriter.writeAsVectorFormat(layer6,directory + shpmap + "/road2.shp","utf-8",driverName="ESRI Shapefile")
1
  • I figured it out, my assumption that the styling didnt export was wrong. The styling doesnt filter the output for the objects. You need to add a layer.setSubsetString("put your filter here") for queries you want filtered from your data. After adding this, it exported the layer correctly with the filters in place!
    – GladH
    Commented Feb 10, 2020 at 15:56

1 Answer 1

0

I figured it out, my assumption that the styling didnt export was wrong. The styling doesnt filter the output for the objects. You need to add a

layer.setSubsetString("put your filter here") 

for queries you want filtered from your data. After adding this, it exported the layer correctly with the filters in place!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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