Development guide - jkall/qgis-midvatten-plugin GitHub Wiki

Development guide

This is a short guide for the development of the Midvatten plugin.

  • Completely new functions/plots should be new modules under midvatten/tools/.
  • The module midvatten_plugin.py loads the plugin and creates the Midvatten meny and plugin icons. This is where you create actions that starts your new feature ("click menu item> plot function/class starts"). There are many example in this file which you could copy-paste and alter with new names.
  • If you want a gui for making settings for your plots or features, using Qt-5 Designer is helpful (drag widgets to the dialog, then right click and Lay out > ....
  • The Midvatten plugin has been written over a long period of time and by several contributors, so the coding style varies a lot. Both the format and how things are done (list of lists, dicts, numpy arrays, pandas dataframes and more).
  • I try to keep the code format close to PEP8, except for allowing lines longer than 80 chars. I also try the make the code speak for itself by having descriptive variables, functions, classes and methods instead of using an abundance of comments. Since Pandas is easy to install with QGIS, I try to use that package more and more for time series.
  • There are lots of tests under midvatten/tools/tests and most of them follows a specific structure to make them work with both SQLite and Postgresql databases. Writing the tests, however, are often more complicated then writing the features themselves. It's good to have at least one test for each feature to make sure the feature at least starts correctly.
  • To make changes, clone the repository to your own git, make changes and create pull requests to this repository.
  • I usually use the branch qgis3_henrik for general developement and sometimes separate branches for specific features. The master branch is the latest stable bransch, but could lack features compared to qgis3_henrik. It's probably best if you base your changes on the qgis3_henrik branch and make a new bransch with your new features.
  • To access the midvatten database and and extract data, the easiest way is to use the premade functions/classes from midvatten/tools/db_utils.py:

Example 1, for sparse queries. Connect to database, get data and close connection in one function:

from midvatten.tools.utils.db_utils import sql_load_fr_db
query_result = sql_load_fr_db('''SELECT obsid, date_time, level_masl FROM w_levels''')[1]

Example 2, for many queries without connecting and disconnecting from the database after each statement:

from midvatten.tools.utils.db_utils import DbConnectionManager
dbconnection = db_utils.DbConnectionManager()
query_result = dbconnection.execute_and_fetchall('''SELECT obsid, date_time, level_masl FROM w_levels''')
dbconnection.closedb()

Then you can do what you want with the data in query_result, for example plotting using matplotlib.

/HenrikSpa