I'd like to get geometries from a local file (e.g. GML) displayed as rubber bands (QgsRubberBands) in QGIS. However, I wouldn't like to load the layer into the map because in the real scenario there could be a large number of files to get geometries from.
I've been attempting it with the following code (you can copy it directly to the QGIS Python Console), but the problem is that only the last feature is displayed:
from qgis.core import QgsFeature, QgsVectorLayer
from qgis.gui import QgsRubberBand
def loadGMLAsRubberBands(fileName):
featList = []
rb = QgsRubberBand(qgis.utils.iface.mapCanvas(), True)
vlayer = QgsVectorLayer(fileName, "layer", "ogr")
allAttrs = vlayer.dataProvider().attributeIndexes()
vlayer.select(allAttrs)
f = QgsFeature()
while vlayer.dataProvider().nextFeature(f):
featList.append(f)
rb.addGeometry(featList[-1].geometry(), None)
loadGMLAsRubberBands("/tmp/gml/sample_three_features.gml")
A sample GML file can be found here.
I've already attempted several things with no success, namely to store layers (vlayer), features, geometries and/or rubber bands in a list (as suggested in some QGIS mailing-list threads).
I'm using QGIS 1.8.0-Lisboa (rev. a1255fc), Python v.2.7.2, GDAL/OGR v.1.7.3 on Ubuntu/Linux.
Any help would be appreciated.