[Qgis-developer] Layer-level metadata

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Feb 3 12:58:07 EST 2009


2009/2/3 Florian El Ahdab <felahdab at gmail.com>:

> Typical example: I want to develop a plugin to implement S52 representation
> of S57 maps. To do that, I must identify the layer type (as in S57 layer
> type). Of course I get the layer type when opening the datasource from OGR
> (the ogrsublayersdialog now displays this name). But once this has been
> done, unless I get down to the QgsOgrProvider object, I have no way to
> retrieve this information simply. Moreover, I can imagine designing my own
> layers that I would like to render as S57 layers of such or such type and
> therefore requiring some additionnal data that OGR wouldn't give me in that
> case...

 You can do this, depending on how much control you have of the layer
loading process...

 Python lets you add attributes to just about any object. So if you
create a new menu option 'Load My Special Layer' which lets the user
choose a shapefile or other data source, you can create a
QgsVectorLayer and then set something on it. Load a vector layer and
then try this in the python console: l = iface.mapCanvas().layer(0)
followed by l.fnord = "hello world". The .fnord attribute is now set,
and other python code can check for presence of this. Any decent
object-oriented programmer would throw up at this point at the way
you've violated their object...

 So you can subclass QgsVectorLayer to keep them happy. You do (something like):

class SpecialLayer(QgsVectorLayer):
   def __init__(self,file):
      QgsVectorLayer.__init__(self,file)
   def setFnord(self,value):
       self.fnord=value

 BUT (and it's a big but) none of this gets saved in project files.
You could write a writeXML method on your layer which could save the
metadata, but when it is read back Qgis will just create a plain
QgsVectorLayer and throw away your extra information.

 It would be nice to have a userMetadata object saved and restored
with layers which would be some key-value structure accessible to
Python and C++. At the moment such a thing doesn't exist.

 A possibility is to save it in the project settings (see QgsProject)
which is separate from the layers. I'm thinking about this for the
thing I'm working on.

Barry


More information about the Qgis-developer mailing list