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 );
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 );
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();