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:
- Start QGIS.
- From the Plugins menu select Python Console.
- In the Python console, create a
QgsVectorLayer, including fields, and specify it as type memory:vectorLyr = QgsVectorLayer('Point?crs=epsg:4326 &field=city:string(25) ...
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime