7

I recently loaded a big point set (in terms of memory usage) in the QGIS project i'm working on. This layer is necessary for the project but it is not used often. Most of the time is turned off.

The problem is that each time that I start the project, this layer is loaded, taking a long time for the project to start and making the project very slow.

Is there a way for not loading this layer into memory unless it is turned on?

I'm using version 2.12.3 but i guess it applies to older versions as well.

  • Does the same happen when you include the point dataset into a VRT? For rasters they are then only loaded when the layer is activated - I am not sure if the behaviour is the same for large point datasets. – Kersten Feb 1 '16 at 16:42
  • I haven't tried. How should i do that? – Ariel Loncarich Feb 1 '16 at 16:59
  • What file format is the layer? – ndawson Feb 1 '16 at 20:26
  • Origiinally text, later converted to .shp – Ariel Loncarich Feb 1 '16 at 21:13
3

A possible workaround would be to add an empty memory layer in your project which acts as a placeholder for your large point dataset. This memory layer is saved, alongside any styles etc. that you typically applied, and as it is empty, will load immediately when you open your project. You can use the following in the Python Console to create a simple point layer:

mem_layer = QgsVectorLayer("Point", "Enter_name", "memory")
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

You can change Enter_name to the name of large point layer, remove the original point layer from your project and save.

Now the next time you load your project, you could enter the following code. The code, which would populate your memory layer with all the attributes and features of the point dataset, will not run immediately as only the function is defined:

from PyQt4.QtCore import *

def load():
    for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        root = QgsProject.instance().layerTreeRoot()
        if layer.name() == "Enter_name":    # Change to match your memory layer name
            node = root.findLayer(layer.id())    # Optional line
            if node.isVisible() == Qt.Checked:   # Optional line
                real_layer = QgsVectorLayer("path/to/point.shp","point","ogr") # Change path to your dataset
                feats = [ feat for feat in real_layer.getFeatures() ]
                layer.startEditing()
                attr = layer.dataProvider().fields().toList()
                layer.dataProvider().addAttributes(attr)
                layer.updateFields()
                layer.dataProvider().addFeatures(feats)
                layer.commitChanges()
            else:
                pass

To run this, check the memory layer and type load() into the console when you are ready to pull the data into the memory layer.


Note: I've added that the script will only run if the memory layer is checked. These lines of code are not necessary and can be removed (remember to match indentation) but were added as I was looking for a more dynamic function but not sure if it is possible.

  • My thanks to the person who included the from PyQt4.QtCore import * as this was indeed a line that is required. – Joseph Feb 3 '16 at 9:49

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.