How to create Features and to set Properties in them

Please note that this page is outdated as it only targets HALE up to version 2.1.2

This short article explains how to create Feature objects and how to set properties of features. It is relevant both to CST and HALE programmers.

The FeatureBuilder

Since we are working with complex Features, we cannot use SimpleFeature implementation objects. Instead, we are using FeatureImpl objects. For creating these, a utility class is available that can be invoked as follows:

import eu.esdihumboldt.cst.transformer.service.rename.FeatureBuilder;
...
Feature newFeature = FeatureBuilder.buildFeature(ft, source, true);
  • ft is the FeatureType for which to build a Feature}
  • source a source Feature from which to use the ID. Can be null, in which case a new random UUID will be assigned.
  • createNestedFeatures can be set to true if you want the feature builder to already create one instance for each attribute that is itself a Feature. This increases memory consumption and decreases transformation speed and is not recommended anymore, since we now also have a lazy approach for this (see next section).

Getting and Setting Properties of complex Features

You should always only use the accessor methods that the Feature interface itself provides to ensure that you code is independet from implementation details. You can access a property of a Feature like this, and retrieve it's value:

org.opengis.feature.Property property = feature.getProperty("PropertyLocalName");
Object value = property.getValue();

Setting the value works like this:

feature.getProperty("PropertyLocalName").setValue(newValue);

If you need to create or to set so-called nested properties, i.e. the script element of a SpellingOfName attribute in a GeographicalName -type property of the target Features, there are tools available in the commons project to support you, which can be used as follows:

import eu.esdihumboldt.tools.FeatureInspector;
...
eu.esdihumboldt.goml.omwg.Property targetProperty;
...
FeatureInspector.setPropertyValue(targetFeature, targetProperty.getAbout(), value);
  • targetFeature is the Feature in which you want to set a property's value
  • targetProperty.getAbout() the IAbout of a OML property object that indicates the location of the nested property you wish to set. Note that this about must contain a specially formatted URL describing the path from the feature's first level properties to the property to set.
  • value the Object representing the value you want to set.

Note that all missing Features in between the targetFeature and the property you are setting will be created automatically.