[Qgis-user] pyqgis (beginners' question)

aline Hansegard aline.hansegard at gmail.com
Mon Oct 14 19:20:47 PDT 2013


Hi,

I'm trying to write my first plugin with pyqgis. I usually do my statistics
analysis with R (or matlab), but I would like to speed up things with a
custom plugin. Who knows it could be useful in the future for others.

I tried R in Sextante, but I think a separate plugin would be more useful.

ok, I created my plugin structure with pluginbuilder and started to
customize things.

To keep it simple, in my ui file, I created a qcombobox to select a layer,
and a qlistwidget to select one or more than one fields.

In my py file the code is something like this (most of this is adapted from
other people's code :-) )...

-----
    def chooselayer1(self):
        self.dlg.ui.chosenlayer1.clear()
        for name, layer in
QgsMapLayerRegistry.instance().mapLayers().iteritems():
            if layer.type() == QgsMapLayer.VectorLayer:
                self.dlg.ui.chosenlayer1.addItem(layer.name())
                self.set_select_attributes()

    def set_select_attributes(self):
        self.dlg.ui.Column.clear()

    if self.dlg.ui.chosenlayer1.currentText() != "":
            layername1 = self.dlg.ui.chosenlayer1.currentText()

            for name, selectlayer in
QgsMapLayerRegistry.instance().mapLayers().iteritems():
                if selectlayer.name() == layername1:
                    for field in selectlayer.dataProvider().fields():
                        self.dlg.ui.Column1.addItem(field.name())
-----

Question 1: I want to include a button to calculate for example relative
frequencies for a field.

In R I would something like:  data$fx <- data$x / sum(data$x) ###this would
add a column to object data

In pyqgis, I would need to get for example the selected field.
Would it be something like: selectlayer.pendingFields() / "sum of x which I
have no idea how to calculate".

Question 2: create a button to sum a selection of fields: again, in R:
data$s <- data$var1 + data$var2.

I know this are very beginners' questions. I've been studying the material
in pyqgis cookbook and qgis api. It's very useful, yet I'm still struggling.

Another detail which makes it much harder for beginners are the changes in
api 2.0. For all beginners out there it makes it quite hard to google for
help. But I guess there's no way around it.

Thank you for your help,
Aline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20131015/6795c61a/attachment.html>


More information about the Qgis-user mailing list