geodatafarm.support_scripts package

This is some script that didn’t fit in any other places, the scripts supports the other scripts

Module contents

geodatafarm.support_scripts.check_date_format(sample, column, format_)

Checks that the date format matches the selected format

Parameters:
  • sample (list) – the sample of the data including a heading row
  • column (str) – the column in the heading row containing the date
  • format (str) – the format of the date
Returns:

That tells if the sample had the correct format

Return type:

bool

geodatafarm.support_scripts.check_text(text)

Checks that the text only contains ascii letters and numbers

Parameters:text (str) –
Returns:A text string with only ascii letters and numbers
Return type:str
geodatafarm.support_scripts.isfloat(x)

Checks if the inserted value is of float type

Parameters:x (str) –
Returns:
Return type:bool
geodatafarm.support_scripts.isint(x)

Checks if the inserted value is of int type

Parameters:x (str) –
Returns:
Return type:bool

Submodules

geodatafarm.support_scripts.add_field module

class geodatafarm.support_scripts.add_field.AddField(parent_widget)

Bases: object

finish(result, values)

Produces either an error message telling what went wrong or adds the fields to canvas and zoom to the layers.

Parameters:
  • result (object) –
  • values (list) –
    If all went ok:
    [True, list of layers]
    Else:
    [False, exception, traceback]
help()

A function that gives some advice on how the function works for the user.

quit()

Closes the widget.

remove_field()

Removes a field that the user wants, a check that there are no data that is depended on is made.

run()

Presents the sub widget AddField and connects the different buttons to their function

save()

Saves the field in the database

set_widget_connections()

Function that sets the main widget connections.

view_fields()

Add all fields that aren’t displayed on the canvas, if no background map is loaded Google maps are loaded.

geodatafarm.support_scripts.add_field.add_fields_2_canvas(task, db, fields_db, defined_field, sources)

A function that adds fields that are not added previously.

Parameters:
  • task (QgsTask) – a QgsTask to run the function in
  • db (DB) – A database connection
  • fields_db (list) – list of the names of all fields in the database
  • defined_field (str) – the last added field name
  • sources (list) – list of source names
Returns:

If all went ok:

[True, list of layers]

Else:

[False, exception, traceback]

Return type:

list

geodatafarm.support_scripts.add_layer_to_canvas module

class geodatafarm.support_scripts.add_layer_to_canvas.AddLayerToCanvas(parent)

Bases: object

A class that adds layers to the canvas

add_2_canvas(parameter)

Adds the parameter to the canvas :param parameter: A dict containing index_col, tbl_name, schema :type parameter: dict

add_selected()

All checked items in the ListWidget is added to the canvas. Afterwards the button becomes disconnected and this widget closes

get_tables()

Fills the dict ‘parameters’ with an int as key and a dict as the value The value dict has following args: index_col, schema, and tbl_name

populate_widget()

For all selected data sets in the GeoDataFarm widget adds all column names to a ListWidget except if there is only one, then it is directly added to the canvas.

run()

Displays the widget and connects the button

geodatafarm.support_scripts.create_guiding_file module

class geodatafarm.support_scripts.create_guiding_file.CreateGuideFile(parent_widget)

Bases: object

add_to_param_list()

Adds the selected columns to the list of fields that should be treated as “special” in the database both to work as a parameter that could be evaluated and as a layer that is added to the canvas

create_file()

Creates the guide file with the information from the user.

fill_cb()

Updates the ComboBox with names from the differnt schemas in the database

help()

Shows a help message in a QMessageBox

possible_attr(text)

Adds the name of the table which the user than can use as base for calculation of the guiding file.

Parameters:text (str) – The schema.table
remove_from_param_list()

Removes the selected columns from the list of fields that should be treated as “special” in the database

run()

Presents the sub widget HandleInput and connects the different buttons to their function

set_output_path()

Sets the path where the guide file should be saved.

update_max_min()

Update the text min, max text and set the equation for the guide file.

geodatafarm.support_scripts.create_layer module

class geodatafarm.support_scripts.create_layer.CreateLayer(db, dock_widget=None)

Bases: object

create_layer_style(layer, target_field, tbl_name, schema, min=None, max=None, steps=20)

Create the layer and adds the layer to the canvas

Parameters:
  • layer (QgsVectorLayer) –
  • target_field (str) –
  • tbl_name (str) –
  • schema (str) –
  • min (float, optional) –
  • max (float, optional) –
  • steps (int, optional default 20) –
equal_count(layer, data_values_list, field, steps=10, min_value=None, max_value=None)
Parameters:
  • layer
  • data_values_list
  • field
  • steps
  • min_value
  • max_value
repaint_layer()

Applies the new min and max and repaints the layer with new colors

geodatafarm.support_scripts.create_layer.add_background()

Check if there are no other tiles present on the canvas then adds a google satellite as a background map.

geodatafarm.support_scripts.create_layer.hist_edges_equal(x, nbin)
Histogram with equal number of points in each bin
https://stackoverflow.com/questions/39418380/histogram-with-equal-number-of-points-in-each-bin
Parameters:
  • x (list) – list of all values to put in the histogram
  • nbin (int) – Number of bins to use in the histogram
Returns:

Returns the second parameter (bins) in plt.hist

Return type:

bins

geodatafarm.support_scripts.create_layer.set_label(layer, field_label)

Function that sets the label to a field value. Inspiration found at: https://gis.stackexchange.com/questions/277106/loading-labels-from-python-script-in-qgis

Parameters:
  • layer (QgsVectorLayer) – valid qgis layer.
  • field_label (str) – The label of the field
geodatafarm.support_scripts.create_layer.set_zoom(iface, extra_extent)

Sets the zoom level to include all layers (excluding tiles layer) with some extra extent

Parameters:
  • iface (QGIS interface) – The QGIS iface module.
  • extra_extent (float) – How much extra space around the layers eg. 1.1 is 10% extra

geodatafarm.support_scripts.generate_reports module

class geodatafarm.support_scripts.generate_reports.MyDocTemplate(filename, tr, plugin_dir, growing_year, cur_date, **kw)

Bases: reportlab.platypus.doctemplate.BaseDocTemplate

header(canvas, doc, growing_year, cur_date)

Create the header of the document

Parameters:
  • canvas
  • doc
  • growing_year (int) –
  • cur_date (str, with the current date) –
class geodatafarm.support_scripts.generate_reports.RapportGen(parent)

Bases: object

collect_data(task, year)

Collect data from the different schemas at the server and store them in a dict

Parameters:
  • task (QgsTask) –
  • year (int) –
Returns:

if success:

[bool, dict]

else:

[bool, str, str]

Return type:

list

report_per_field()

Creates a QgsTask in order to collect data then on finish it runs simple_field.

report_per_operation()

Creates a QgsTask in order to collect data then on finish it runs simple_operation.

select_folder()

A function that lets the user select the folder for the generated reports. The self.path will be updated with this function.

set_widget_connections()

A simple function that sets the buttons on the report tab

simple_field(result, values)

Generates a simple report of all operations listed by fields.

Parameters:
  • result (QgsTask.result) – Not used
  • values (list) –
    if success:
    [True, dict]
    else:
    [False, message, tracback]
simple_operation(result, values)

Generates a simple report of all operations

Parameters:
  • result (QgsTask.result) – Not used
  • values (list) –
    if success:
    [True, dict]
    else:
    [False, message, tracback]

geodatafarm.support_scripts.multiedit module

class geodatafarm.support_scripts.multiedit.MultiEdit(parent)

Bases: object

This class enable the user to edit the attributes in a simple way, it is almost a copy of QuickMultiAttributeEdit, so all cred to them for it! https://github.com/lucadelu/QuickMultiAttributeEdit

do_checks()

Checks that all is ready to be updated

run()

Change the values, if it is a postgres database source it does the update directly to the server and then reloads the layer.

show()

Displays the widget

geodatafarm.support_scripts.multiedit.bool2str(b_var)

Converts a str to bool

Parameters:b_var (bool) –
Returns:
Return type:str
geodatafarm.support_scripts.multiedit.rm_if_too_old_settings_file(my_path_and_file)

Removes the settings file if it is too old.

geodatafarm.support_scripts.multiedit.str2bool(b_var)

Converts a str to bool

Parameters:b_var (str) –
Returns:
Return type:bool

geodatafarm.support_scripts.populate_lists module

class geodatafarm.support_scripts.populate_lists.Populate(parent)

Bases: object

A class to set/reload list widgets and comboBoxes

get_items_in_table()

Returns the list of list ‘items in table’ :returns: :rtype: list

get_lw_list()

Function returns the list of lists with [[ListWidget, ‘name’]] :returns: :rtype: list

refresh(db)

Refreshes database connection in this Populate class :param db: :type db: database class

reload_crops(cmd_box=None)

Reloads all crops comboBoxes in the GeoDataFarm widget :param cmd_box: a comboBox to fill with the crop names (used in text_data_handler) :type cmd_box: QtComboBox, optional

reload_fields(cmd_box=None)

Reloads all field comboBoxes in the GeoDataFarm widget :param cmd_box: a comboBox to fill with the field names (used in text_data_handler) :type cmd_box: QtComboBox, optional

update_table_list()

Update the list of tables in the docket widget

geodatafarm.support_scripts.radio_box module

class geodatafarm.support_scripts.radio_box.RadioComboBox

Bases: PyQt5.QtWidgets.QComboBox

check_others(target_row)
handle_item_pressed(index)

geodatafarm.support_scripts.rain_dancer module

class geodatafarm.support_scripts.rain_dancer.MyRainDancer(client='000001', username='Demo', password='Demo')

Bases: object

get_operation_data()

Collects data from crops returns a list of list with guid and names for the crops

Returns:
  • dict
  • or str = Failed

geodatafarm.support_scripts.RG module

geodatafarm.support_scripts.RG.rg(percent)

A colour map with 100 colours, from green(0) to yellow to red(1)

Parameters:percent (float) – Anywhere between 0 and 1
Returns:The RGB code corresponding to the percentage, 0=red, 0.5 yellow and 1=green
Return type:list

geodatafarm.support_scripts.shapefile module

shapefile.py Provides read and write support for ESRI Shapefiles. author: jlawhead<at>geospatialpython.com date: 2015/06/22 version: 1.2.3 Compatible with Python versions 2.4-3.x version changelog: Reader.iterShapeRecords() bugfix for Python 3

class geodatafarm.support_scripts.shapefile.Editor(shapefile=None, shapeType=1, autoBalance=1)

Bases: geodatafarm.support_scripts.shapefile.Writer

balance()

Adds a corresponding empty attribute or null geometry record depending on which type of record was created to make sure all three files are in synch.

delete(shape=None, part=None, point=None)

Deletes the specified part of any shape by specifying a shape number, part number, or point number.

point(x=None, y=None, z=None, m=None, shape=None, part=None, point=None, addr=None)

Creates/updates a point shape. The arguments allows you to update a specific point by shape, part, point of any shape type.

select(expr)

Select one or more shapes (to be implemented)

validate()

An optional method to try and validate the shapefile as much as possible before writing it (not implemented).

class geodatafarm.support_scripts.shapefile.Reader(*args, **kwargs)

Bases: object

Reads the three files of a shapefile as a unit or separately. If one of the three files (.shp, .shx, .dbf) is missing no exception is thrown until you try to call a method that depends on that particular file. The .shx index file is used if available for efficiency but is not required to read the geometry from the .shp file. The “shapefile” argument in the constructor is the name of the file you want to open.

You can instantiate a Reader without specifying a shapefile and then specify one later with the load() method.

Only the shapefile headers are read upon loading. Content within each file is only accessed when required and as efficiently as possible. Shapefiles are usually not large but they can be.

close()
iterRecords()

Serves up records in a dbf file as an iterator. Useful for large shapefiles or dbf files.

iterShapeRecords()

Returns a generator of combination geometry/attribute records for all records in a shapefile.

iterShapes()

Serves up shapes in a shapefile as an iterator. Useful for handling large shapefiles.

load(shapefile=None)

Opens a shapefile from a filename or file-like object. Normally this method would be called by the constructor with the file object or file name as an argument.

record(i=0)

Returns a specific dbf record based on the supplied index.

records()

Returns all records in a dbf file.

shape(i=0)

Returns a shape object for a shape in the the geometry record file.

shapeRecord(i=0)

Returns a combination geometry and attribute record for the supplied record index.

shapeRecords()

Returns a list of combination geometry/attribute records for all records in a shapefile.

shapes()

Returns all shapes in a shapefile.

exception geodatafarm.support_scripts.shapefile.ShapefileException

Bases: Exception

An exception to handle shapefile specific problems.

class geodatafarm.support_scripts.shapefile.Writer(shapeType=None)

Bases: object

Provides write support for ESRI Shapefiles.

bbox()

Returns the current bounding box for the shapefile which is the lower-left and upper-right corners. It does not contain the elevation or measure extremes.

close()
field(name, fieldType='C', size='50', decimal=0)

Adds a dbf field descriptor to the shapefile.

line(parts=[], shapeType=3)

Creates a line shape. This method is just a convienience method which wraps ‘poly()’.

mbox()

Returns the current m extremes for the shapefile.

null()

Creates a null shape.

point(x, y, z=0, m=0)

Creates a point shape.

poly(parts=[], shapeType=5, partTypes=[])

Creates a shape that has multiple collections of points (parts) including lines, polygons, and even multipoint shapes. If no shape type is specified it defaults to ‘polygon’. If no part types are specified (which they normally won’t be) then all parts default to the shape type.

record(*recordList, **recordDict)

Creates a dbf attribute record. You can submit either a sequence of field values or keyword arguments of field names and values. Before adding records you must add fields for the record values using the fields() method. If the record values exceed the number of fields the extra ones won’t be added. In the case of using keyword arguments to specify field/value pairs only fields matching the already registered fields will be added.

save(target=None, shp=None, shx=None, dbf=None)

Save the shapefile data to three files or three file-like objects. SHP and DBF files can also be written exclusively using saveShp, saveShx, and saveDbf respectively. If target is specified but not shp,shx, or dbf then the target path and file name are used. If no options or specified, a unique base file name is generated to save the files and the base file name is returned as a string.

saveDbf(target)

Save a dbf file.

saveShp(target)

Save an shp file.

saveShx(target)

Save an shx file.

shape(i)
shapes()

Return the current list of shapes.

zbox()

Returns the current z extremes for the shapefile.

geodatafarm.support_scripts.shapefile.b(v)
geodatafarm.support_scripts.shapefile.is_string(v)
geodatafarm.support_scripts.shapefile.signed_area(coords)

Return the signed area enclosed by a ring using the linear time algorithm at http://www.cgafaq.info/wiki/Polygon_Area. A value >= 0 indicates a counter-clockwise oriented ring.

geodatafarm.support_scripts.shapefile.test()
geodatafarm.support_scripts.shapefile.u(v)