Added by Justin Deoliveira, last edited by jgarnett on May 08, 2008  (view change)

Labels

 
(None)

Building a Feature Type

Simple Case

SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

//set the name
b.setName( "Flag" );

//add some properties
b.add( "name", String.class );
b.add( "classification", Integer.class );
b.add( "height", Double.class );

//add a geometry property
b.setCRS( DefaultGeographicCRS.WSG84 );
b.add( "location", Point.class );

//build the type
SimpleFeatureType type = b.buildFeatureType();

Alternative: Chaining

SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
SimpleFeatureType type;

// you can chain builder methods
type = b.name("Flag").add("name", String.class ).
       add( "classification", Integer.class ).add( "height", Double.class ).
       crs( DefaultGeographicCRS.WSG84 ).add( "location", Point.class ).buildFeatureType();

Namespace

// you can set a namespace
b.setNamespaceURI( "http://geotools.org/example" );

Multiple Geometries with Implicit Default

b.setCRS( DefaultGeographicCRS.WSG84 );

//add some geometry properties (first added is the default)
b.add( "region", Polygon.class );
b.add( "hub", Point.class );
b.add( "network", MultiLineString.class );

Multiple Geometries with Explicit Default

b.setCRS( DefaultGeographicCRS.WSG84 );

b.add( "hub", Point.class );
b.add( "region", Polygon.class );
b.add( "network", MultiLineString.class );

//set the default geometry
b.setDefaultGeometry( "region" ):

CRS for Geometries

CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");

//set the coordinate reference system
b.setCRS( crs );

// when geometry properties are added they will use the crs set above
b.add( "position", Point.class );
b.add( "route", LineString.class );

Alternative: Unknown CRS

b.setCRS( null );
b.add( "position", Point.class );
b.add( "route", LineString.class );

Multiple CRS for Geometries

CoordinateReferenceSystem crs1 = CRS.decode("EPSG:3005");
CoordinateReferenceSystem crs2 = DefaultGeographicCRS.WSG84;

b.setCRS( crs1 );
b.add( "local", Point.class );

b.setCRS( crs2 );
b.add( "world", Point.class );

Alternative: Chaining

CoordinateReferenceSystem crs1 = CRS.decode("EPSG:3005");
CoordinateReferenceSystem crs2 = DefaultGeographicCRS.WSG84;

b.crs( crs1 ).add( "local", Point.class );
b.crs( crs2 ).add( "world", Point.class );

Alternative: Using an SRS

b.srs( "EPSG:3005" ).add( "local", Point.class );
b.srs( "EPSG:4326" ).add( "world", Point.class );

Building an Attribute Descriptor

Simple Case

AttributeTypeBuilder build = new AttributeTypeBuilder();
build.setNillable(true);
build.setBinding(String.class);
            
AttributeDescriptor descriptor = build.buildDescriptor( "name" );

Alternative: With Explicit Attribute Type

AttributeTypeBuilder build = new AttributeTypeBuilder();
build.setNillable(true);
build.setBinding(String.class);
build.setName("Text");

AttributeType textType = build.buildType();            

AttributeDescriptor descriptor = build.buildDescriptor( "name", textType );

Building a Geometry Descriptor

build.setNillable(true);
build.setCRS(crs);
build.setBinding(Polygon.class);
            
GeometryType geometryType = build.buildGeometryType();
GeometryDescriptor build.buildDescriptor( "the_geom", geometryType ) );

Building a Geometry Descriptor with Limited Length

AttributeTypeBuilder build = new AttributeTypeBuilder();
build.setNillable(true);
build.setBinding(String.class);
build.setLength(15);
AttributeDescriptor descriptor = build.buildDescriptor( "username" );

Building a Feature

Simple Case

//the type, schema = ( name:String, classification:Integer, height:Double, location:Point)
SimpleFeatureType type = ...;

//create the builder
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);

//add the values
builder.add( "Canada" );
builder.add( 1 );
builder.add( 20.5 );
builder.add( new Point( -124, 52 ) );

//build the feature with provided ID
SimpleFeature feature = builder.buildFeature( "fid.1" );

Alternative: Array of Values

Object[] values = new Object[]{
  "Canada", 1, 20.5, new Point( -124, 52  )
};
builder.addAll( values );

Alternative: List of Values

ArrayList<Object> values = new ArrayList<Object>( 4 );
values.add("Canada");
values.add( 1 );
values.add( 20.5 );
values.add( new Point( -124, 52  ) );
builder.addAll( list );

Alternative: Setting by Name

builder.set( "name", "Canada" );
builder.set( "classification", 1 );
builder.set( "height", 20.5 );
builder.set( "location", new Point( -124, 52  ) );

Alternative: Setting by Index

builder.set( 0, "Canada" );
builder.set( 1, 1 );
builder.set( 2 20.5 );
builder.set( 3, new Point( -124, 52  ) );

Access a Feature

Simple Case

SimpleFeature feature = ...see above...;

for (Object value : feature.getAttributes() ) {
  System.out.print( value ",");
}
// prints Canada,1,20.5,POINT( -124, 52 ),

Alternative: Access using Index

for (int i = 0; i < feature.getAttributeCount(); i++ ) {
  Object value = feature.getAttribute( i );
  System.out.print( value ",");
}
// prints Canada,1,20.5,POINT( -124, 52 ),
Alternative: Access using Name
for (Property property : feature.getProperties()) {
  String name = property.getName();
  Object value = feature.getAttribute( property.getName() );
  System.out.print( name+"="+value+"," );
}
// prints name=Canada,classification=1,height=20.5,location=POINT( -124, 52 ),

Property Access

Simple Case

Property property = feature.getProperty( "name" );
String name = property.getName();
Object value = property.getValue();

Alternative: Property access using Index

Property property = feature.getProperty( 2 );
String name = property.getName();
Object value = property.getValue();

Geometry Value Access

Point point = (Point) feature.getDefaultGeometry();

Alternative: Access as Value

Point point = (Point) feature.getAttribute( "location" );

Alternative: Access as Property

GeometryAttribute geom = feature.getDefaultGeometryProperty();

String name = geom.getName();
Point point = (Point) theGeom.getValue();
CoordinateReferenceSystem crs = geom.getCRS();
BoundingBox bounds = geom.getBounds();

Alternative: Access using Name

GeometryAttribute geom = (GeometryAttribute) feature.getProperty("location");

CoordinateReferenceSystem crs = geom.getCRS();
BoundingBox bounds = geom.getBounds();
Geometry point = (Geometry) theGeom.getValue();

CoordinateReferenceSystem Access

// Access the CRS of getDefaultGeometryProperty()
CoordinateReferenceSystem crs = feature.getCRS();

Alternative: Access CoordinateReferenceSystem of getDefaultGeometryProperty()

CoordinateReferenceSystem crs =
     feature.getDefaultGeometryProperty() == null ? null : feature.getDefaultGeometryProperty().getCRS();

Alternative: Access CoordinateReferenceSystem of named Property

GeometryAttribute location = (GeometryAttribute) feature.getProperty( "location" );
CoordinateReferenceSystem bounds = location.getCRS();

BoundingBox Access

// Access the BoundingBox of getDefaultGeometryProperty()
BoundingBox bounds = feature.getBounds();

Alternative: Access BoundingBox of getDefaultGeometryProperty()

BoundingBox bounds =
     feature.getDefaultGeometryProperty() == null ? null : feature.getDefaultGeometryProperty().getBounds();

Alternative: Access BoundingBox of named Property

GeometryAttribute location = (GeometryAttribute) feature.getProperty( "location" );
BoundingBox bounds = location.getBounds();

Validating a Feature

for (PropertyDescriptor property : feature.getType().getAttributes() )) {
   Object value = feature.getAttribute( property.getName() );

   Types.validate( property, value );
}
Alternative: Checking Super Types by Hand
SimpleFeature feature = ...;

for (PropertyDescriptor property : feature.getType().getAttributes() )) {
  PropertyType propertyType = property.getType();
  Object value = feature.getAttribute( property.getName() );

  if( value == null ){
     //check nillability
     if ( property.isNillable() ){
        continue;
     }
     else {
        throw new Exception( "value can not be null" );
     }
  }
  //check the type
  if ( type.getBinding().isAssignableFrom( value.getClass() ) ) {
    throw new Exception( "value not same type as binding" );
  }
  // check restrictions for this propertyType and all super types
  for(PropertyType type=propertyType; type !=null; type=propertyType.getSuper() ){
     for( Filter valid : type.getRestrictions() ){
          if( !valid.evaulate( value ) ){
              throw new Exception(
                  "Not a valid "+type.getName()+" values must be:"+valid
               );
          }
     }
  }
}