9

I have a QGIS project with layers, based on PostGIS SQL queries that use current date. Since the data in the database changes, these queries return different data every new day.

Is it possible to dynamically change the name of the layer in layer tree panel, so it represents the current date? (i.e. layer name like Traffic on 24.01.2015, that changes every new day). The data for the caption should be taken from attribute of any feature in a layer - they all have the same value for field traffic_date.

  • What exactly would you extract from your database table, "Traffic" or the current date? – Germán Carrillo Jan 24 '15 at 0:20
  • I'm extracting data for current date to one layer and forecast data to other layers. So "24.01.2015" is actually a field value, that comes from the database. I need the layer name in the project to automatically change corresponding to the date. – mofoyoda Jan 24 '15 at 0:26
  • Would it be ok to change the name each time the project loads? – Nathan W Jan 24 '15 at 1:59
13

If I got you right, the answer is yes, QGIS supports dynamic layer names.

You would need to write a Python macro to be ran every time the project is opened. This would be the workflow:

  1. Go to QGIS->Project->Project Properties and replace openProject() with the following Python code:

    def openProject():
        import re, qgis     
        iface = qgis.utils.iface
        layers = iface.mapCanvas().layers()
        for lyr in layers:
            # Get date from layer
            it=lyr.getFeatures()
            feat = next(it)
            idx = lyr.fieldNameIndex('traffic_da')
            currDate = feat.attributes()[idx]
    
            # Set new layer name
            name = lyr.name()
            if re.search(' on \d{2,2}.\d{2,2}.\d{4,4}$', name):
                 name = name[:-14]
            name = name + " on " + currDate
            lyr.setLayerName( name )
    

    enter image description here

    Note 1: I tested it on shapefiles, so the field which I take dates from is traffic_da because Shapefiles do not support more letters in field names. Adjust that in the code to work on your layers.

    Note 2: Depending on your project configuration, you should add validation code to act only on the layers you want. For instance, if you run the code on raster layers, errors will appear for sure. If you need help with this, open a new question, I can help you.

  2. Make sure you enable macros on your project, this way: Settings->Options->General->Enable macros: Always

  3. Load your layers to the project.

  4. Save your project.

Every time you open your project from this point, your layer names will be dynamic, the date will be taken from the field traffic_date on each layer's attribute table.

After the first time I opened the project, this is how my layer names were dynamically generated:

enter image description here

I think this can help you to get started. Tell me if you face non-obvious troubles.

  • I figured it out by reading your comment, posted 1s before I submitted the answer. Some adjustments should be made in the code to fit the specific arrangement of layers, but I hope @mofoyoda can deal with them. – Germán Carrillo Jan 24 '15 at 2:06
  • 1
    Yeah it's a good first go. You could also just find the index of on and strip from there to the end to save on using regex. – Nathan W Jan 24 '15 at 2:09
  • 1
    @gcarrillo intense! But great! – DPSSpatial Jan 25 '15 at 0:38
  • 1
    @gcarrillo Very cool, thanks! Still trying to figure out how to apply to only certain layers, i hope I'll make it. Thanks for a great answer! – mofoyoda Jan 25 '15 at 12:12

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.