fairdm.utils.location.resources#

The location as four columns in tabular import and export.

Module Contents#

Classes#

MeasureWidget

A number cell that exports as the number itself, never localised, and empty for none.

CoordinateField

The latitude or the longitude of a record’s point, as a number.

LocatedResourceMixin

Give a resource of a model with a location the four location columns.

Functions#

location_fields

Build the four columns a location stands for in a resource.

API#

class MeasureWidget#

Bases: import_export.widgets.FloatWidget

A number cell that exports as the number itself, never localised, and empty for none.

Initialization

:param coerce_to_string: If True, :meth:~import_export.widgets.Widget.render will return a string representation of the value, otherwise the value is returned.

clean(value, row=None, **kwargs)#

Read one cell as a number.

Args: value: The cell. row: The row the cell is in. **kwargs: Passed through by import-export.

Returns: The number, or None for an empty cell.

Raises: ValueError: The cell is not a number, or is nan or infinite.

render(value, obj=None, **kwargs)#

Return the number, or an empty cell when the record has none.

class CoordinateField(axis)#

Bases: import_export.fields.Field

The latitude or the longitude of a record’s point, as a number.

Args: axis: "lat" or "lon".

Initialization

get_value(instance)#

Return the record’s own latitude or longitude, or None when it has no point.

location_fields()#

Build the four columns a location stands for in a resource.

Returns: A dict of field name to Field, in column order: latitude, longitude, location_elevation and location_uncertainty.

class LocatedResourceMixin(*args, **kwargs)#

Give a resource of a model with a location the four location columns.

Where location is among the resource’s fields, it is replaced by latitude, longitude, location_elevation and location_uncertainty. Export writes the record’s own values. Import reads the columns with the same rules as the location input and reports a row it refuses as invalid, so the row is not saved. A file without the columns leaves each record’s location as it was, and a file with only some of them changes only those.

Initialization

expand_order(order)#

Put the four columns where location stood in an ordering.

Args: order: Field names.

Returns: The names, with location replaced by its four columns once each.

get_export_order()#

Return the export order with the location columns in place of location.

get_import_order()#

Return the import order with the location columns in place of location.

import_instance(instance, row, **kwargs)#

Import the row’s fields, then set the point from its coordinate columns.

Args: instance: The record being imported. row: The row. **kwargs: Passed to the resource.

Raises: ValidationError: The columns of any field, or the location as a whole, are refused.

set_location(instance, row)#

Set the record’s point from the row’s coordinates, by the input’s rules.

The cells are read as numbers by the same field the location input uses.

Args: instance: The record being imported, whose elevation and uncertainty are already set from their columns. row: The row.

Raises: ValidationError: A coordinate is not a number, is out of range or is given without the other, or an elevation or uncertainty has no coordinates or the uncertainty is negative.