Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
About the Author
About the Reviewers

Creating a vector layer in memory

Sometimes, you need to create a temporary data set for 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 data set, including the geometry, fields, and attributes, virtually. Once the memory layer is created, you can work with it in the same way you would work with a vector layer loaded from disk.

Getting ready

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

How to do it...

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

  1. Start QGIS.

  2. From the Plugins menu, select Python Console.

  3. In the Python console, create a QgsVectorLayer, including fields, and specify it as a memory data provider:

    vectorLyr =  QgsVectorLayer('Point?crs=epsg:4326&field=city:string(25)&field=population:nt', 'Layer 1' , "memory")
  4. Now, validate...