Class Query

  • Direct Known Subclasses:
    JoiningQuery

    public class Query
    extends Object
    Encapsulates a request for data, typically as:
    
     Query query = ...
     myFeatureSource.getFeatures(query);
     
    The query class is based on the Web Feature Server specification and offers a few interesting capabilities such as the ability to sort results and use a filter (similar to the WHERE clause in SQL).

    Additional capabilities:

    • setMaxFeatures(int) and setStartIndex(Integer) can be used implement 'paging' through the data source's content. This is useful if, for example, the FeatureSource has an upper limit on the number of features it can return in a single request or you are working with limited memory.
    • setHandle(String) can be used to give the query a mnemonic name which will appear in error reporing and logs.
    • setCoordinateSystem(CoordinateReferenceSystem) is used to to specify the coordinate system that retrieved features will be "forced" into. This is often used to correct a feature source when the application and the data format have different ideas about the coordinate system (for example, the "axis order" issue).
    • setCoordinateSystemReproject(CoordinateReferenceSystem) is used to ask for the retrieved features to be reproejcted.
    Vendor specific: Joins:

    The Query class supports the concepts of joins in that a query can result in a join of the feature type to other feature types in the same datastore. For example, the following would be a spatial join that selected the country that contain a particular city.

    
     Query query = new Query("countries");
     Join join = new Join("cities", CQL.toFilter("CONTAINS(geometry, b.geometry)"));
     join.setAlias("b");
     join.setFilter(CQL.toFilter("CITY_NAME = 'Canmore'"))
     query.getJoins().add(join);
     
    Example:
    
     Filter filter = CQL.toFilter("NAME like '%land'");
     Query query = new Query( "countries", filter );
    
     FeatureCollection features = featureSource.getFeatures( query );
     
    Author:
    Chris Holmes
    • Field Detail

      • INCLUDE_MANDATORY_PROPS

        public static Hints.Key INCLUDE_MANDATORY_PROPS
        When specifying properties to select, setting this hint flag true tells the datastore to include mandatory properties (i.e. properties with minOccurs >= 1) in the end result, irrespective of whether they are not included in the list of properties.

        Datastores may implement adding all mandatory properties to the end result when this flag is set to true. For example:

        Object includeProps = query.getHints().get(Query.INCLUDE_MANDATORY_PROPS); if (includeProps instanceof Boolean && ((Boolean)includeProps).booleanValue()) { query.setProperties (DataUtilities.addMandatoryProperties(type, query.getProperties())); }

      • NO_NAMESPACE

        public static final URI NO_NAMESPACE
        Constant (actually null) used to represent no namespace restrictions on the returned result, should be considered ANY_URI
      • DEFAULT_MAX

        public static final int DEFAULT_MAX
        So getMaxFeatures does not return null we use a very large number.
        See Also:
        Constant Field Values
      • ALL

        public static final Query ALL
        Implements a query that will fetch all features from a datasource. This query should retrieve all properties, with no maxFeatures, no filtering, and the default featureType.
      • FIDS

        public static final Query FIDS
        Implements a query that will fetch all the FeatureIDs from a datasource. This query should retrieve no properties, with no maxFeatures, no filtering, and the a featureType with no attribtues.
      • NO_NAMES

        public static final String[] NO_NAMES
        A constant (empty String array) that can be used with setPropertyNames(String[]) to indicate that no properties are to be retrieved.

        Note the query will still return a result - limited to FeatureIDs.

      • ALL_NAMES

        public static final String[] ALL_NAMES
        A constant (value null) that can be used with setPropertyNames(String[]) to indicate that all properties are to be retrieved.
      • maxFeatures

        protected int maxFeatures
        The maximum numbers of features to fetch
      • startIndex

        protected Integer startIndex
        The index of the first feature to process
      • filter

        protected Filter filter
        The filter to constrain the request.
      • typeName

        protected String typeName
        The typeName to get
      • alias

        protected String alias
        The optional alias for type name
      • namespace

        protected URI namespace
        The namespace to get
      • handle

        protected String handle
        The handle associated with this query.
      • coordinateSystemReproject

        protected CoordinateReferenceSystem coordinateSystemReproject
        Reprojection associated associated with this query
      • sortBy

        protected SortBy[] sortBy
        Sorting for the query
      • version

        protected String version
        The version according to WFS 1.0 and 1.1 specs
      • hints

        protected Hints hints
        The hints to be used during query execution
      • joins

        protected List<Join> joins
        join clauses for this query
    • Constructor Detail

      • Query

        public Query()
        Default constructor. Use setter methods to configure the Query before use (the default Query will retrieve all features).
      • Query

        public Query​(String typeName)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve
      • Query

        public Query​(String typeName,
                     Filter filter)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        filter - the OGC filter to constrain the request.
      • Query

        public Query​(String typeName,
                     Filter filter,
                     String... properties)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        filter - the OGC filter to constrain the request.
        properties - an array of the properties to fetch.
      • Query

        public Query​(String typeName,
                     Filter filter,
                     List<PropertyName> properties)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        filter - the OGC filter to constrain the request.
        properties - a list of the properties to fetch.
      • Query

        public Query​(String typeName,
                     Filter filter,
                     int maxFeatures,
                     String[] propNames,
                     String handle)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        filter - the OGC filter to constrain the request.
        maxFeatures - the maximum number of features to be returned.
        propNames - an array of the properties to fetch.
        handle - the name to associate with this query.
      • Query

        public Query​(String typeName,
                     Filter filter,
                     int maxFeatures,
                     List<PropertyName> properties,
                     String handle)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        filter - the OGC filter to constrain the request.
        maxFeatures - the maximum number of features to be returned.
        properties - a list of the properties to fetch.
        handle - the name to associate with this query.
      • Query

        public Query​(String typeName,
                     URI namespace,
                     Filter filter,
                     int maxFeatures,
                     String[] propNames,
                     String handle)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        namespace - Namespace for provided typeName, or null if unspecified
        filter - the OGC filter to constrain the request.
        maxFeatures - the maximum number of features to be returned.
        propNames - an array of the properties to fetch.
        handle - the name to associate with the query.
      • Query

        public Query​(String typeName,
                     URI namespace,
                     Filter filter,
                     int maxFeatures,
                     List<PropertyName> properties,
                     String handle)
        Constructor.
        Parameters:
        typeName - the name of the featureType to retrieve.
        namespace - Namespace for provided typeName, or null if unspecified
        filter - the OGC filter to constrain the request.
        maxFeatures - the maximum number of features to be returned.
        properties - a list of the property names to fetch.
        handle - the name to associate with the query.
      • Query

        public Query​(Query query)
        Copy contructor.
        Parameters:
        query - the query to copy
    • Method Detail

      • getPropertyNames

        public String[] getPropertyNames()
        Get the names of the properties that this Query will retrieve as part of the returned FeatureCollection.
        Returns:
        the attributes to be used in the returned FeatureCollection.
        See Also:
        retrieveAllProperties()
        TODO:
        REVISIT: make a FidProperties object, instead of an array size 0. I think Query.FIDS fills this role to some degree. Query.FIDS.equals( filter ) would meet this need?
      • setPropertyNames

        public void setPropertyNames​(String... propNames)
        Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection. As well as an array of names, the following constants can be used:
        • ALL_NAMES to retrieve all properties.
        • NO_NAMES to indicate no properties are required, just feature IDs.
        The available properties can be determined with FeatureSource.getSchema(). If properties that are not part of the source's schema are requested an exception will be thrown.
        Parameters:
        propNames - the names of the properties to retrieve or one of ALL_NAMES or NO_NAMES.
      • getProperties

        public List<PropertyName> getProperties()
        Get the names of the properties that this Query will retrieve values for as part of the returned FeatureCollection.
        Returns:
        the xpath expressions to be used in the returned FeatureCollection.
        See Also:
        retrieveAllProperties()
      • setProperties

        public void setProperties​(List<PropertyName> propNames)
        Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection. As well as an array of names, the following constants can be used: The available properties can be determined with FeatureSource.getSchema(). If properties that are not part of the source's schema are requested an exception will be thrown.
        Parameters:
        propNames - the names of the properties to retrieve or one of ALL_PROPERTIES or NO_PROPERTIES.
      • setPropertyNames

        public void setPropertyNames​(List<String> propNames)
        Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection.

        The available properties can be determined with FeatureSource.getSchema(). If properties that are not part of the source's schema are requested an exception will be thrown.

        Parameters:
        propNames - the names of the properties to retrieve or ALL_NAMES; an empty List can be passed in to indicate that only feature IDs should be retrieved
        TODO:
        REVISIT: This syntax is really obscure. Consider having an fid or featureID propertyName that datasource implementors look for instead of looking to see if the list size is 0.
      • retrieveAllProperties

        public boolean retrieveAllProperties()
        Convenience method to determine if the query should retrieve all properties defined in the schema of the feature data source. This is equivalent to testing if getPropertyNames() returns ALL_NAMES.
        Returns:
        true if all properties will be retrieved by this Query; false otherwise
      • getMaxFeatures

        public int getMaxFeatures()
        Get the maximum number of features that will be retrieved by this Query.

        Note: This is the only method that is not directly out of the Query element in the WFS specification. It is instead a part of a GetFeature request, which can hold one or more queries. But each of those in turn will need a maxFeatures, so it is needed here.

        If the value returned here is max integer then the number of features should not be limited.

        Returns:
        the maximum number of features that will be retrieved by this query
      • isMaxFeaturesUnlimited

        public boolean isMaxFeaturesUnlimited()
        Check if this query allows an unlimited number of features to be returned.

        Returns:
        true maxFeatures is less then zero, or equal to Integer.MAX_VALUE.
      • setMaxFeatures

        public void setMaxFeatures​(int maxFeatures)
        Sets the maximum number of features that should be retrieved by this query. The default is to retrieve all features.
        Parameters:
        maxFeatures - the maximum number of features to retrieve
      • getStartIndex

        public Integer getStartIndex()
        Get the index of the first feature to retrieve.
        Returns:
        the index of the first feature to retrieve or null if no start index is defined.
      • setStartIndex

        public void setStartIndex​(Integer startIndex)
        Set the index of the first feature to retrieve. This can be used in conjuction with setMaxFeatures(int) to 'page' through a feature data source.
        Parameters:
        startIndex - index of the first feature to retrieve or null to indicate no start index
        Throws:
        IllegalArgumentException - if startIndex is less than 0
      • getFilter

        public Filter getFilter()
        Gets the filter used to define constraints on the features that will be retrieved by this Query.
        Returns:
        The filter that defines constraints on the query.
      • setFilter

        public void setFilter​(Filter filter)
        Sets the filter to constrain the features that will be retrieved by this Query. If no filter is set all features will be retrieved (taking into account any bounds set via setMaxFeatures(int) and setStartIndex(java.lang.Integer)).

        The default is Filter.INCLUDE.

        Parameters:
        filter - the OGC filter which features must pass through to be retrieved by this Query.
      • getTypeName

        public String getTypeName()
        Get the name of the feature type to be queried.
        Returns:
        the name of the feature type to be returned with this query.
      • setTypeName

        public void setTypeName​(String typeName)
        Sets the name of the feature type to be queried. If no typename is specified, then the data source's default type will be used. When working with sources such as shapefiles that only support one feature type this method can be ignored.
        Parameters:
        typeName - the name of the featureType to retrieve.
      • getAlias

        public String getAlias()
        An alias substitutable for getTypeName().

        This value is typically used in a join query in which the join filter requires disambiguation due to property name overlaps between joined types.

        Since:
        8.0
      • setAlias

        public void setAlias​(String alias)
        Sets the type name alias.
        Since:
        8.0
        See Also:
        getAlias()
      • getNamespace

        public URI getNamespace()
        Get the namespace of the feature type to be queried.
        Returns:
        the gml namespace of the feature type to be returned with this query
      • setNamespace

        public void setNamespace​(URI namespace)
        Set the namespace of the feature type to be queried.
      • getHandle

        public String getHandle()
        Get the handle (mnemonic name) that will be associated with this Query. The handle is used in logging and error reporting.
        Returns:
        the name to refer to this query.
      • setHandle

        public void setHandle​(String handle)
        Set the handle (mnemonic name) that will be associated with this Query. The handle is used in logging and error reporting.
        Parameters:
        handle - the name to refer to this query.
      • getVersion

        public String getVersion()
        Defines version or version range requested.

        From WFS Spec: The version attribute is included in order to accommodate systems that support feature versioning. A value of ALL indicates that all versions of a feature should be fetched. Otherwise an integer, n, can be specified to return the n th version of a feature. The version numbers start at '1' which is the oldest version. If a version value larger than the largest version is specified then the latest version is return. The default action shall be for the query to return the latest version. Systems that do not support versioning can ignore the parameter and return the only version that they have.

        GeoTools employs the following options:

        Returns:
        the version of the feature to return, or null for LAST.
      • setVersion

        public void setVersion​(int index)
      • setVersion

        public void setVersion​(Date date)
      • setVersion

        public void setVersion​(Date startTime,
                               Date endTime)
      • setVersion

        public void setVersion​(ResourceId history)
      • setVersion

        public void setVersion​(String version)
        Set the version of features to retrieve where this is supported by the data source being queried.
        Since:
        2.4
        See Also:
        getVersion() for explanation
      • getCoordinateSystem

        public CoordinateReferenceSystem getCoordinateSystem()
        Get the coordinate system to use as an override for features retrieved by this Query.
        Returns:
        The coordinate system to be returned for Features from this Query (override the set coordinate system).
      • setCoordinateSystem

        public void setCoordinateSystem​(CoordinateReferenceSystem system)
        Provide an override coordinate system to apply to features retrieved by this Query.

        This denotes a request to temporarily override the coordinate system contained in the feature data source being queried. The same coordinate values will be used, but the features retrieved will appear in this Coordinate System.

        This change is not persistent and only applies to the features returned by this Query. If used in conjunction with getCoordinateSystemReproject() the reprojection will occur from getCoordinateSystem() to getCoordinateSystemReproject().

        Parameters:
        system - the coordinate system to apply to features retrieved by this Query
      • setCoordinateSystemReproject

        public void setCoordinateSystemReproject​(CoordinateReferenceSystem system)
        Request that features retrieved by this Query be reprojected into the given coordinate system.

        If used in conjunction with setCoordinateSystem(CoordinateReferenceSystem) the reprojection will occur from the overridden coordinate system to the system specified here.

      • getSortBy

        public SortBy[] getSortBy()
        SortBy results according to indicated property and order.

        SortBy is part of the Filter 1.1 specification, it is referenced by WFS1.1 and Catalog 2.0.x specifications and is used to organize results. The SortBy's are ment to be applied in order:

        • SortBy( year, ascending )
        • SortBy( month, decsending )
        Would produce something like:
        
         [year=2002 month=4],[year=2002 month=3],[year=2002 month=2],
         [year=2002 month=1],[year=2003 month=12],[year=2002 month=4],
         

        SortBy should be considered at the same level of abstraction as Filter, and like Filter you may sort using properties not listed in getPropertyNames.

        At a technical level the interface SortBy2 is used to indicate the additional requirements of a GeoTools implementation. The pure WFS 1.1 specification itself is limited to SortBy.

        Returns:
        SortBy array or order of application
      • setSortBy

        public void setSortBy​(SortBy... sortBy)
        Sets the sort by information.
      • hashCode

        public int hashCode()
        Hashcode based on all parameters other than the handle.
        Overrides:
        hashCode in class Object
        Returns:
        hascode for this Query
      • equals

        public boolean equals​(Object obj)
        Equality based on all query parameters other than the handle.
        Overrides:
        equals in class Object
        Parameters:
        obj - Other object to compare against
        Returns:
        true if this Query matches the other object; false otherwise
      • toString

        public String toString()
        Return a string representation of this Query.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this Query
      • getJoins

        public List<Join> getJoins()
        The list of joins for this query.

        Each Join object specifies a feature type to join to. The join may only reference a feature type from within the same datastore.

        See Also:
        Join