Added by Justin Deoliveira, last edited by jgarnett on May 08, 2008
( view change)
Building a Feature Type
Simple Case
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
b.setName( "Flag" );
b.add( "name", String.class );
b.add( "classification", Integer.class );
b.add( "height", Double.class );
b.setCRS( DefaultGeographicCRS.WSG84 );
b.add( "location", Point.class );
SimpleFeatureType type = b.buildFeatureType();
Alternative: Chaining
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
SimpleFeatureType type;
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
b.setNamespaceURI( "http: );
Multiple Geometries with Implicit Default
b.setCRS( DefaultGeographicCRS.WSG84 );
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 );
b.setDefaultGeometry( "region" ):
CRS for Geometries
CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
b.setCRS( crs );
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
SimpleFeatureType type = ...;
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
builder.add( "Canada" );
builder.add( 1 );
builder.add( 20.5 );
builder.add( new Point( -124, 52 ) );
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 ",");
}
Alternative: Access using Index
for (int i = 0; i < feature.getAttributeCount(); i++ ) {
Object value = feature.getAttribute( i );
System.out.print( value ",");
}
Alternative: Access using Name
for (Property property : feature.getProperties()) {
String name = property.getName();
Object value = feature.getAttribute( property.getName() );
System.out.print( name+"="+value+"," );
}
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
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
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 ){
if ( property.isNillable() ){
continue;
}
else {
throw new Exception( "value can not be null" );
}
}
if ( type.getBinding().isAssignableFrom( value.getClass() ) ) {
throw new Exception( "value not same type as binding" );
}
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
);
}
}
}
}
|