[Qgis-developer] QgsMapLayerRegistry (and QgsMapCanvas) problem

Martin Dobias wonder.sk at gmail.com
Sat Oct 18 06:57:35 EDT 2008


Hi Carson,

On Fri, Oct 17, 2008 at 4:39 PM, Carson Farmer <carson.farmer at gmail.com> wrote:
> Hello gang,
>
> According to the API, QgsMapLayerRegistry.instance().mapLayers() should
> return (in the case of python) a dict with the following format:
> { layer0 : QgsMapLayer, layer1: QgsMapLayer }
> However, what I appear to be getting is slightly different. Instead of
> QgsMapLayers, I get QgsVectorLayers and QgsRasterLayers. This also occurs
> when trying to get a QgsMapLayer from the map canvas. Is this intended, and

this is correct behaviour. Python doesn't support casting in a way
that c++ does. I.e. one you have a QgsMapLayer reference, you can't
cast it to QgsVectorLayer (although it would be a correct cast). So
SIP has a mechanism (which PyQGIS uses) that returns a subclass
instead of parent class. You can take a look at qgsmaplayer.sip in
python/core dir to get an idea.

> if so then this creates a problem:
>
> QgsMapLayer.type() returns (as of 9490-ish) either VectorLayer (0) or
> RasterLayer (1) [ QgsMapLayer.LayerType ]
> QgsVectorLayer,type() returns either Point (0), Line (1), Polygon (2), or
> UnknownGeometry (3) [ QGis.geometryType ]
> QgsRasterLayer.type() returns (as far as I can tell) the same as
> QgsMapLayer.type(), so: RasterLayer (1)
>
> [...]
>
> I see three solutions to this problem:
> a) I'm doing this all wrong, and hopefully someone will tell me this,
> b) My favoured solution: Since QgsVectorLayer inherits QgsMapLayer,
> QgsVectorLayer.type() should be changed to QgsVectorLayer.vectorType()
> c) All of these enumerators should return something other than numbers?

b) is correct! type() should be really renamed back to vectorType()
because current behaviour is not acceptable.

Bye
Martin


More information about the Qgis-developer mailing list