I'm having a .gdb file for a digital catastre, therefore i created .qml styles for the different lines, polygons and points. Is there a way to load them automatic when adding the vector? I already saved the .qml styles in the same folder with the same name.
- 1A few more ideas. If you use Shapefiles the .qml files will be automatically loaded. With PostgreSQL/PostGIS or Geopackage you can store the data in the database and make it the default style when a layer is added.HeikkiVesanto– HeikkiVesanto ♦2022-02-09 09:42:18 +00:00Commented Feb 9, 2022 at 9:42
Add a comment |
1 Answer
You can use PyQGIS and execute this code when you have added the layers.
It will find and store all styles files in a dictionary, with style file name as key and full path to the style file as value. Then apply each style file to the layer by matching style file name and layer name:
import os style_file_folder = r'C:\GIS\data\Bakgrundskartor_LMV' #Adjust style_files = {} for root, folder, files in os.walk(style_file_folder): #Find all style files in folder and add to style_files dictionary for file in files: fullname = os.path.join(root, file) if file.endswith('.qml'): style_files[file.split('.')[0]]=fullname #style_files is now: {'ak_riks': 'C:\\GIS\\data\\Bakgrundskartor_LMV\\ak_riks.qml', 'vl_riks': 'C:\\GIS\\data\\Bakgrundskartor_LMV\\vl_riks.qml'} for layer in QgsProject.instance().mapLayers().values(): #For each file added to the map if layer.name() in style_files: #Find matching style file layer.loadNamedStyle(style_files[layer.name()]) #And apply it layer.triggerRepaint() - 1Thank you, that's ingenious. How did I open a text editor pane like the one on the right in your screenshot? I was able to run this by pasting it into the Python command line, and to create a whole Processing Tool by subclassing QgsProcessingAlgorithm, but it looks like there's some middle ground way to run short Python commands that I'm missing.Cowirrie– Cowirrie2022-02-10 08:35:54 +00:00Commented Feb 10, 2022 at 8:35
- 1Nice! Click the pen and paper to the right of the "play" arrow (Show editor), where it says "Python Console"Bera– Bera2022-02-10 08:48:34 +00:00Commented Feb 10, 2022 at 8:48
- 1Thank you. I was going to say I didn't have any toolbar at all in the Python Console, then I closed and reopened QGIS, and it's there now, so I can run code snippets like that. Thank you for explaining where it should be.Cowirrie– Cowirrie2022-02-10 09:59:50 +00:00Commented Feb 10, 2022 at 9:59
