[GIS] Why are the features in the memory layer not displayed

pyqgisqgis

I am trying to create a memory layer, I followed the directions from the page below but no points appeared on my map. First I tried typing each line into the python console and I also create a plugin with the same code and still nothing appeared on the map. At some point while I was typing the code into the console, it asked for the projection, so I specified my typical projection (epsg 2272, a state plane coordinate system)

How to create a new empty vector layer programmatically?

I also reviewed
http://www.qgis.org/pyqgis-cookbook/vector.html#memory-provider

I am experimenting with memory layers because I want to include it into a plugin I am developing.

Best Answer

I needed to add the layer to the interface using:

QgsMapLayerRegistry.instance().addMapLayer(vl)

from PyQt4.QtCore import *

vl = QgsVectorLayer("Point", "temporary_points", "memory")
pr = vl.dataProvider()
vl.startEditing()
pr.addAttributes( [ QgsField("name", QVariant.String), QgsField("age",  QVariant.Int),    QgsField("size", QVariant.Double) ] )

fet = QgsFeature()
fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(1260179,432083)) )
fet.setAttributeMap( { 0 : QVariant("Johny"), 1 : QVariant(20), 2 : QVariant(0.3) } )
pr.addFeatures( [ fet ] )
vl.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(vl)
Related Question