[Qgis-developer] Usefull methods

Martin Dobias wonder.sk at gmail.com
Wed Dec 10 14:25:53 EST 2008


On Wed, Dec 10, 2008 at 4:32 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 2008/12/10 vatto <mauricio.dev at gmail.com>:
>> I've written some methods to work with layers' names. I found the usefull and
>> usually being used in every script I make. Shouldn't these stuff be a default
>> in the API? Well, the python code is here. It shouldn't be hard to translate
>> do C++.
>
>  I'd like to make a plugin of widely-useful python functions like this
> for other plugins to use, called 'qutils'. But my worry is that then
> we'd be defining our own API, and people would have to keep upgrading
> their qutils plugin... It might get messy.

It would be much better to put these utility functions/classes
directly to QGIS libs and wrapped in PyQGIS. There are several reasons
for this:
1. no need to install/update more plugins (and btw. with current
implementation it's not possible to interact between plugins)
2. also QGIS C++ code or 3rd party C++ devs could use this functionality
3. slightly faster implementation (not that important)


>> def getLayerByName(self,layerName):
>>    layersmap=QgsMapLayerRegistry.instance().mapLayers()
>>    for (name,layer) in layersmap.iteritems():
>>        if (layerName==layer.name()):
>>            return layer
>
>  Qgis layer names aren't unique, so your code will get the first layer
> with that name. I think (someone prove me wrong) the only unique way
> of identifying a layer in Qgis is with the layer object itself. That
> does mean that drop-down selection boxes can't rely on looking up text
> names to find the right layer, and need to store the layer object

Barry is right, there might be more layers with the same name. You can
also simply change layer's name with QgsMapLayer::setLayerName
function. But another unique way of identifying layers (apart from
object itself) is by layer ID - returned by QgsMapLayer::getLayerID()
- these text strings serve as identifiers also in map layer registry.


> But yes, I agree we should try and sort out some common python
> functionality into a plugin!

As I've stated above, I would be happier if such common functionality
would be in libs...
May I suggest you to put ideas for new functionality e.g. to wiki?

Bye
Martin


More information about the Qgis-developer mailing list