6

I like the idea of Virtual Layers, but when they are too big and constructed from complex queries, QGIS takes a lot of time to redraw it (it seems like it recalculates the entire layer at every zoom/pan/etc).

My workaround is to export Virtual Layer to GeoPackage format and continue to work with that.

In my case, Virtual Layer is often just an intermediate to be processed with further tools and I do not need it long term. I would rather save it as "Temporary scratch layer", just like many Processing tools do.

Is there a way to do so?

1

3 Answers 3

7

You are absolutely right that Virtual Layers are useful only to a certain extend.

What you can do is to create a Virtual Layer by means of PyQGIS and saving it as a GeoPackage or working with it as a temporary vlayer.

vlayer = QgsVectorLayer("?query=SELECT * FROM 'your_layer'", "vlayer", "virtual")

writer = QgsVectorFileWriter.writeAsVectorFormat(vlayer, r"C:/.../output.gpkg", "GeoPackage")

if writer[0] == QgsVectorFileWriter.NoError:
    print("success!")
else:
    print("error")

References:

3

I usually just run the fix geometries processing tool on any layer to make a temporary copy of it. And try the memory layer saver plugin for persistence.

2

Rather than create the Virtual Layer, you can use the native Execute SQL tool under Vector General in the Processing Toolbox.

enter image description here

(There is also the GDAL Execute SQL tool under Vector Miscellaneous in the Processing Toolbox).

The main advantage of the native Execute SQL tool over the GDAL Execute SQL tool is that the native SQL can use the layers as named in the Layers panel - so the same SQL will work as you may have already created for your Virtual Layers.

If you use the GDAL version, you will probably need to convert your existing SQL to get it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.