main module

This script can be used to run a Streamlit web app that displays time series of Radar backscatter and NDVI values from a database. It was developed to work with a specific database from the Radar Crop Monitor project.

Authors: Markus Adam, Laura Walder | Date created: 13/10/2021 | Date last modified: 27/02/2022 | Python version: 3.8 |

main.db_connect(db_path)

Tries connecting to database & gets dataframe of all table names (which will be empty if database path is invalid), prints error if connection is unsuccessful.

Parameters

db_path – string with path to database file

Returns

sqlite3.Connection object with connection to database, dataframe with database table names

main.db_path_query()

First checks if permanent database path has been set in set_permanent_db_path(). If yes, it checks if this path is valid and tries connecting to database with db_connect(). If no, it queries path from user in the app and tries connection with the entered path. Path validity is checked by checking path ending (must be “.db”) and table_names (which is empty if path is invalid). After connection with valid path is established, the main web app page/functionality is deployed by executing main_part().

main.display_chart(vv_vh_ndvi, records, chart)

Displays chart in app if the corresponding parameter was selected by user and if data with this parameter is available in the “records” dataframe. Returns warning if parameter was selected but is not available.

Parameters
  • vv_vh_ndvi – string with parameter (VV,VH,NDVI)

  • records – dataframe with data that will be displayed

  • chart – altair.Chart object displaying values with vv_vh_ndvi parameter

Returns

warning if no data is available for selected parameter

main.main_part(db)
Deploys the main page of the app and its functionalities. This mainly includes:
  • setting app title and description

  • getting and displaying available data filter values from database, then getting filter selections from user

  • querying data as dataframe from database, based on filter values selected by user

  • making and displaying charts based on queried dataframe

Parameters

db – sqlite3.Connection object with connection to database

Returns

no return in script, but deploys streamlit app functionalities (filters, charts)

main.make_chart(pol_records, axis_label, domain, selection, color_column, sort, title, stat_button)

Creates scatterplot and trendline diagrams of VV/VH/NDVI values from respective subset of dataframe “records”. X axis of scatterplot shows time, while Y axis shows values. Points in the scatterplot are colored based on user selection.

Two types of trendlines are created based on two different statistical methods: Locally Estimated Scatterplot Smoothing (LOESS) and Rolling Mean (with 10 values per mean). One of these trendlines is added to the scatterplot if selected by user.

Parameters
  • pol_records – subset of dataframe “records” with one polarisation/value (VV,VH,NDVI)

  • axis_label – string with y-axis label

  • domain – numpy.ndarray with boundaries for x-axis (start and end date)

  • selection – altair.selection_multi object with values that will be colored (bound to user selection in chart legend)

  • color_column – dataframe column by which data points are colored in chart

  • sort – list with order of values in chart legend

  • title – string with chart title

  • stat_button – string with name of trendline selected by user

Returns

altair.Chart object displaying either VV/VH/NDVI values (and trend line if selected)

main.placeholders(multiselections)

Adds a string (“placeholder”) to a tuple if its length is 1. This is necessary because tuples with length 1 lead to a syntax error when included in a SQL query.

Parameters

multiselections – tuple with user-selected multiselection filter values

Returns

tuple with filter values and optional placeholder

main.replace_strings(string_list, string_dict)
Uses a dictionary to convert a list of strings in two ways:
  • searching if keys from dictionary are in list and replacing them with corresponding values

  • searching if values from dictionary are in list and replacing them with corresponding keys

Direction of conversion is chosen automatically depending on whether keys or values from dictionary are in list.

Parameters
  • string_list – list of strings

  • string_dict – dictionary where keys and values are strings

Returns

potentially updated list of strings

main.set_permanent_db_path()

Defines a string that can be used to set a permanent path to the database. The default value “Enter path here” leads to path query in the web app. If the default is replaced with valid database path, the path query will be avoided (main app page opens directly).

Returns

string with default or database path