Class Query
- Direct Known Subclasses:
JoiningQuery
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.
- setHints(org.geotools.util.factory.Hints) is used to specify venfor specific capabilities provided by a feature source implementation.
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 Summary
FieldsModifier and TypeFieldDescriptionprotected String
The optional alias for type namestatic final Query
Implements a query that will fetch all features from a datasource.static final String[]
A constant (valuenull
) that can be used with setPropertyNames(String[]) to indicate that all properties are to be retrieved.static final List<PropertyName>
A constant (valuenull
) that can be used with setProperties(java.util.List<org.geotools.api.filter.expression.PropertyName>) to indicate that all properties are to be retrieved.protected CoordinateReferenceSystem
Coordinate System associated with this queryprotected CoordinateReferenceSystem
Reprojection associated associated with this querystatic final int
So getMaxFeatures does not return null we use a very large number.static final Query
Implements a query that will fetch all the FeatureIDs from a datasource.protected Filter
The filter to constrain the request.protected String
The handle associated with this query.protected Hints
The hints to be used during query executionstatic Hints.Key
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.join clauses for this queryprotected int
The maximum numbers of features to fetchprotected URI
The namespace to getstatic final String[]
A constant (empty String array) that can be used with setPropertyNames(String[]) to indicate that no properties are to be retrieved.static final URI
Constant (actually null) used to represent no namespace restrictions on the returned result, should be considered ANY_URIstatic final List<PropertyName>
A constant (empty String array) that can be used with setProperties(java.util.List<org.geotools.api.filter.expression.PropertyName>) to indicate that no properties are to be retrieved.protected List<PropertyName>
The properties to fetchprotected SortBy[]
Sorting for the queryprotected Integer
The index of the first feature to processprotected String
The typeName to getprotected String
The version according to WFS 1.0 and 1.1 specs -
Constructor Summary
ConstructorsConstructorDescriptionQuery()
Default constructor.Constructor.Query
(String typeName, URI namespace, Filter filter, int maxFeatures, String[] propNames, String handle) Constructor.Query
(String typeName, URI namespace, Filter filter, int maxFeatures, List<PropertyName> properties, String handle) Constructor.Constructor.Constructor.Query
(String typeName, Filter filter, int maxFeatures, List<PropertyName> properties, String handle) Constructor.Constructor.Query
(String typeName, Filter filter, List<PropertyName> properties) Constructor.Copy contructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Equality based on all query parameters other than the handle.getAlias()
An alias substitutable forgetTypeName()
.Get the coordinate system to use as an override for features retrieved by this Query.If reprojection has been requested, this returns the coordinate system that features retrieved by this Query will be reprojected into.Gets the filter used to define constraints on the features that will be retrieved by this Query.Get the handle (mnemonic name) that will be associated with this Query.getHints()
Get hints that have been set to control the query execution.getJoins()
The list of joins for this query.int
Get the maximum number of features that will be retrieved by this Query.Get the namespace of the feature type to be queried.Get the names of the properties that this Query will retrieve values for as part of the returned FeatureCollection.String[]
Get the names of the properties that this Query will retrieve as part of the returned FeatureCollection.SortBy[]
SortBy results according to indicated property and order.Get the index of the first feature to retrieve.Get the name of the feature type to be queried.Defines version or version range requested.int
hashCode()
Hashcode based on all parameters other than the handle.boolean
Check if this query allows an unlimited number of features to be returned.boolean
Convenience method to determine if the query should retrieve all properties defined in the schema of the feature data source.void
Sets the type name alias.void
Provide an override coordinate system to apply to features retrieved by this Query.void
Request that features retrieved by this Query be reprojected into the given coordinate system.void
Sets the filter to constrain the features that will be retrieved by this Query.void
Set the handle (mnemonic name) that will be associated with this Query.void
Set hints to control the query execution.void
setMaxFeatures
(int maxFeatures) Sets the maximum number of features that should be retrieved by this query.void
setNamespace
(URI namespace) Set the namespace of the feature type to be queried.void
setProperties
(List<PropertyName> propNames) Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection.void
setPropertyNames
(String... propNames) Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection.void
setPropertyNames
(List<String> propNames) Set the names of the properties that this Query should retrieve as part of the returned FeatureCollection.void
Sets the sort by information.void
setStartIndex
(Integer startIndex) Set the index of the first feature to retrieve.void
setTypeName
(String typeName) Sets the name of the feature type to be queried.void
setVersion
(int index) void
setVersion
(String version) Set the version of features to retrieve where this is supported by the data source being queried.void
setVersion
(Date date) void
setVersion
(Date startTime, Date endTime) void
setVersion
(ResourceId history) void
setVersion
(Version.Action action) toString()
Return a string representation of this Query.
-
Field Details
-
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
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_MAXSo getMaxFeatures does not return null we use a very large number.- See Also:
-
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
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
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
A constant (valuenull
) that can be used with setPropertyNames(String[]) to indicate that all properties are to be retrieved. -
NO_PROPERTIES
A constant (empty String array) that can be used with setProperties(java.util.List<org.geotools.api.filter.expression.PropertyName>) to indicate that no properties are to be retrieved.Note the query will still return a result - limited to FeatureIDs.
-
ALL_PROPERTIES
A constant (valuenull
) that can be used with setProperties(java.util.List<org.geotools.api.filter.expression.PropertyName>) to indicate that all properties are to be retrieved. -
properties
The properties to fetch -
maxFeatures
protected int maxFeaturesThe maximum numbers of features to fetch -
startIndex
The index of the first feature to process -
filter
The filter to constrain the request. -
typeName
The typeName to get -
alias
The optional alias for type name -
namespace
The namespace to get -
handle
The handle associated with this query. -
coordinateSystem
Coordinate System associated with this query -
coordinateSystemReproject
Reprojection associated associated with this query -
sortBy
Sorting for the query -
version
The version according to WFS 1.0 and 1.1 specs -
hints
The hints to be used during query execution -
joins
join clauses for this query
-
-
Constructor Details
-
Query
public Query()Default constructor. Use setter methods to configure the Query before use (the default Query will retrieve all features). -
Query
Constructor.- Parameters:
typeName
- the name of the featureType to retrieve
-
Query
Constructor.- Parameters:
typeName
- the name of the featureType to retrieve.filter
- the OGC filter to constrain the request.
-
Query
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
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
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 unspecifiedfilter
- 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 unspecifiedfilter
- 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
Copy contructor.- Parameters:
query
- the query to copy
-
-
Method Details
-
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:
- 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
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.
-
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:
-
setProperties
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_PROPERTIES to retrieve all properties.
- NO_PROPERTIES to indicate no properties are required, just feature IDs.
- Parameters:
propNames
- the names of the properties to retrieve or one of ALL_PROPERTIES or NO_PROPERTIES.
-
setPropertyNames
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
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
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 ornull
to indicate no start index- Throws:
IllegalArgumentException
- if startIndex is less than 0
-
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
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
Get the name of the feature type to be queried.- Returns:
- the name of the feature type to be returned with this query.
-
setTypeName
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
An alias substitutable forgetTypeName()
.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
Sets the type name alias.- Since:
- 8.0
- See Also:
-
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
Set the namespace of the feature type to be queried. -
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
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
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:
setVersion(Date)
: "date: dow mon dd hh:mm:ss zzz yyyy"setVersion(int)
: "index"setVersion(Version.Action)
: "PREVIOUS", "LAST", "NEXT", "FIRST", "ALL"setVersion(Date, Date)
: "start: dow mon dd hh:mm:ss zzz yyyy end: dow mon dd hh:mm:ss zzz yyyy"
- Returns:
- the version of the feature to return, or
null
for LAST.
-
setVersion
public void setVersion(int index) -
setVersion
-
setVersion
-
setVersion
-
setVersion
-
setVersion
Set the version of features to retrieve where this is supported by the data source being queried.- Since:
- 2.4
- See Also:
-
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
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 fromgetCoordinateSystem()
togetCoordinateSystemReproject()
.- Parameters:
system
- the coordinate system to apply to features retrieved by this Query
-
getCoordinateSystemReproject
If reprojection has been requested, this returns the coordinate system that features retrieved by this Query will be reprojected into.- Returns:
- the coordinate system that features will be reprojected into (if set)
- See Also:
-
setCoordinateSystemReproject
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
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 )
[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
Sets the sort by information. -
getHints
Get hints that have been set to control the query execution.- Returns:
- hints that are set (may be empty)
- See Also:
-
setHints
Set hints to control the query execution.Hints can control such things as:
- the GeometryFactory to be used
- a generalization distance to be applied
- the fetch size to be used in JDBC queries
Note: Data sources may ignore hints (depending on their values) and no mechanism currently exists to discover which hints where actually used during the query's execution.
- Parameters:
hints
- the hints to apply- See Also:
-
hashCode
public int hashCode()Hashcode based on all parameters other than the handle. -
equals
Equality based on all query parameters other than the handle. -
toString
Return a string representation of this Query. -
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:
-