org.geotools.feature
Class DefaultFeature

java.lang.Object
  extended byorg.geotools.feature.DefaultFeature
All Implemented Interfaces:
java.lang.Cloneable, org.opengis.util.Cloneable, Feature, SimpleFeature
Direct Known Subclasses:
MutableFIDFeature

public class DefaultFeature
extends java.lang.Object
implements SimpleFeature, org.opengis.util.Cloneable

Provides a more efficient feature representation for the flat and complex features. This implementation actually not only enforces feature type synchronization, it also enforces the use of its accessor methods to change the state of internal object representations. In this case, the implementation is trivial, since all allowed attribute objects (from the feature type) are immutable.

Author:
Chris Holmes, TOPP
, Rob Hranac, TOPP, Ian Schneider ARS-USDA
Task:
TODO: look at synchronization (or locks as IanS thinks), REVISIT: Right now we always validate, which means whenever a Feature is created or a new value set then an operation must be performed. One thing we should consider is to allow a Feature to turn off its its validation - which would likely improve performance with large datasets. If you are reading from a database, with a FeatureType you got from the database, it is probably a reasonable assumption that the Features contained in it will properly validate. I am not sure if this should with a switch in DefaultFeature, or perhaps an interface that says if it is validating or not, or maybe even an option in Feature. But it would be a nice option to have - if datastore implementors could at least create their features without validating (though probably should return Features that will check for validity if someone else tries to change them).

Nested Class Summary
 
Nested classes inherited from class org.geotools.feature.Feature
Feature.NULL
 
Field Summary
protected  java.lang.String featureId
          The unique id of this feature
 
Constructor Summary
protected DefaultFeature(DefaultFeatureType schema, java.lang.Object[] attributes)
          Creates a new instance of flat feature, which must take a flat feature type schema and all attributes as arguments.
protected DefaultFeature(DefaultFeatureType schema, java.lang.Object[] attributes, java.lang.String featureID)
          Creates a new instance of flat feature, which must take a flat feature type schema and all attributes as arguments.
 
Method Summary
 java.lang.Object clone()
          Creates an exact copy of this feature.
 boolean equals(java.lang.Object obj)
          override of equals.
 java.lang.Object getAttribute(int index)
          Gets an attribute by the given zero-based index.
 java.lang.Object getAttribute(java.lang.String xPath)
          Gets an attribute for this feature at the location specified by xPath.
 java.lang.Object[] getAttributes(java.lang.Object[] array)
          Copy all the attributes of this Feature into the given array.
 com.vividsolutions.jts.geom.Envelope getBounds()
          Get the total bounds of this feature which is calculated by doing a union of the bounds of each geometry this feature is associated with.
 com.vividsolutions.jts.geom.Geometry getDefaultGeometry()
          Gets the geometry for this feature.
 FeatureType getFeatureType()
          Gets a reference to the feature type schema for this feature.
 java.lang.String getID()
          Gets the unique indentification string of this Feature.
 int getNumberOfAttributes()
          Get the number of attributes this feature has.
 FeatureCollection getParent()
          Gets the feature collection this feature is stored in.
 int hashCode()
          returns a unique code for this feature
 void setAttribute(int position, java.lang.Object val)
          Sets the attribute at position to val.
 void setAttribute(java.lang.String xPath, java.lang.Object attribute)
          Sets a single attribute for this feature, passed as a complex object.
 void setAttributes(java.lang.Object[] attributes)
          Sets all attributes for this feature, passed as an array.
protected  void setAttributeValue(int position, java.lang.Object val)
          Sets the attribute value at a given position, performing no parsing or validation.
 void setDefaultGeometry(com.vividsolutions.jts.geom.Geometry geometry)
          Modifies the geometry.
 void setParent(FeatureCollection collection)
          Sets the parent collection this feature is stored in, if it is not already set.
 Feature toComplex()
           
 java.lang.String toString()
          Returns a string representation of this feature.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

featureId

protected java.lang.String featureId
The unique id of this feature

Constructor Detail

DefaultFeature

protected DefaultFeature(DefaultFeatureType schema,
                         java.lang.Object[] attributes,
                         java.lang.String featureID)
                  throws IllegalAttributeException,
                         java.lang.NullPointerException
Creates a new instance of flat feature, which must take a flat feature type schema and all attributes as arguments.

Parameters:
schema - Feature type schema for this flat feature.
attributes - Initial attributes for this feature.
featureID - The unique ID for this feature.
Throws:
IllegalAttributeException - Attribtues do not conform to feature type schema.
java.lang.NullPointerException - if schema is null.

DefaultFeature

protected DefaultFeature(DefaultFeatureType schema,
                         java.lang.Object[] attributes)
                  throws IllegalAttributeException
Creates a new instance of flat feature, which must take a flat feature type schema and all attributes as arguments.

Parameters:
schema - Feature type schema for this flat feature.
attributes - Initial attributes for this feature.
Throws:
IllegalAttributeException - Attribtues do not conform to feature type schema.
Task:
REVISIT: should we allow this? Force users to explicitly set featureID to null?
Method Detail

getFeatureType

public FeatureType getFeatureType()
Gets a reference to the feature type schema for this feature.

Specified by:
getFeatureType in interface SimpleFeature
Returns:
A copy of this feature's metadata in the form of a feature type schema.

getID

public java.lang.String getID()
Gets the unique indentification string of this Feature.

Specified by:
getID in interface Feature
Returns:
The unique id.

getAttributes

public java.lang.Object[] getAttributes(java.lang.Object[] array)
Copy all the attributes of this Feature into the given array. If the argument array is null, a new one will be created. Gets all attributes from this feature, returned as a complex object array. This array comes with no metadata, so to interpret this collection the caller class should ask for the schema as well.

Specified by:
getAttributes in interface Feature
Parameters:
array - The array to copy the attributes into.
Returns:
The array passed in, or a new one if null.

getAttribute

public java.lang.Object getAttribute(java.lang.String xPath)
Gets an attribute for this feature at the location specified by xPath.

Specified by:
getAttribute in interface SimpleFeature
Parameters:
xPath - XPath representation of attribute location.
Returns:
Attribute.
See Also:
Feature.getAttribute(String)

getAttribute

public java.lang.Object getAttribute(int index)
Gets an attribute by the given zero-based index.

Specified by:
getAttribute in interface SimpleFeature
Parameters:
index - the position of the attribute to retrieve.
Returns:
The attribute at the given index.

setAttribute

public void setAttribute(int position,
                         java.lang.Object val)
                  throws IllegalAttributeException
Sets the attribute at position to val.

Specified by:
setAttribute in interface SimpleFeature
Parameters:
position - the index of the attribute to set.
val - the new value to give the attribute at position.
Throws:
IllegalAttributeException - if the passed in val does not validate against the AttributeType at that position.

setAttributeValue

protected void setAttributeValue(int position,
                                 java.lang.Object val)
Sets the attribute value at a given position, performing no parsing or validation. This is so subclasses can have access to setting the array, without opening it up completely.

Parameters:
position - the index of the attribute to set.
val - the new value to give the attribute at position.

setAttributes

public void setAttributes(java.lang.Object[] attributes)
                   throws IllegalAttributeException
Sets all attributes for this feature, passed as an array. All attributes are checked for validity before adding.

Specified by:
setAttributes in interface SimpleFeature
Parameters:
attributes - All feature attributes.
Throws:
IllegalAttributeException - Passed attributes do not match feature type.

setAttribute

public void setAttribute(java.lang.String xPath,
                         java.lang.Object attribute)
                  throws IllegalAttributeException
Sets a single attribute for this feature, passed as a complex object. If the attribute does not exist or the object does not conform to the internal feature type, an exception is thrown.

Specified by:
setAttribute in interface Feature
Parameters:
xPath - XPath representation of attribute location.
attribute - Feature attribute to set.
Throws:
IllegalAttributeException - Passed attribute does not match feature type

getDefaultGeometry

public com.vividsolutions.jts.geom.Geometry getDefaultGeometry()
Gets the geometry for this feature.

Specified by:
getDefaultGeometry in interface Feature
Returns:
Geometry for this feature.

setDefaultGeometry

public void setDefaultGeometry(com.vividsolutions.jts.geom.Geometry geometry)
                        throws IllegalAttributeException
Modifies the geometry.

Specified by:
setDefaultGeometry in interface Feature
Parameters:
geometry - All feature attributes.
Throws:
IllegalAttributeException - if the feature does not have a geometry.

getNumberOfAttributes

public int getNumberOfAttributes()
Get the number of attributes this feature has. This is simply a convenience method for calling getFeatureType().getNumberOfAttributes();

Specified by:
getNumberOfAttributes in interface Feature
Returns:
The total number of attributes this Feature contains.

getBounds

public com.vividsolutions.jts.geom.Envelope getBounds()
Get the total bounds of this feature which is calculated by doing a union of the bounds of each geometry this feature is associated with.

Specified by:
getBounds in interface Feature
Returns:
An Envelope containing the total bounds of this Feature.
Task:
REVISIT: what to return if there are no geometries in the feature? For now we'll return a null envelope, make this part of interface? (IanS - by OGC standards, all Feature must have geom)

clone

public java.lang.Object clone()
Creates an exact copy of this feature.

Specified by:
clone in interface org.opengis.util.Cloneable
Returns:
A default feature.
Throws:
java.lang.RuntimeException - DOCUMENT ME!

toString

public java.lang.String toString()
Returns a string representation of this feature.

Returns:
A representation of this feature as a string.

hashCode

public int hashCode()
returns a unique code for this feature

Returns:
A unique int

equals

public boolean equals(java.lang.Object obj)
override of equals. Returns if the passed in object is equal to this.

Parameters:
obj - the Object to test for equality.
Returns:
true if the object is equal, false otherwise.

getParent

public FeatureCollection getParent()
Gets the feature collection this feature is stored in.

Specified by:
getParent in interface Feature
Returns:
the collection that is the parent of this feature.

setParent

public void setParent(FeatureCollection collection)
Sets the parent collection this feature is stored in, if it is not already set. If it is set then this method does nothing.

Specified by:
setParent in interface Feature
Parameters:
collection - the collection to be set as parent.

toComplex

public Feature toComplex()


Copyright © GeoTools. All Rights Reserved.