[Qgis-developer] Memory provider

Martin Dobias wonder.sk at gmail.com
Sun Jun 8 14:13:00 EDT 2008


Hi devs,

in trunk, revision 8626 I've added a new "memory" provider. It's
intended to be used mainly by plugin or 3rd party app developers. It
doesn't store data on disk, allowing developers to use it as a fast
backend for some temporary layers (until now one had to use e.g. OGR
provider). It's fully functional now and you're invited to test it.

Now, how to use it? Just create a new layer, add fields with their
appropriate types and you can start adding features.
The two important things are:
- URI for provider - depending on type of layer it's going to be:
"Point" / "LineString" / "Polygon" / "MultiPoint" / "MultiLineString"
/ "MultiPolygon"
- field types: "string" / "int" / "double" - when calling
addAttributes() function
I'll demonstrate it with following example - will be put to wiki once
writing will be enabled again.

8<-----

# create layer
vl = QgsVectorLayer("Point", "", "memory")
pr = vl.getDataProvider()

# add fields
pr.addAttributes( { "name" : "string", "age" : "int", "size" : "double" } )

# add a feature
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
fet.setAttributeMap( { 0 : QVariant("Johny"), 1 : QVariant(20), 2 :
QVariant(0.3) } )
pr.addFeatures( [ fet ] )

# show some stats
print "fields:", pr.fieldCount()
print "features:", pr.featureCount()
e = pr.extent()
print "extent:", e.xMin(),e.yMin(),e.xMax(),e.yMax()

# iterate over features
f = QgsFeature()
pr.select()
while pr.getNextFeature(f):
       print "F:",f.featureId(), f.attributeMap(), f.geometry().asPoint()

8<-----

Enjoy!

Martin


More information about the Qgis-developer mailing list