lmpy.point

Module containing Point class.

Note: A namedtuple could replace this class for Python 3.7+

Module Contents

Classes

Point

Constructor.

PointCsvReader

Constructor for a Point CSV retriever.

PointCsvWriter

Constructor for writing points to csv file.

PointDwcaReader

Constructor for reading Darwin Core Archives.

PointJsonWriter

Constructor for writing JSON points.

Functions

get_field_process_func(index=None, default=None, vocabulary=None, delimiter=None)

Get a function to process a field for a specimen record.

none_getter(obj)

Return None as a function.

Attributes

CORE_TAG

DEFAULT_META_FILENAME

DEFAULT_SPECIES_TERM

DEFAULT_X_TERM

DEFAULT_Y_TERM

DWCA_OCCURRENCE_PARAMS

FIELD_TAG

FILES_TAG

ID_TAG

LOCATION_TAG

OCCURRENCE_ROW_TYPE

lmpy.point.CORE_TAG = {http://rs.tdwg.org/dwc/text/}core[source]
lmpy.point.DEFAULT_META_FILENAME = meta.xml[source]
lmpy.point.DEFAULT_SPECIES_TERM = scientificName[source]
lmpy.point.DEFAULT_X_TERM = decimalLongitude[source]
lmpy.point.DEFAULT_Y_TERM = decimalLatitude[source]
lmpy.point.DWCA_OCCURRENCE_PARAMS[source]
lmpy.point.FIELD_TAG = {http://rs.tdwg.org/dwc/text/}field[source]
lmpy.point.FILES_TAG = {http://rs.tdwg.org/dwc/text/}files[source]
lmpy.point.ID_TAG = {http://rs.tdwg.org/dwc/text/}id[source]
lmpy.point.LOCATION_TAG = {http://rs.tdwg.org/dwc/text/}location[source]
lmpy.point.OCCURRENCE_ROW_TYPE = http://rs.tdwg.org/dwc/terms/Occurrence[source]
class lmpy.point.Point(species_name, x, y, attributes=None)[source]

Constructor.

Parameters
  • species_name (str) – The species name for this point.

  • x (float) – The value of the x coordinate for this occurrence point.

  • y (float) – The value of the y coordinate for this occurrence point.

  • attributes (dict) – A dictionary of attributes associated with this point.

Raises

ValueError – Raised if the species name is omitted.

__eq__(self, other)[source]

Test if this point equals the other.

Parameters

other (Point) – A different Point object to compare with.

Returns

An indication if the two points are equal for the primary attributes.

Return type

bool

__lt__(self, other)[source]

Test if this point is less than the other.

Parameters

other (Point) – A different Point object to compare with.

Returns

An indication if this point is less than the other for the primary

attributes.

Return type

bool

__repr__(self)[source]

Get a string representation of this Point object.

Returns

A string representation of this Point.

Return type

str

get_attribute(self, attribute_name)[source]

Get an attribute for the point.

Parameters

attribute_name (str) – The attribute to attempt to retrieve.

Returns

The value of the attribute if it exists. None: Returned if the attribute does not exist for the Point.

Return type

object

get_attribute_names(self)[source]

Get the point’s attributes.

Returns

A list of the attributes for the point.

Return type

list of str

set_attribute(self, attribute_name, value)[source]

Set an attribute for the point.

Parameters
  • attribute_name (str) – The name of the attribute to set.

  • value (object) – The value to set the attribute to.

class lmpy.point.PointCsvReader(filename, species_field, x_field, y_field, geopoint=None, group_field='species_name')[source]

Constructor for a Point CSV retriever.

Parameters
  • filename (str) – A file path containing CSV occurrence data.

  • species_field (str) – The field name of the column containing species data.

  • x_field (str) – The field name of the column containing x coordinates.

  • y_field (str) – The field name of the column containing y coordinates.

  • geopoint (str) – The field name of the column containing geopoint data.

  • group_field (str) – The name of the field to use for grouping points.

__enter__(self)[source]

Context manager magic method.

Returns

This instance.

Return type

PointCsvReader

__exit__(self, *args)[source]

Context manager magic method on exit.

Parameters

*args – Positional arguments passed to the exit function.

__iter__(self)[source]

Iterator magic method.

Returns

This instance.

Return type

PointCsvReader

__next__(self)[source]

Get lists of consecutive points with the same attribute value.

Returns

A list of point objects.

Return type

list

Raises
  • KeyError – Raised if an attribute is missing.

  • StopIteration – Raised when there are no additional objects.

close(self)[source]

Close the file.

open(self)[source]

Open the file and initialize.

class lmpy.point.PointCsvWriter(filename, fields, write_headers=True, mode='w', **kwargs)[source]

Constructor for writing points to csv file.

Parameters
  • filename (str) – A file location to write points to.

  • fields (list) – A list of fields to include in the csv headers.

  • write_headers (bool) – Should headers be written.

  • mode (str) – File write mode.

  • **kwargs (dict) – Keyword parameters that will be passed to the DictWriter instance from the csv module.

__enter__(self)[source]

Context manager magic method.

Returns

This instance.

Return type

PointCsvWriter

__exit__(self, *args)[source]

Context manager magic method on exit.

Parameters

*args – Positional arguments passed to the exit function.

close(self)[source]

Close file.

open(self)[source]

Open file for writing.

write_points(self, points)[source]

Write a Point object to the CSV file.

Parameters

points (list of Point) – A list of points to write.

class lmpy.point.PointDwcaReader(dwca_filename, meta_filename=DEFAULT_META_FILENAME, species_term=DEFAULT_SPECIES_TERM, x_term=DEFAULT_X_TERM, y_term=DEFAULT_Y_TERM, geopoint_term=None)[source]

Constructor for reading Darwin Core Archives.

Parameters
  • dwca_filename (str) – File location of a DWCA zip file.

  • meta_filename (str) – File within the archive containing metadata. Defaults to DEFAULT_META_FILENAME.

  • species_term (str) – Species term in the DWCA file. Defaults to DEFAULT_SPECIES_TERM.

  • x_term (str) – X term in the DWCA file. Defaults to DEFAULT_X_TERM.

  • y_term (str) – Y term in the DWCA file. Defaults to DEFAULT_Y_TERM.

  • geopoint_term (str) – Geopoint term in the DWCA file. Default is None.

__enter__(self)[source]

Context manager magic method.

Returns

This instance.

Return type

PointDwcaReader

__exit__(self, *args)[source]

Context manager magic method on exit.

Parameters

*args – Positional arguments passed to the exit function.

__iter__(self)[source]

Iterator magic method.

Returns

This instance.

Return type

PointDwcaReader

__next__(self)[source]

Get lists of consecutive points with the same attribute value.

Returns

A list of point objects.

Return type

list

Raises

StopIteration – Raised when there are no additional objects.

_get_species_name(self, point_dict)[source]

Get the species name from the attribute dictionary.

Parameters

point_dict (dict) – A dictionary of point attributes.

Returns

A species name

Return type

str

_get_x_value(self, point_dict)[source]

Get the x coordinate value from the attribute dictionary.

Parameters

point_dict (dict) – A dictionary of point attributes.

Returns

The x coordinate retrieved. None: Returned if there is no x value.

Return type

numeric

_get_y_value(self, point_dict)[source]

Get the y coordinate value from the attribute dictionary.

Parameters

point_dict (dict) – A dictionary of point attributes.

Returns

The y coordinate retrieved. None: Returned if there is no y value.

Return type

numeric

_process_metadata(self, meta_contents)[source]

Process the metadata file contained in the archive.

Parameters

meta_contents (str) – The string contents of the metadata file (meta.xml).

close(self)[source]

Close the file.

open(self)[source]

Open the file and initialize.

class lmpy.point.PointJsonWriter(filename)[source]

Constructor for writing JSON points.

Parameters

filename (str) – A file location to write the points to.

__enter__(self)[source]

Context manager magic method.

Returns

This instance.

Return type

PointJsonWriter

__exit__(self, *args)[source]

Exit and write JSON.

Parameters

*args – Positional arguments sent to the exit function.

close(self)[source]

Close the writer.

open(self)[source]

Dummy method for consistency.

write_points(self, points)[source]

Add a point to the JSON output.

Parameters

points (list of Point) – A list of point objects to write out.

lmpy.point.get_field_process_func(index=None, default=None, vocabulary=None, delimiter=None)[source]

Get a function to process a field for a specimen record.

Parameters
  • index (int, optional) – The column index of the field value to process. If none, always return the default.

  • default (number or string, optional) – An optional default value (optional if index is not None) to return when the value of the field is empty.

  • vocabulary (str, optional) – A URI that identifies a vocabulary used for this field’s possible values.

  • delimiter (str, optional) – An optional delimiter to split the field value with.

Returns

A method for getting the value of a field for a specimen row.

Return type

Method

lmpy.point.none_getter(obj)[source]

Return None as a function.

Parameters

obj (object) – Any object.

Returns

Always returns None.

Return type

None