Class SimpleFeatureTypeBuilder


  • public class SimpleFeatureTypeBuilder
    extends Object
    A builder for simple feature types.

    Simple Usage:

      
      //create the builder
      SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    
      //set global state
      builder.setName( "testType" );
      builder.setNamespaceURI( "http://www.geotools.org/" );
      builder.setSRS( "EPSG:4326" );
    
      //add attributes
      builder.add( "intProperty", Integer.class );
      builder.add( "stringProperty", String.class );
      builder.add( "pointProperty", Point.class );
    
      //add attribute setting per attribute state
      builder.minOccurs(0).maxOccurs(2).nillable(false).add("doubleProperty",Double.class);
    
      //build the type
      SimpleFeatureType featureType = builder.buildFeatureType();
      
     
    This builder builds type by maintaining state. Two types of state are maintained: Global Type State and Per Attribute State. Methods which set global state are named set<property>(). Methods which set per attribute state are named <property>() . Furthermore calls to per attribute

    Global state is reset after a call to buildFeatureType(). Per attribute state is reset after a call to add(java.lang.String, java.lang.Class<?>).

    A default geometry for the feature type can be specified explictly via setDefaultGeometry(String). However if one is not set the first geometric attribute (GeometryType) added will be resulting default. So if only specifying a single geometry for the type there is no need to call the method. However if specifying multiple geometries then it is good practice to specify the name of the default geometry type. For instance:

      builder.add( "pointProperty", Point.class );
      builder.add( "lineProperty", LineString.class );
      builder.add( "polygonProperty", Polygon.class );
    
      builder.setDefaultGeometry( "lineProperty" );
            
    Author:
    Justin Deolivera, Jody Garnett
    • Field Detail

      • bindings

        protected Map<Class<?>,​AttributeType> bindings
        Map of java class bound to properties types.
      • local

        protected String local
        Naming: local name
      • uri

        protected String uri
        Naming: uri indicating scope
      • restrictions

        protected List<Filter> restrictions
        Additional restrictions on the type.
      • defaultGeometry

        protected String defaultGeometry
        Name of the default geometry to use
      • defaultCrsSet

        protected boolean defaultCrsSet
      • isAbstract

        protected boolean isAbstract
        flag controlling if the type is abstract.
    • Constructor Detail

      • SimpleFeatureTypeBuilder

        public SimpleFeatureTypeBuilder()
        Constructs the builder.
      • SimpleFeatureTypeBuilder

        public SimpleFeatureTypeBuilder​(FeatureTypeFactory factory)
        Constructs the builder specifying the factory for creating feature and feature collection types.
    • Method Detail

      • setFeatureTypeFactory

        public void setFeatureTypeFactory​(FeatureTypeFactory factory)
        Sets the factory used to create feature and feature collection types.
      • getFeatureTypeFactory

        public FeatureTypeFactory getFeatureTypeFactory()
        The factory used to create feature and feature collection types.
      • init

        public void init​(SimpleFeatureType type)
        Initializes the builder with state from a pre-existing feature type.
      • init

        protected void init()
        Clears the running list of attributes.
      • reset

        protected void reset()
        Completely resets all builder state.
      • setNamespaceURI

        public void setNamespaceURI​(String namespaceURI)
        Set the namespace uri of the built type.
      • setNamespaceURI

        public void setNamespaceURI​(URI namespaceURI)
      • getNamespaceURI

        public String getNamespaceURI()
        The namespace uri of the built type.
      • setName

        public void setName​(String name)
        Sets the name of the built type.
      • getName

        public String getName()
        The name of the built type.
      • setName

        public void setName​(Name name)
        Sets the local name and namespace uri of the built type.
      • setDescription

        public void setDescription​(InternationalString description)
        Sets the description of the built type.
      • getDescription

        public InternationalString getDescription()
        The description of the built type.
      • setDefaultGeometry

        public void setDefaultGeometry​(String defaultGeometryName)
        Sets the name of the default geometry attribute of the built type.
      • getDefaultGeometry

        public String getDefaultGeometry()
        The name of the default geometry attribute of the built type.
      • setCRS

        public void setCRS​(CoordinateReferenceSystem crs)
        Sets the coordinate reference system of the next attribute being added.

        The supplied coordinate reference is a "default" only used if geometric attributes are later added to the type without specifying their coordinate reference system.

        You must call this method prior to calling add for it to function.

        Example:

         
         builder.setCRS( DefaultGeographicCRS.EGS84 );
         builder.add( "geom", Polygon.class );
         
         
      • getCRS

        public CoordinateReferenceSystem getCRS()
        The fallback coordinate reference system that will be applied to any geometric attributes added to the type without their own coordinate reference system specified.
      • setSRS

        public void setSRS​(String srs)
        Sets the coordinate reference system of the next attribute being added by specifying its srs.
        Throws:
        IllegalArgumentException - When the srs specified can be decored into a crs.
      • setAbstract

        public void setAbstract​(boolean isAbstract)
        Sets the flag controlling if the resulting type is abstract.
      • isAbstract

        public boolean isAbstract()
        The flag controlling if the resulting type is abstract.
      • setSuperType

        public void setSuperType​(SimpleFeatureType superType)
        Sets the super type of the built type.
      • getSuperType

        public SimpleFeatureType getSuperType()
        The super type of the built type.
      • addBinding

        public void addBinding​(AttributeType type)
        Specifies an attribute type binding.

        This method is used to associate an attribute type with a java class. The class is retreived from type.getBinding(). When the add(String, Class) method is used to add an attribute to the type being built, this binding is used to locate the attribute type.

        Parameters:
        type - The attribute type.
      • addBindings

        public void addBindings​(Schema schema)
        Specifies a number of attribute type bindings.
        Parameters:
        schema - The schema containing the attribute types.
      • setBindings

        public void setBindings​(Schema schema)
        Specifies a number of attribute type bindings clearing out all existing bindings.
        Parameters:
        schema - The schema contianing attribute types.
      • getBinding

        public AttributeType getBinding​(Class<?> binding)
        Looks up an attribute type which has been bound to a class.
        Parameters:
        binding - The class.
        Returns:
        AttributeType The bound attribute type.
      • length

        public SimpleFeatureTypeBuilder length​(int length)
        Sets a restriction on the field length of the next attribute added to the feature type.

        This method is the same as adding a restriction based on length( value ) < length This value is reset after a call to add(String, Class)

      • options

        public SimpleFeatureTypeBuilder options​(Object... options)
        Sets a restriction on the possible field values of the next attribute added to the feature type.

        This method is the same as adding a restriction based on value in (option1, option2, ...}. This restriction is reset after a call to add(String, Class)

      • options

        public SimpleFeatureTypeBuilder options​(List<?> options)
        Sets a restriction on the possible field values of the next attribute added to the feature type.

        This method is the same as adding a restriction based on value in (option1, option2, ...}. This restriction is reset after a call to add(String, Class)

      • srs

        public SimpleFeatureTypeBuilder srs​(String srs)
        Sets the srs of the next attribute added to the feature type.

        The srs parameter is the id of a spatial reference system, for example: "epsg:4326".

        This only applies if the attribute added is geometric.

        This value is reset after a call to add(String, Class)

        Parameters:
        srs - The spatial reference system.
      • srid

        public SimpleFeatureTypeBuilder srid​(Integer srid)
        Sets the srid of the next attribute added to the feature type.

        The srid parameter is the epsg code of a spatial reference system, for example: "4326".

        This only applies if the attribute added is geometric.

        This value is reset after a call to add(String, Class)

        Parameters:
        srid - The id of a spatial reference system.
      • userData

        public SimpleFeatureTypeBuilder userData​(Object key,
                                                 Object value)
        Sets user data for the next attribute added to the feature type.

        This value is reset after a call to add(String, Class)

        Parameters:
        key - The key of the user data.
        value - The value of the user data.
      • descriptor

        public SimpleFeatureTypeBuilder descriptor​(AttributeDescriptor descriptor)
        Sets all the attribute specific state from a single descriptor.

        This method is convenience for: builder.minOccurs( descriptor.getMinOccurs() ).maxOccurs( descriptor.getMaxOccurs() ) .nillable( descriptor.isNillable() )...

      • add

        public void add​(String name,
                        Class<?> binding)
        Adds a new attribute w/ provided name and class.

        The provided class is used to locate an attribute type binding previously specified by addBinding(AttributeType),addBindings(Schema), or setBindings(Schema).

        If not such binding exists then an attribute type is created on the fly.

        Parameters:
        name - The name of the attribute.
        binding - The class the attribute is bound to.
      • remove

        public AttributeDescriptor remove​(String attributeName)
        Removes an attribute from the builder
        Parameters:
        attributeName - the name of the AttributeDescriptor to remove
        Returns:
        the AttributeDescriptor with the name attributeName
        Throws:
        IllegalArgumentException - if there is no AttributeDescriptor with the name attributeName
      • addAll

        public void addAll​(AttributeDescriptor... descriptors)
        Adds an array of descriptors directly to the builder.

        Use of this method is discouraged. Consider using add(String, Class).

      • add

        public void add​(String name,
                        Class<?> binding,
                        CoordinateReferenceSystem crs)
        Adds a new geometric attribute w/ provided name, class, and coordinate reference system.

        The crs parameter may be null.

        Parameters:
        name - The name of the attribute.
        binding - The class that the attribute is bound to.
        crs - The crs of of the geometry, may be null.
      • add

        public void add​(String name,
                        Class<?> binding,
                        String srs)
        Adds a new geometric attribute w/ provided name, class, and spatial reference system identifier

        The srs parameter may be null.

        Parameters:
        name - The name of the attribute.
        binding - The class that the attribute is bound to.
        srs - The srs of of the geometry, may be null.
      • add

        public void add​(String name,
                        Class<?> binding,
                        Integer srid)
        Adds a new geometric attribute w/ provided name, class, and spatial reference system identifier

        The srid parameter may be null.

        Parameters:
        name - The name of the attribute.
        binding - The class that the attribute is bound to.
        srid - The srid of of the geometry, may be null.
      • get

        public AttributeDescriptor get​(int index)
        Gets an AttributeBuilder configured using the descriptor at the provided index.
        Parameters:
        index - attribute index
        Returns:
        attribute builder configured with descriptor
      • set

        public void set​(int index,
                        AttributeDescriptor descriptor)
        Replace the descriptor at the provided index.
      • set

        public void set​(AttributeDescriptor descriptor)
        Replace the descriptor at the provided index.
      • set

        public void set​(String attributeName,
                        AttributeDescriptor descriptor)
        Replace the descriptor at the provided index.
      • set

        public void set​(String attributeName,
                        AttributeTypeBuilder attributeBuilder)
        Replace the descriptor at the provided index.
      • setAttributes

        public void setAttributes​(List<AttributeDescriptor> attributes)
        Directly sets the list of attributes.
        Parameters:
        attributes - the new list of attributes, or null to reset the list
      • setAttributes

        public void setAttributes​(AttributeDescriptor... attributes)
        Directly sets the list of attributes.
        Parameters:
        attributes - the new list of attributes, or null to reset the list
      • buildFeatureType

        public SimpleFeatureType buildFeatureType()
        Builds a feature type from compiled state.

        After the type is built the running list of attributes is cleared.

        Returns:
        The built feature type.
      • newSet

        protected Set newSet()
        Creates a new set instance, this default implementation returns HashSet.
      • newList

        protected List newList()
        Creates a new list instance, this default impelementation returns ArrayList.
      • newMap

        protected Map newMap()
        Creates a new map instance, this default implementation returns HashMap
      • newList

        protected List newList​(List origional)
        Creates a new list which is the same type as the provided list.

        If the new copy can not be created reflectively.. newList() is returned.

      • name

        protected Name name()
        Naming: Accessor which returns type name as follows:
        1. If typeName has been set, its value is returned.
        2. If name has been set, it + namespaceURI are returned.
      • attributes

        protected List<AttributeDescriptor> attributes()
        Accessor for attributes (will create attributes list if needed)
      • restrictions

        protected List<Filter> restrictions()
        Accessor for restrictions (will create restrictions list if needed).
      • bindings

        protected Map<Class<?>,​AttributeType> bindings()
        Accessor for bindings (will create binding map if needed).
      • retype

        public static SimpleFeatureType retype​(SimpleFeatureType original,
                                               String... attributes)
        Create a SimpleFeatureType containing just the attribute descriptors indicated.
        Parameters:
        original - SimpleFeatureType
        attributes - name of attributes to include in result
        Returns:
        SimpleFeatureType containing just the types indicated by name
      • retype

        public static SimpleFeatureType retype​(SimpleFeatureType original,
                                               List<String> attributes)
        Create a SimpleFeatureType containing just the attribute descriptors indicated.
        Parameters:
        original - SimpleFeatureType
        attributes - name of attributes to include in result
        Returns:
        SimpleFeatureType containing just the types indicated by name
      • retype

        public static SimpleFeatureType retype​(SimpleFeatureType original,
                                               CoordinateReferenceSystem crs)
        Create a SimpleFeatureType with the same content; just updating the geometry attribute to match the provided coordinate reference system.
        Parameters:
        original - SimpleFeatureType
        crs - CoordinateReferenceSystem of result
        Returns:
        SimpleFeatureType updated with the provided CoordinateReferenceSystem
      • retype

        public static SimpleFeatureType retype​(SimpleFeatureType original,
                                               Query query)
        Create a SimpleFeatureType with the parameters and coordinate reference system of the query.
        Parameters:
        original - SimpleFeatureType
        query - Query defining attributes and coordinate reference system
        Returns:
        SimpleFeatureType updated with the provided CoordinateReferenceSystem
      • copy

        public static SimpleFeatureType copy​(SimpleFeatureType original)
        Copies a feature type.

        This method does a deep copy in that all individual attributes are copied as well.