public class Query extends Object
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:
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 );
Modifier and Type | Field and Description |
---|---|
protected String |
alias
The optional alias for type name
|
static Query |
ALL
Implements a query that will fetch all features from a datasource.
|
static String[] |
ALL_NAMES
A constant (value
null ) that can be used with setPropertyNames(String[]) to indicate that all properties are to be retrieved. |
static List<PropertyName> |
ALL_PROPERTIES
A constant (value
null ) that can be used with #setProperties(Collection |
protected CoordinateReferenceSystem |
coordinateSystem
Coordinate System associated with this query
|
protected CoordinateReferenceSystem |
coordinateSystemReproject
Reprojection associated associated with this query
|
static int |
DEFAULT_MAX
So getMaxFeatures does not return null we use a very large number.
|
static Query |
FIDS
Implements a query that will fetch all the FeatureIDs from a datasource.
|
protected Filter |
filter
The filter to constrain the request.
|
protected String |
handle
The handle associated with this query.
|
protected Hints |
hints
The hints to be used during query execution
|
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.
|
protected List<Join> |
joins
join clauses for this query
|
protected int |
maxFeatures
The maximum numbers of features to fetch
|
protected URI |
namespace
The namespace to get
|
static String[] |
NO_NAMES
A constant (empty String array) that can be used with setPropertyNames(String[]) to indicate that no properties are to be retrieved.
|
static URI |
NO_NAMESPACE
Constant (actually null) used to represent no namespace restrictions on the returned result,
should be considered ANY_URI
|
static List<PropertyName> |
NO_PROPERTIES
A constant (empty String array) that can be used with #setProperties(Collection
|
protected List<PropertyName> |
properties
The properties to fetch
|
protected SortBy[] |
sortBy
Sorting for the query
|
protected Integer |
startIndex
The index of the first feature to process
|
protected String |
typeName
The typeName to get
|
protected String |
version
The version according to WFS 1.0 and 1.1 specs
|
Constructor and Description |
---|
Query()
Default constructor.
|
Query(Query query)
Copy contructor.
|
Query(String typeName)
Constructor.
|
Query(String typeName,
Filter filter)
Constructor.
|
Query(String typeName,
Filter filter,
int maxFeatures,
List<PropertyName> properties,
String handle)
Constructor.
|
Query(String typeName,
Filter filter,
int maxFeatures,
String[] propNames,
String handle)
Constructor.
|
Query(String typeName,
Filter filter,
List<PropertyName> properties)
Constructor.
|
Query(String typeName,
Filter filter,
String[] properties)
Constructor.
|
Query(String typeName,
URI namespace,
Filter filter,
int maxFeatures,
List<PropertyName> propNames,
String handle)
Constructor.
|
Query(String typeName,
URI namespace,
Filter filter,
int maxFeatures,
String[] propNames,
String handle)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Equality based on all query parameters other than the handle.
|
String |
getAlias()
An alias substitutable for
getTypeName() . |
CoordinateReferenceSystem |
getCoordinateSystem()
Get the coordinate system to use as an override for features retrieved by this Query.
|
CoordinateReferenceSystem |
getCoordinateSystemReproject()
If reprojection has been requested, this returns the coordinate system that features
retrieved by this Query will be reprojected into.
|
Filter |
getFilter()
Gets the filter used to define constraints on the features that will be retrieved by this
Query.
|
String |
getHandle()
Get the handle (mnemonic name) that will be associated with this Query.
|
Hints |
getHints()
Get hints that have been set to control the query execution.
|
List<Join> |
getJoins()
The list of joins for this query.
|
int |
getMaxFeatures()
Get the maximum number of features that will be retrieved by this Query.
|
URI |
getNamespace()
Get the namespace of the feature type to be queried.
|
List<PropertyName> |
getProperties()
Get the names of the properties that this Query will retrieve values for as part of the
returned FeatureCollection.
|
String[] |
getPropertyNames()
Get the names of the properties that this Query will retrieve as part of the returned
FeatureCollection.
|
SortBy[] |
getSortBy()
SortBy results according to indicated property and order.
|
Integer |
getStartIndex()
Get the index of the first feature to retrieve.
|
String |
getTypeName()
Get the name of the feature type to be queried.
|
String |
getVersion()
Defines version or version range requested.
|
int |
hashCode()
Hashcode based on all parameters other than the handle.
|
boolean |
isMaxFeaturesUnlimited()
Check if this query allows an unlimited number of features to be returned.
|
boolean |
retrieveAllProperties()
Convenience method to determine if the query should retrieve all properties defined in the
schema of the feature data source.
|
void |
setAlias(String alias)
Sets the type name alias.
|
void |
setCoordinateSystem(CoordinateReferenceSystem system)
Provide an override coordinate system to apply to features retrieved by this Query.
|
void |
setCoordinateSystemReproject(CoordinateReferenceSystem system)
Request that features retrieved by this Query be reprojected into the given coordinate
system.
|
void |
setFilter(Filter filter)
Sets the filter to constrain the features that will be retrieved by this Query.
|
void |
setHandle(String handle)
Set the handle (mnemonic name) that will be associated with this Query.
|
void |
setHints(Hints hints)
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(List<String> 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 |
setSortBy(SortBy[] sortBy)
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(Date date) |
void |
setVersion(Date startTime,
Date endTime) |
void |
setVersion(int index) |
void |
setVersion(ResourceId history) |
void |
setVersion(String version)
Set the version of features to retrieve where this is supported by the data source being
queried.
|
void |
setVersion(Version.Action action) |
String |
toString()
Return a string representation of this Query.
|
public static Hints.Key INCLUDE_MANDATORY_PROPS
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())); }
public static final URI NO_NAMESPACE
public static final int DEFAULT_MAX
public static final Query ALL
public static final Query FIDS
public static final String[] NO_NAMES
Note the query will still return a result - limited to FeatureIDs.
public static final String[] ALL_NAMES
null
) that can be used with setPropertyNames(String[]) to indicate that all properties are to be retrieved.public static final List<PropertyName> NO_PROPERTIES
Note the query will still return a result - limited to FeatureIDs.
public static final List<PropertyName> ALL_PROPERTIES
null
) that can be used with #setProperties(Collectionprotected List<PropertyName> properties
protected int maxFeatures
protected Integer startIndex
protected Filter filter
protected String typeName
protected String alias
protected URI namespace
protected String handle
protected CoordinateReferenceSystem coordinateSystem
protected CoordinateReferenceSystem coordinateSystemReproject
protected SortBy[] sortBy
protected String version
protected Hints hints
public Query()
public Query(String typeName)
typeName
- the name of the featureType to retrievepublic Query(String typeName, Filter filter)
typeName
- the name of the featureType to retrieve.filter
- the OGC filter to constrain the request.public Query(String typeName, Filter filter, String[] properties)
typeName
- the name of the featureType to retrieve.filter
- the OGC filter to constrain the request.properties
- an array of the properties to fetch.public Query(String typeName, Filter filter, List<PropertyName> properties)
typeName
- the name of the featureType to retrieve.filter
- the OGC filter to constrain the request.properties
- a list of the properties to fetch.public Query(String typeName, Filter filter, int maxFeatures, String[] propNames, String handle)
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.public Query(String typeName, Filter filter, int maxFeatures, List<PropertyName> properties, String handle)
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.public Query(String typeName, URI namespace, Filter filter, int maxFeatures, String[] propNames, String handle)
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.public Query(String typeName, URI namespace, Filter filter, int maxFeatures, List<PropertyName> propNames, String handle)
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.public Query(Query query)
query
- the query to copypublic String[] getPropertyNames()
retrieveAllProperties()
public void setPropertyNames(String[] propNames)
public List<PropertyName> getProperties()
retrieveAllProperties()
public void setProperties(List<PropertyName> propNames)
propNames
- the names of the properties to retrieve or one of ALL_PROPERTIES or NO_PROPERTIES.public void setPropertyNames(List<String> propNames)
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.
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 retrievedpublic boolean retrieveAllProperties()
public int getMaxFeatures()
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.
public boolean isMaxFeaturesUnlimited()
public void setMaxFeatures(int maxFeatures)
maxFeatures
- the maximum number of features to retrievepublic Integer getStartIndex()
null
if no start index is
defined.public void setStartIndex(Integer startIndex)
startIndex
- index of the first feature to retrieve or null
to indicate no start
indexIllegalArgumentException
- if startIndex is less than 0public Filter getFilter()
public void setFilter(Filter filter)
The default is Filter.INCLUDE.
filter
- the OGC filter which features must pass through to be retrieved by this Query.public String getTypeName()
public void setTypeName(String typeName)
typeName
- the name of the featureType to retrieve.public String getAlias()
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.
public void setAlias(String alias)
getAlias()
public URI getNamespace()
public void setNamespace(URI namespace)
public String getHandle()
public void setHandle(String handle)
handle
- the name to refer to this query.public String getVersion()
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"null
for LAST.public void setVersion(int index)
public void setVersion(Date date)
public void setVersion(Version.Action action)
public void setVersion(ResourceId history)
public void setVersion(String version)
version
- getVersion() for explanation
public CoordinateReferenceSystem getCoordinateSystem()
public void setCoordinateSystem(CoordinateReferenceSystem system)
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()
.
system
- the coordinate system to apply to features retrieved by this Querypublic CoordinateReferenceSystem getCoordinateSystemReproject()
setCoordinateSystemReproject( CoordinateReferenceSystem )
public void setCoordinateSystemReproject(CoordinateReferenceSystem system)
If used in conjunction with setCoordinateSystem(CoordinateReferenceSystem)
the
reprojection will occur from the overridden coordinate system to the system specified here.
public SortBy[] getSortBy()
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:
[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.
public void setSortBy(SortBy[] sortBy)
public Hints getHints()
setHints(Hints) for more explanation
public void setHints(Hints hints)
Hints can control such things as:
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.
hints
- the hints to applyHints.FEATURE_DETACHED
,
Hints.JTS_GEOMETRY_FACTORY
,
Hints.JTS_COORDINATE_SEQUENCE_FACTORY
,
Hints.JTS_PRECISION_MODEL
,
Hints.JTS_SRID
,
Hints.GEOMETRY_DISTANCE
,
Hints.FEATURE_2D
public int hashCode()
public boolean equals(Object obj)
public String toString()
Copyright © 1996–2019 Geotools. All rights reserved.