database_scripts package

create_new_farm module

class geodatafarm.database_scripts.create_new_farm.CreateFarm(parent_widget, new_farm)

Bases: object

Sends a request to create a database for the farm

Parameters
  • parent_widget (GeoDataFarm) –

  • new_farm (bool,) – True if the user creates new farm, False if user connects to a farm

add_schemas()

Adds schemas to the new database

add_tables(first_year)

Add field, crops and manual tables to the database

connect_to_source()

Connects the plugin to another database

create_new_farm()

Sends a request to the server and asks it to create the database for the farm, when the database is created does the server return a message telling if the farm name or user name is taken else a message that the database has been created.

create_spec_functions()

Generates the function makegrid_2d in the users postgres database.

reset_db_connections()

Resets the database connection

run()

Presents the sub widget CreateFarm

db module

class geodatafarm.database_scripts.db.DB(dock_widget, path=None)

Bases: object

The widget that is connects to the database :param dock_widget: The widget from GeoDataFarm :type dock_widget: dock_widget :param path: The path to plugin main folder :type path: str :param tr: The translation function of GeoDataFarm :type tr: tr

add_postgis_layer(table, geom_col, schema, extra_name='', filter_text='')

Creates a qgis layer from a postgres database table.

Parameters
  • table (str) – The table that should be added

  • geom_col (str) – the geometry column in that table

  • schema (str) – The schema

  • extra_name (str, optional) – for a “pre name” in front of the table

  • filter_text (str, optional) – SQL statement to filter the data

Returns

A QgsVectorLayer ready to be styled.

Return type

QgsVectorLayer

check_table_exists(table_name, schema, ask_replace=True)

Checks if a table already exists, if it exists, the user can choose if it wants to reset the table. Returns False the table doesn’t exists or have been dropped.

Parameters
  • table_name (str) –

  • schema (str) –

  • ask_replace (bool, default True) –

Returns

Return type

bool

create_indexes(tbl_name, params_to_eval, schema, primary_key=True)

Drops is exists and create a gist index and btree indexes for params_to_eval a table

Parameters
  • tbl_name (str) – table name

  • params_to_eval (list) – of text strings with columns to make indexes over

  • schema (str) – schema name

  • primary_key (bool) – default True -> create a primary key over field_row_id

create_table(sql, tbl_name)

Create s table, if table exists the table is dropped.

Parameters
  • sql (str) – With the sql statement to create the table

  • tbl_name (str) – table name

execute_and_return(sql, return_failure=False)

Execute and returns an SQL statement

Parameters
  • sql (str) – text string with the sql statement

  • return_failure (bool) – if the error should be returned instead of showed to the user

Returns

the data requested in the statement

Return type

list of lists

execute_sql(sql, return_failure=False, return_row_count=False)
Parameters
  • sql (str) – text string with the sql statement

  • return_failure (bool, optional default False) – If True returns if failure

get_all_columns(table, schema, exclude="''")

Get all columns of a table

Parameters
  • table (str) –

  • schema (str) –

  • exclude (str, optional, string with comma sep names) –

Returns

Return type

list of lists

get_conn()

A function that checks if the database is created and sets then the database name, user name and password.

Returns

Return type

bool

get_distinct(table, column, schema)

Get distinct values from a schema.table

Parameters
  • table (str) –

  • column (str) –

  • schema (str) –

Returns

[distinct value, count]

Return type

list

get_indexes(tables, schema)

Get indexes of tables in a schema, returns a dict.

Parameters
  • tables (str) – what table(s) to look for. (separated by comma if multiple)

  • schema (str) – schema name

Returns

int as first arg and for each int ‘tbl_name’,’index_name’, ‘index_col’ and ‘schema’

Return type

dict

get_tables_in_db(schema)

Get the tables in schema

Parameters

schema (str) –

Returns

A list of tables in the database

Return type

list

remove_table(tbl_schema_name)

Function that removes a table from the database

Parameters

tbl_schema_name (str) – string with schema.table

reset_row_id(schema, tbl) → list
test_connection()

Tests to open the connection and then closes it again :returns: :rtype: bool

update_row_id(schema, table)

Update the field_row_id :param schema: :type schema: str :param table: :type table: str

exception geodatafarm.database_scripts.db.DBException

Bases: Exception

class geodatafarm.database_scripts.db.NoConnection(tr)

Bases: object

run_failure()
class geodatafarm.database_scripts.db.SomeFailure

Bases: object

display_failure(er)

mean_analyse module

class geodatafarm.database_scripts.mean_analyse.Analyze(parent_widget, tables_to_analyse)

Bases: object

A widget that analyses the data in the database

Parameters
  • parent_widget (GeoDataFarm) – The main class

  • tables_to_analyse (list) – list of list schemas and tables that should be included in the analyse

check_consistency()

Checks that the harvest tables is intersecting some of the input data If the data is an activity does it also check that the year is the same table both with respect to location and time!

default_layout()

Creating the layout, (the UI file only contains the plotting area). This function adds parameters names and default value both in a scroll area bellow and to the right of the drawing area.

fill_dict_tables()

Fills the dict tables

get_initial_distinct_values(parameter_to_eval, tbl, schema)

Calls the database and gets distinct values

Parameters
  • parameter_to_eval (str, What parameter to eval) –

  • tbl (str, In what table is the param located) –

  • schema (str, In what schema) –

Returns

analyse_params{‘distinct_values’:[],’distinct_count’:[]}

Return type

dict

plot_data(result, values)

Plots the data from the sql query.

Parameters
  • result (Unused parameter) –

  • values (list) –

    if success:

    [True, dict]

    else:

    [False, message, tracback]

run()

Starts this widget

update_checked_field(other_parameters, main_investigate_col)

Updates the parameters listed as checked in layout_dict

Parameters
  • other_parameters (dict) –

  • main_investigate_col (dict) –

update_pic()

Collects the data that the user gave as input and starts the QgsTask that runs the SQL query. On finish plot_data is called

update_top_panel(nbr, col)

Updates the top right panel with the data that complies the diagram

Parameters
  • nbr (int, what number to update) –

  • col (str, the name och parameter to update) –

geodatafarm.database_scripts.mean_analyse.sql_query(task, investigating_param, other_parameters, db, min_counts, limiting_polygon)

Function that creates and runs the SQL questions to the database, creates filters and connects the correct tables to each other. Runs one question for each interval.

Parameters
  • task (QgsTask) –

  • investigating_param (dict) –

  • other_parameters (dict) –

  • db (DB) –

  • min_counts (int, the minimum data points that are required.) –

  • limiting_polygon (str, a wkt string with a polygon limiting the search.) –

Returns

if success:

[bool, dict]

else:

[bool, str, str]

Return type

list

plan_ahead module

class geodatafarm.database_scripts.plan_ahead.PlanAhead(parent)

Bases: object

A class that fill the tab plan ahead with content

save_data()

Saves the plan

set_widget_connections()

A simple function that sets the buttons on the plan ahead tab

update_fields()

A function that updates the crops and field table.

update_sum()

Update the yearly summary for current and last year

view_year()

Add a background map to the canvas.

table_managment module

class geodatafarm.database_scripts.table_managment.TableManagement(parent)

Bases: object

A widget that enables the possibility to insert data from a text file into a shapefile

check_multiple()

Checks if multiple table is selected

Returns

  • Bool, False if more than one row is selected

  • str, schema.table_name

duplicate_first_row(org_min, org_max, table, schema='harvest')

Duplicates the original data table and returns the first and last row_id

edit_param_name()

Edit the name of all selected parameters.

edit_tbl_name()

This function pops a question if the user wants to rename the selected tables to. Then it is replaced and the manual table is also updated.

merge_tbls()

Merging two data sets into one.

remove_table_from_db()

Removes the selected tables from the database

retrieve_params()

This function is trigged by “edit” table, and basically fills the left list widget, all attributes that have a index get selected by default.

run()

Connects the push buttons and enable the visibility of the dialog.

save_table()

Updates the attribute indexes that are checked in the list widget

split_rows()

Spilt the harvest data to multiple rows.

update_column_list()
update_table_list()

Update the list of tables in the docket widget