[Qgis-user] QGIS crashes when loading / unloading raster layers using Python

Hollis, Dan dan.hollis at metoffice.gov.uk
Wed Oct 19 08:36:54 PDT 2016


Hi,

I'm really hoping someone can help me with this.

I'm trying to write a Python script to batch process the loading of rasters into QGIS. This script will be run in the Python Console. The rasters are geotiffs created using some separate software that we have been developing. As far as I can tell there are no problems with the rasters themselves i.e. I can load and unload them using the standard buttons in the QGIS interface without running into problems.

The rasters contain temperature data. There are two rasters for each day - one containing actual values and one containing anomalies from the long-term average. The aim is to load all of the data for one month such that there is a separate group for each day in the Layers Panel (table of contents). In the full version of my script each group contains the two rasters for that day, plus some vector layers overlaid on top (point temperature data, coastline etc). I don't think the vector data are the cause of the problems I'm encountering so I'm ignoring these in the rest of this posting.

I'm working on Windows 7. Until recently I was using QGIS 2.4 Chugiak. My script worked well enough in that it would load all the layers as required and the user could mostly interact with them without generating any errors i.e. they could pan, zoom, identify, switch layers on/off, expand/contract groups. However when the user tried to close down QGIS, or if they tried to remove the layers prior to loading a different set of data, then the software would always crash ("qgis-bin.exe has stopped working", "Crash dumped - minidump written to C:" etc). This only happened when the layers had been loaded using my script.

The obvious conclusion was that there was something wrong / incomplete about how my script loaded the layers. I've recently had QGIS 2.14 Essen installed so I thought I'd take another look and see if I could work out what was wrong but so far I've not had much success. The full version of my script now crashes QGIS as the layers are loaded (a step backwards!). The strangest thing is that it doesn't always fail in the same place - often it's while loading the layers for the first group, but occasionally it loads up 2 or 3 groups before crashing. There are no useful error messages or trace back to go on, only "stopped working" and "minidump" messages. If I load only the vector layers then it works, but as soon as I include the raster layers then things start failing. I've added numerous print statements and done all sorts of refactoring but to no avail.

I've now tried to come at this from a different angle - starting with the simplest possible script and building it up until it fails. The code pasted below shows where I've got to. The various functions reflect the structure of my full script. I _think_ the problem must be something to do with the functions - objects are possibly getting destroyed or corrupted as a result of passing them around - but I just cannot work it out.

NB I started out with the simplest possible code i.e. no functions or loops of any kind - just a sequential series of instructions. This works! I then introduced the add_grid() function and it still worked, then add_layers() and it also still worked. Finally I introduced add_groups() - it still loaded the layers without crashing, but then I was back to the situation where QGIS would crash when I remove the layers added by the script. This is different to my full script (which crashes as the layers are loaded) despite one being a simplified version of the other. I then reverted the last change (getting rid of add_groups()) and it's crashing again i.e. what was working is no longer working! Aaaaggghhh!!

If anyone can shed any light at all on this I would be very grateful. I have spent many hours trying to unpick this without success. The most frustrating thing is the lack of any useful error messages and the fact that the crashes seem somewhat random (clearly connected with my script, but not entirely reproducible).

Thanks,

Dan


import os

def add_grid(layer_path, layer_name, grp):
    rlayer = QgsRasterLayer(layer_path, layer_name)
    QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
    grp.addLayer(rlayer)

def add_layers(grp, data_folder, date, short_name):

    # Add first layer
    type = 'anomaly'

    fn = '_'.join((date, short_name, type)) + '.tiff'
    layer_path = os.path.join(data_folder, fn)
    layer_name = ' '.join((short_name, type))

    add_grid(layer_path, layer_name, grp)

    # Add second layer
    type = 'actual'

    fn = '_'.join((date, short_name, type)) + '.tiff'
    layer_path = os.path.join(data_folder, fn)
    layer_name = ' '.join((short_name, type))

    add_grid(layer_path, layer_name, grp)

def add_groups(data_folder, dates, short_name):
    root = QgsProject.instance().layerTreeRoot()
    for date in dates:
        group_name = date + ' ' + short_name
        grp = root.addGroup(group_name)
        add_layers(grp, data_folder, date, short_name)


data_folder = r'C:\path\to\my\data'
short_name = 'daily_mintemp'
dates = ['2016-08-01', '2016-08-02']

add_groups(data_folder, dates, short_name)




More information about the Qgis-user mailing list