GeoTools : Feature Model Preparation

Much of the api for building features has been deprecated on geotools trunk. The TypeBuilder and FeatureBuilder interfaces developed on the fm branch are intended to be used as the alternative. So in preparation for the new feature model, these classes can be used to create features based on the current model.

Building a Feature Type

Before
//create a new feature type factory
FeatureTypeFactory typeFactory = FeatureTypeFactory.newInstance( "simpleFeatureTypeFactory" );

//set the name
typeFactory.setName( "simpleTypeName" );
typeFactory.setNamespace( new URI( "http://www.geotools.org/simple" ) );

//create the attributes
AttributeType pointPropertyAttributeType = AttributeTypeFactory.newAttributeType( "pointProperty", Point.class );
typeFactory.addType( pointPropertyAttributeType );
typeFactory.setDefaultGeometry( pointPropertyAttributeType );

typeFactory.addType( AttributeTypeFactory.newAttributeType( "lineProperty", LineString.class ) );
typeFactory.addType( AttributeTypeFactory.newAttributeType( "intProperty", Integer.class ) );
typeFactory.addType( AttributeTypeFactory.newAttributeType( "stringProperty", String.class ) );
typeFactory.addType( AttributeTypeFactory.newAttributeType( "dateProperty, Date.class ) );

//build the type
FeatureType featureType = typeFactory.getFeatureType();
After
//create the builder
SimpleTypeBuilder typeBuilder = new SimpleTypeBuilder( CommonFactoryFinder.getSimpleFeatureFactory(null) );

//set the name
typeBuilder.setName( "simpleFeatureType" );
typeBuilder.setNamespace( "http://www.geotools.org/simple" );

//add the attribute types
typeBuilder.attribute( "pointProperty", Point.class );
typeBuilder.attribute( "lineProperty", LineString.class );
typeBuilder.attribute( "intProperty", Integer.class );
typeBuilder.attribute( "stringProperty", String.class );
typeBuilder.attribute( "dateProperty, Date.class );

//set the default geometry
typeBuilder.setGeometryName( "pointProperty" );

//set the crs
typeBuilder.setCRS( CRS.decode( "4326" ) );

//build the type
FeatureType featureType = typeBuilder.feature();

Builing a Feature

Before
//already have a feature type around from somewhere...
FeatureType featureType = ....

//add the attributes
Object[] attributes = new Object[5];
attributes[0] =  new Point(..);
attributes[1] =  new LineString(..);
attributes[2] =  new Integer(..);
attributes[3] =  new String(..);
attributes[4] =  new Date(..);

Feature feature = featureType.create( attributes, "fid" );
After
//already have a feature type around from somewhere...
FeatureType featureType = ....

//create the builder
SimpleFeautreBuilder builder = new SimpleFeatureBuilder( new SimpleFeatureFactoryImpl() );

//set the type
builder.setType( featureType );

//add the attributes
builder.add( new Point(..) );
builder.add( new LineString(..) );
builder.add( new Integer(..) );
builder.add( new String(..) );
builder.add( new Date(..) );

//build the feature
Feature feature = builder.build( "fid" );

Shifting to these new apis can be mostly isolated to the following methods currently used to build features:

  • AttributeTypeFactory.newAttributeType() ( 666 errors )
  • DataUtilities.createType()
  • FeatureTypes.newFeatureType()
  • FeatureType.create() ( 160 errors )

Comments:

So, if the new FeatureTypeImplementation supports the new APIs, this is not a show stopper but in fact a useful step forward we would benefit from doing ASAP ?

Also, not sure that I want to set the CRS for the featureType. I think the CRS should be set per geometry attribute. Its perfectly sensible to have different geometries in different CRS (0-D, 2-D and 3-D representations of a building for instance)

Posted by rob_cto_sco at Dec 19, 2006 13:53