Added by jgarnett, last edited by jgarnett on Jul 10, 2008  (view change)

Labels

 
(None)

The main module includes an implementation of SimpleFeatureType, which is responsible for describing the contents of a SimpleFeature. SimpleFeatureType includes information such as the name of the Feature, its CoordinateReferenceSystem and information about the attributes making up the feature.

Related

Related GeoTools 2.4

Creating a SimpleFeatureType using SimpleTypeBuilder

Here is how you can create a "Flag" SimpleFeatureType using a builder:

SimpleTypeBuilder builder = new SimpleTypeBuilder();
  builder.setName( "Flag" );
  builder.setNamespaceURI( "http://localhost/" );
  builder.setCRS( DefaultGeographicCRS.WSG84 );
  
  //add attributes in order
  builder.add( "Location", Point.class );
  builder.add( "ID", Integer.class );
  builder.add( "Name", String.class );
  
  //build the type
  final SimpleFeatureType FLAG = builder.buildFeatureType();

Since FeatureType is immutable you will often see them used as constants as shown above.

Alternative - Creating a SimpleFeatureType using a TypeFactory and SimpleTypeFactory

Using a builder is really handy (and recommended). You can however make use of a factory directly:

TypeFactory typeFactory = CommonFactoryFinder.getTypeFactory( null );
SimpleTypeFactory featureTypeFactory = CommonFactoryFinder.getSimpleTypeFeatureFactory( null );

URI namespace = new URI("http://localhost/Flag/");
CoordianteReferenceSystem crs = CRS.decode("EPSG:4326");

Name locationName = new NameImpl( namespace, "Location" );
InternationalString locationDescription = new SimpleInternationalString("Location of the base of this Flag, in WSG84");
GeometryAttributeType GEOM = typeFactory.createGeometryType( locationName, Point.class, crs, false, false, null, null, locationDescription );

Name idName = new NameImpl( namespace, "Id" );
AttributeType ID = typeFactory.createAttributeType( idName, Integer.class, false, false, null, null, null );

Name locationName = new NameImpl( namespace, "Name" );
AttributeType NAME = typeFactory.createAttributeType( nameName, String.class, false, false, null, null, null );

Name name = new NameImpl( new URI("http://localhost/"), "Flag" );
InternationalString description = new SimpleInternationalString("A Flag used to place a marker on the world");

AttributeDescriptor defaultGeoemtry = typeFactory.createAttributeDescriptor(GEOM, geomName, 1, 1, true, null );

List<AttributeDescriptor> types = new ArrayList<AttributeDescriptor>();
types.add( defaultGeometry );
types.add( typeFactory.createAttributeDescriptor(ID, idName, 1, 1, false, new Integer(0) ) );
types.add( typeFactory.createAttributeDescriptor(NAME, nameName, 1, 1, true, null ) );

final FeatureType FLAG = featureTypeFactory.createSimpleFeatureType( name, types, defaultGeometry, crs, Collections.EMPTY_SET, description );

Alternative - Creating a FeatureType using DataUtilities

DataUtilities has a method that you can use to quickly create a FeatureType for test cases:

FeatureType schema = DataUtilities.create("Flag","Location:Point,Name:String");

you can precise the Coordinate Reference System using :

FeatureType schema = DataUtilities.create("Flags","geom:MultiPoint:srid=4326,Name:String");

You can also ask for the String representation of a FeatureType:

System.out.println( DataUtilities.spec( schmea ) );

This is only recommended for tests cases, as it does not offer you the opportunity to specify the CoordinateReferenceSystem for your FeatureType.

Using SimpleFeatureType to figure out the CoordinateReferenceSystem

You can ask SimpleFeatureType for its CoordinateReferenceSystem:

SimpleFeatureType schema = feature.getFeatureType();
CoordianteReferenceSystem crs = schmea.getCRS();

Please be warned that this value may be null if the CoordianteReferenceSystem is unknown.

Creating a Simple Feature using SimpleFeatureBuilder

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( simpleFeatureType );
  
  //add the attributes
  featureBuilder.add( new Point( 0 , 0 ) );
  featureBuilder.add( 12 );
  featureBuilder.add( "My Name" );
  
  //build the feature
  SimpleFeature feature = featureBuilder.buildFeature( "Flag.12" );