Creating a vector layer in memory

Sometimes you need to create a temporary dataset for a quick output, or as an intermediate step in a more complex operation without the overhead of actually writing a file to disk. PyQGIS employs memory layers that allow you to create a complete vector dataset, including the geometry, fields, and attributes, virtually. Once the memory layer is created, you can work with it the same way you would with a vector layer loaded from the disk.

Getting ready

This recipe runs entirely inside the PyQGIS console, so no preparation or outside resources are required.

How to do it...

We will create a point vector layer with a few fields named Layer 1 and then validate it:

  1. Start QGIS.
  2. From the Plugins menu select Python Console.

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.