2

I have been trying to make a local addition to the python code for QGIS plugin Midvatten to allow labels on the stratigraphy plot that give the capacity explanation rather than the capacity value. In my case these capacity values are just meaningless codes but the explanation is the information that I wish display about the plot. The screenshot shows the type of plot with capacity value label and attribute tables that I am using with explanation field.

enter image description here

I found the file stratigraphy.py in .qgis2>plugins>midvatten>tools and have added the following lines of code:

                elif self.GeoOrComment == "explanation":
                   p.drawText(tRect, PyQt4.QtCore.Qt.AlignVCenter, '' if layer.hydro=='NULL' else self.digitToText(layer.hydro, 'explanation'))

at line 469

    def digitToText(self, id=''):    # A function to return 'explanation' for 'capacity' value
       """ returns explanation for the specified capacity """
    if id in self.hydroColors:
        return getattr(PyQt4.QtCore.Qt, self.hydroColors[id][2])   # Or possibly [0]?
    else:
        return PyQt4.QtCore.Qt.nolabel

at line 509

        self.GeologyOrCommentCBox.addItem('explanation')

at line 572

This does not produce any syntax errors and adds explanation to label menu but interferes with the content of the stratigraphy plot content as shown in the screenshot:

enter image description here

I am now trying to figure out what I need to change. In .qgis2>plugins>midvatten>definitions>midvatten_defs.py I found def hydrocolors(): which has the line of code:

    res, dict_qt1 = utils.get_sql_result_as_dict('select a.capacity, a.explanation, b.color_qt from zz_capacity a, zz_capacity_plots b where a.capacity = b.capacity')

This suggests to me that I am looking in the right dictionary to find the explanation label. In the getattr statement I have tried changing the number in [ ] between 0,1 and two to see if this has any effect but it doesn't.

What am I missing?

2

The issue seem to have been a hidden/silent exception that happend when layer.hydro was None. A test for None solved it for me:

elif self.GeoOrComment == "hydro_expl":
    if layer.hydro is None or layer.hydro=='NULL':
        hydr = ''
    else:
        hydr = self.hydroColors.get(layer.hydro, '')[0]
    p.drawText(tRect, PyQt4.QtCore.Qt.AlignVCenter, hydr)

Edit: I've added your suggestion to the code! Many thanks for the idea!

  • Which was about the same thing you did. Hmm. – HenrikSp Feb 22 '18 at 13:36
  • Tested your fix on my system and it works on my data set. Your hydroColors.get is a lot simpler and more elegant than the 'def digiToText()' that I wrote and I think returning 'nolabel' at the end may be wrong. Looking forward to this being in the official code, thanks for including it. – S. OConnor Feb 23 '18 at 17:22
2

Some kind of (sqlite) join between "stratigraphy" and "zz_capacity" will probably be needed. Perhaps as a nested sqlite clause in the "_getDataStep2" method of the "Surveystore" class.

But I have not been looking into these parts of the code for years so I may be wrong here.

Also, please notice that there are related enhancement issues reported on github:

github.com/jkall/qgis-midvatten-plugin/issues/34

github.com/jkall/qgis-midvatten-plugin/issues/81

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.