• support@ifer.cz
  • +420 2 4195 0607

Grouping species

By Martin Cerny / Nov 21, 2016

In many tasks of data analysis the results are presented by species. However, often the data are not distributed evenly by individual species.

Field-Map provides with handy solution for efficient classification of species into species groups.

Class TSpeciesGroupsWrapper is available with all required functionality:

TSpeciesGroupsWrapper = class(TObjectWrapper)
public
  constructor Create;
  destructor Destroy;
  procedure Free;

  property ID :variant;
  property Name :string;
  property FullName :string;

  property SpeciesLookupTableName :string;

  function SpecGroupsCount :integer;
  function UngroupedSpeciesCount :integer;

  function SpecGroupName(const SpecGroupID_ :variant) :string;
  function SpecGroupFullName(const SpecGroupID_ :variant) :string;

  function SpecGroupIDForSpecies(const Species_ :variant) :variant;

  function ClassifyTable(
    TableOrLayer :TObjectWrapper;
    const SpeciesAttributeName_,SpecGroupAttributeName_ :string) :boolean;

  function ClassifyLayerUsingSQL(
    const LayerName_,SpeciesAttributeName_,SpecGroupAttributeName_,
    WHERE_ :string) :boolean;

  function EditClassification(
    const EnableGroupChanging_ :boolean; const Memo_ :string) :boolean;

  function PublishClassificationToMemTable :TTableWrapper;

  procedure Assign(Source :TSpeciesGroupsWrapper);

  function LoadFromTable(
    TableOrLayer :TObjectWrapper; const FieldName_ :string) :boolean;
  function SaveToTable(
    TableOrLayer :TObjectWrapper; const FieldName_ :string) :boolean;

  function LoadFromXML(const XmlFilename_ :string) :boolean;
  function SaveToXML(const XmlFilename_ :string) :boolean;

  function GetSpeciesCompositionInfo(
    TableOrLayer :TObjectWrapper; const SpeciesAttributeName_ :string) :string;

  function UpdateSpeciesGroupsLookupList(
    const LayerName_,AttributeName_ :string) :boolean;

end;

Species group classification defines assignment of species into species group.

Classification is identified by its ID, Name, and FullName.

SpeciesLookupTableName property is used to assign species lookup table to species group classification. From this lookup table the overall list of species to be classified is taken. User might specify SpeciesLookupTableName in all cases but it is not necessary when tree layer is present in the project. In such a case the species lookup list is taken automatically from the species attribute of this layer.

SpecGroupIDForSpecies is the basic function which returns ID of the species group for given species.

SpecGroupsCount, SpecGroupName and SpecGroupFullName provides with information on species group.

EditClassification opens dialog for species and species groups assignment. It is possible to create and edit species groups:

If the Memo parameter of EditClassification is defined then the memo section of the dialog window is active. Any suitable information can be passed to the dialog via memo. Usually it is convenient to pass the information on the species composition. For getting information on species composition by analyzing data of any data table or layer there is function GetSpeciesCompositionInfo.

When the classification is available it can be published in the form of memory table using PublishClassificationToMemTable function. The table can be published to Excel.

It is common that together with the species attribute there is another attribute for species group. Both of them are lookup lists. For automatic creation of the lookup table content of species group attribute there is UpdateSpeciesGroupsLookupList function.

In addition to the SpecGroupIDForSpecies which is used for particular value of the species there are two methods for batch processing of whole table or layer: ClassifyTable, ClassifyLayerUsingSQL.

Species groups classification may be stored to the attribute. The MEMO or BLOB is suitable for that. LoadFromTableSaveToTable handles storage of the classification to the layer attribute. If the attribute is of MEMO type the classification is stored in the form of XML, in BLOB the classification is stored binary in compressed form.

To export or import classification from external XML file it is possible to use LoadFromXML, SaveToXML methods. If only filename is used as a parameter without specification of the directory, the XML file is assumed to be in MISC directory of the project.

Following script example presents all steps which are usually made for creation of new species group classification:

var spg :TSpeciesGroupsWrapper;
    tb :TTableWrapper;
begin

  //create object of species group classification
  spg := TSpeciesGroupsWrapper.Create;
  try
    //assign species lookup table
    spg.SpeciesLookupTableName:='qSpecies';

    //load existing species group classification
    spg.LoadFromTable(Plots.Table,'SpecGroupsBLOB');

    //run editor of species groups; show species composition info in the dialog
    if spg.EditClassification( false,
                               spg.GetSpeciesCompositionInfo(Data,'Species')) then
    begin                

      //save edit species groups to the table
      spg.SaveToTable(Plots.Table,'SpecGroupsBLOB');
     
      //create or update lookup table of species groups
      spg.UpdateSpeciesGroupsLookupList('Data','SpecGroup');
                                                
      //publish classification scheme to table and to Excel
      tb := spg.PublishClassificationToMemTable;
      if tb<>nil then
      try
        tb.SaveToExcel('test','Species groups',true,true);
      finally
        tb.Free;
      end;
    end;

  finally
    //free species groups object from memory
    spg.Free;
  end;
end.

Species groups are edited, classification is stored to the database, and published to Excel.

The other example script performs classification of species into species groups for whole table.

var spg :TSpeciesGroupsWrapper;

begin

  //create object of species group classification
  spg := TSpeciesGroupsWrapper.Create;
  try

    //load existing species group classification
    spg.LoadFromTable(Plots.Table,'SpecGroupsBLOB');

    //alternative 1: classify whole table
    spg.ClassifyTable (Data,'Species','SpecGroup');

  finally
    //free species groups object from memory
    spg.Free;
  end;

end.

As an alternative it is possible to use classification which is performed using SQL query. For large data sets this is faster and data processing is not attached to the screen interface. Afterwards it is necessary to Refresh updated layer in order to show changes in Field-Map Data Collector window.

    //alternative 2: classify whole table using SQL query
    spg.ClassifyLayerUsingSQL('Data','Species','SpecGroup','');

    //refresh Data layer
    Data.Refresh;

As a result of classification the species group attribute of all table records is filled with new values:

The script and the example is working from Field-Map X6.0.7444 or newer!

Download scripting example 007 (Field-Map Project)


You must be logged in to post a comment