Lindorm GanosBase is developed on top of GeoTools. This allows you to use a Query object to query spatiotemporal objects. You can use Common Query Language (CQL) statements to specify query conditions. Lindorm GanosBase also provides other features for data queries. For example, you can specify attribute columns to return or sort values in a specified attribute column. Data is returned as a SimpleFeatureCollection object. You can use an iterator to traverse all SimpleFeature data and then parse the required data. This topic provides an example on how to query spatiotemporal objects. For more information, see Sample code of the GanosClient class in Getting Started.
Specify query conditions
Query conditions are also known as filters. You can use CQL to specify query conditions. A CQL statement is similar to the WHERE clause in an SQL statement. You can use query conditions to filter data.
Spatial query
The following table describes the predicates that can be used to query spatial data. In the table, Expression specifies a geometry in well-known text (WKT). A geometry can be a point, line, or polygon.
Syntax | Description |
INTERSECTS(Expression , Expression) | Queries whether two geometries intersect. For example, you can query whether two roads intersect. |
DISJOINT(Expression , Expression) | Queries whether two geometries are disjoint. |
CONTAINS(Expression , Expression) | Queries whether the first geometry contains the second geometry. |
WITHIN(Expression , Expression) | Queries whether the first geometry is within the second geometry. WITHIN is the opposite of CONTAINS. |
TOUCHES(Expression , Expression) | Queries whether two geometries touch. If two geometries touch, their boundaries intersect, but their interiors do not intersect. |
CROSSES(Expression , Expression) | Queries whether two geometries cross. The two objects are intersected but one object is not completely included in the other object. |
BBOX ( Expression , Xmin , Ymin , Xmax , Ymax [ , CRS ]) | Queries whether a geometry that is specified by Expression intersects the geographic extent [Xmin, Ymin, Xmax, Ymax]. CRS stands for Coordinate Reference Systems. The CRS parameter is optional. The value of the CRS parameter is a string that contains spatial reference system (SRS) code. For example, you can set CRS to EPSG:1234. By default, the CRS of the queried layer is used. |
Example 1: Query the geometries in the geographic extent (120E, 30N, 130E, 40N). The geometries can be restaurants, vessels, and vehicles.
// Query a DataStore.
DataStore ds = DataStoreFinder.getDataStore(params);
// Specify query conditions.
String schema = "schemaName";
String stFilter = "bbox(geom, 120,30,130,40)"
Query query = new Query(schema, ECQL.toFilter(stFilter));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);Example 2: Query the vehicles in a radius of 5 kilometers.
Build a polygon that covers the area in a radius of 5 kilometers. For example, you can set the geographic extent to (46.9 48.9, 47.1 48.9, 47.1 49.1, 46.9 49.1, 46.9 48.9).
Use the CONTAINS predicate to filter all objects.
// Query a DataStore. DataStore ds = DataStoreFinder.getDataStore(params); // Specify query conditions. String schema = "schemaName"; String stFilter = "contains('POLYGON ((46.9 48.9, 47.1 48.9, 47.1 49.1, 46.9 49.1, 46.9 48.9))', geom)" Query query = new Query(schema, ECQL.toFilter(stFilter)); // Obtain the query result. SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);
Spatiotemporal query
You can query spatial data based on the preceding description. This section describes how to query spatiotemporal data in different scenarios. For example, you want to query the trajectories of all the vehicles in the geographic extent (20E, 30N, 130E, 40N) in the last 2 hours.
Lindorm GanosBase supports the following predicates for temporal queries. In the following table, Expression specifies the name of the date column and Time specifies the date string in the UTC format.
Syntax | Description |
Expression BEFORE Time | Queries trajectory points before a specified point in time. |
Expression BEFORE OR DURING Time Period | Queries trajectory points before or in a specified time period. |
Expression DURING Time Period | Queries trajectory points in a specified time period. |
Expression DURING OR AFTER Time Period | Queries trajectory points in or after a specified time period. |
Expression AFTER Time | Queries trajectory points after a specified time period. |
Lindorm GanosBase supports various expressions for temporal queries. The following table describes the expressions.
Syntax | Description |
Time / Time | Specifies a time period that is between the specified start time and the specified end time. |
Duration / Time | Specifies a time period that is before a specified point in time. |
Time / Duration | Specifies a time period that is after a specified point in time. |
Example: If you want to query the trajectories of the vehicles that are in the geographic extent (120E, 30N, 130E, 40N) from 2014-01-01T11:45:00 to 2014-01-01T12:15:00, perform a spatiotemporal query. You can use the following sample code:
// Specify query conditions.
String schema = "schemaName";
String stFilter = "bbox(geom, 120,30,130,40) AND dtg DURING 2014-01-01T11:45:00.000Z/2014-01-01T12:15:00.000Z";
Query query = new Query(schema, ECQL.toFilter(stFilter));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);Attribute query
Lindorm GanosBase supports the following comparison operators: =, <>, >, >=, <, and <=. If you want to select a city that has a population of more than 15 million, define the query condition PERSONS > 15000000. The PERSONS field specifies the population.
You can use the BETWEEN operator to specify a range. For example, you can use the following string to specify a population range: PERSONS BETWEEN 1000000 AND 3000000.
The comparison operators support text values. You can specify a text value on the right side of the equal sign (=). For example, CITY_NAME='Beijing' specifies the city of Beijing. You can also use the LIKE operator in the same manner in which the operator is used in SQL statements. For example, CITY_NAME LIKE 'N%' specifies cities whose names start with the letter N.
You can compare two attributes. For example, you can use MALE > FEMALE to query cities in which the population of male residents is greater than the population of female residents.
Lindorm GanosBase supports the following arithmetic operators: plus (+), minus (-), multiply (*), and divide (/). For example, you can use the filter condition UNEMPLOY/(EMPLOYED + UNEMPLOY) > 0.07 to query all cities whose unemployment rate is greater than 7%.
You can use the IN operator to specify an attribute whose values are in a specified range. You can use the IN operator in the same manner in which the operator is used in SQL statements. For example, you can use ID IN ('cities.1', 'cities.12') or CITY_NAME IN ('Beijing', 'Shanghai', 'Guangzhou') to query cities whose IDs or names contain the specified values.
Lindorm GanosBase supports all the filter functions in GeoServer. For example, you can use strToLowerCase(CITY_NAME) like '%m%' to query cities whose names contain the letter M or m.
To filter geometries, you can use the bounding box (BBOX). For example, you can use BBOX(the_geom, -90, 40, -60, 45) to query cities whose geographic coordinates are in the geographic extent (-90, 40, -60, 45).
// Specify query conditions.
String schema = "schemaName";
String filter = "name = 'bob'";
Query query = new Query(schema, ECQL.toFilter(filter));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);Specify the columns that you want to return
To specify the columns that you want to return, set the parameters of the Query object. The following sample code provides an example:
// Specify the columns to be returned.
String[] returnFields={"geom"};
// Specify query conditions.
String schema = "schemaName";
String stFilter = "bbox(geom, 120,30,130,40)"
Query query = new Query(schema, ECQL.toFilter(stFilter));
query.setPropertyNames(returnFields);
// Obtain the query result set.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);Specify a sorting method
You can set the parameters of a SortBy object to specify columns based on which data is sorted. The following sample code provides an example:
String sortField="name" //Specify the column in which you want to sort values.
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SortBy[] sort = new SortBy[]{ff.sort(sortField, order)};
query.setSortBy(sort);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);