0

I am developing a plugin for the GIS software, QGIS 2.14.3. I am also using Qt Designer 4.8.5.

I have several checkboxes which, when individually checked, executes their associated function. This selects polygon features on a GIS map and prints the sum of their area:

Image

There's 5 ranks in total so 5 check boxes, the function for each are practically the same but here are the first 2:

selectedLayerIndex = self.dockwidget.combo_box.currentText()
sel_layer = QgsMapLayerRegistry.instance().mapLayersByName(str(selectedLayerIndex))[0]   
self.iface.setActiveLayer(sel_layer) 

def rank_0():
    expr = QgsExpression( "\"Rank\"IS NULL" )
    it = sel_layer.getFeatures( QgsFeatureRequest( expr ) )
    ids = [i.id() for i in it]
    if self.dockwidget.rank0_checkbox.isChecked():
        sel_layer.setSelectedFeatures( ids )
        for f in sel_layer.selectedFeatures():
            sel_area = 0
            sel_area += f.geometry().area()
            self.dockwidget.lineEdit.setText("{:,.2f}".format(sel_area))
    else:
        sel_layer.removeSelection()

def rank_1():
    expr = QgsExpression( "\"Rank\"= 1" )
    it = sel_layer.getFeatures( QgsFeatureRequest( expr ) )
    ids = [i.id() for i in it]
    if self.dockwidget.rank1_checkbox.isChecked():
        sel_layer.setSelectedFeatures( ids )
        for f in sel_layer.selectedFeatures():
            sel_area = 0
            sel_area += f.geometry().area()
            self.dockwidget.lineEdit.setText("{:,.2f}".format(sel_area))
    else:
        sel_layer.removeSelection()

How could I make it so that when multiple checkboxes are checked, the printed area is the sum from those checkboxes?

My guess is I would need to define another function to calculate the sum of the area and print it off but not sure how to proceed.

Joseph
  • 586
  • 1
  • 13
  • 32
  • I am also calling `sel_area = 0` in each checkbox function which probably resets the area and then prints the area of the newly selected features? – Joseph Jun 09 '16 at 12:36

1 Answers1

2

Instead of setSelectedFeatures(ids) function you can use select(ids) function. While setSelectedFeatures(ids) deselectes the previously selected features and selects the new features, select(ids) function adds the new ids to the selected ids.

    sel_layer.select( ids )

By calculating the total area of the selected features you get your desired value.

    sel_area = 0
    for f in sel_layer.selectedFeatures():
        sel_area += f.geometry().area()
    self.dockwidget.lineEdit.setText("{:,.2f}".format(sel_area))
okorkut
  • 491
  • 2
  • 12
  • Thanks for your answer, I will test this out and report back :) – Joseph Jun 09 '16 at 11:47
  • Unfortunately, the total area still does not accumulate when clicking on multiple checkboxes. – Joseph Jun 09 '16 at 12:27
  • I think you are not calculating it right. I just realized that you are setting your total area to zero with every selected feature of your loop. Let me edit my answer. – okorkut Jun 09 '16 at 12:42
  • Yes, I realised that before I posted a comment to my question. Many thanks, it works! Next step for me is to see how to subtract the area when unchecking a checkbox. Will give this a go first before posting any more questions. Thanks again =) – Joseph Jun 09 '16 at 12:56