3

In QGIS is it possible that when I create a shapefile and give it a file name e.g. Site, that I can assign symbology to that automatically?

I have a set symbol for Site and many other features but would love it if I could auto assign the symbology based on the file name.

I know this is possible in ArcMap but am trying to find out whether this can be done in QGIS.

What I mean in the context of ArcMap

  • How is this done in ArcMap? I'm trying to figure out what this question means... – DPSSpatial Nov 2 '17 at 18:21
  • Please see the edit above, which describes it in the context of ArcMap. – AWGIS Nov 3 '17 at 8:50
  • @AGW94 - Perhaps not natively as you can assign default symbology to features when any layer is loaded (Project > Project Properties > Default styles) but you could create a python function which whenever a layer was saved using the Save As... option, it would automatically apply the style based on its name. I'm assuming these styles are saved as .qml files (e.g. Site.qml)? – Joseph Nov 3 '17 at 10:40
  • @Joseph, thanks for the comment. Yes that would be exactly what I am looking for. I have many .qml files saved in my style manager, but would love the automatic assignment when I use the Save As... option. – AWGIS Nov 3 '17 at 11:18
3

The following is a script which reads all .qml style files in a given directory and stores them in a dictionary. We can create a function connected to the layerSavedAs signal. When saving a layer via the Save As... option, the layer added to the map canvas can have a style loaded if both names match. The script can be used in the Python Console:

import glob, os

list_of_style_names = {}
stylePath = 'F:/QGIS Shared/Carterwood Styles'
for style in glob.glob(stylePath + '/*.qml'):
    styleName = os.path.splitext(os.path.basename(style))[0]
    list_of_style_names[styleName] = style

def applyStyle():
    layer = iface.activeLayer()
    if layer.name() in list_of_style_names:
        layer.loadNamedStyle(list_of_style_names[layer.name()])

iface.layerSavedAs.connect(applyStyle)

When finished, you could use the following to disconnect the signal and delete the function:

iface.layerSavedAs.disconnect(applyStyle)
del applyStyle
  • After playing around with this I cannot seem to get it to work, I have defined the style path. Do I need to do anything else? Apologies as my python and coding in general is poor. – AWGIS Nov 3 '17 at 13:12
  • @AGW94 - No need to apologise! How did you define your style path exactly? – Joseph Nov 3 '17 at 13:14
  • stylePath = 'F:\QGIS Shared\Carterwood Styles' – AWGIS Nov 3 '17 at 13:17
  • @AGW94 - Try using stylePath = 'F:/QGIS Shared/Carterwood Styles/' or stylePath = r'F:\QGIS Shared\Carterwood Styles\' :) – Joseph Nov 3 '17 at 13:19
  • Still can't seem to get it working with both of them – AWGIS Nov 3 '17 at 13:24
2

Joseph's method is amazing. An alternative that is less technical and automatic is to have a set of .qml style files that you cut and paste into the same folder as your shapefiles. If a .qml file has the same name as a shapefile and is in the same folder Qgis will use it to style the shapefile.

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.