Query spatio-temporal objects
ApsaraDB for HBase Ganos (HBase Ganos) uses GeoTools Query objects and Common Query Language (CQL) statements to query spatio-temporal data. You can define query conditions, specify attribute columns to return, and sort results. Query results are returned as a SimpleFeatureCollection object that you can iterate over to retrieve individual SimpleFeature records.
Define query conditions
Query conditions, also called filters, are defined by using CQL statements. A CQL statement works like the WHERE clause in an SQL statement to filter data.
Query spatial data
Spatial queries support the following predicates. Expression represents a spatial object (a point, line, or polygon) in Well-Known Text (WKT) format.
|
Syntax |
Description |
|
INTERSECTS(Expression , Expression) |
Checks if two |
|
DISJOINT(Expression , Expression) |
Checks if two |
|
CONTAINS(Expression , Expression) |
Checks if the first |
|
WITHIN(Expression , Expression) |
Checks if the first |
|
TOUCHES(Expression , Expression) |
Checks if two |
|
CROSSES(Expression , Expression) |
Checks if two |
|
BBOX(Expression , Xmin , Ymin , Xmax , Ymax [ , CRS ]) |
Checks if the |
The optional CRS parameter specifies the coordinate reference system as a string that contains the system's code, such as 'EPSG:1234'. If omitted, the coordinate reference system of the query layer is used by default.
-
Example 1: Retrieve all
spatial objects, such as restaurants, ships, or vehicles, within the bounding box (120E, 30N, 130E, 40N).DataStore ds = DataStoreFinder.getDataStore(params); SimpleFeatureType schema=... String stFilter = "bbox(geom, 120,30,130,40)" Query query = new Query(schema, ECQL.toFilter(stFilter)); SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query); -
Example 2: Retrieve all vehicles within a 5-kilometer radius.
-
First, construct a polygon that represents the target area. For example:
(46.9 48.9, 47.1 48.9, 47.1 49.1, 46.9 49.1, 46.9 48.9). -
Then, use the
containspredicateto find all objects within that polygon.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)"
-
Spatio-temporal query
HBase Ganos supports the following predicates for time-based queries. Expression is the name of the time column, and Time is a UTC time string.
|
Syntax |
Description |
|
Expression BEFORE Time |
Finds entries before |
|
Expression BEFORE OR DURING Time Period |
Finds entries before or during |
|
Expression DURING Time Period |
Finds entries during |
|
Expression DURING OR AFTER Time Period |
Finds entries during or after |
|
Expression AFTER Time |
Finds entries after |
Time periods can be expressed in several formats:
|
Syntax |
Description |
|
Time / Time |
An interval defined by a start time and an end time. |
|
Duration / Time |
An interval that ends at |
|
Time / Duration |
An interval that starts at |
To query vehicle trajectories within the bounding box (120E, 30N, 130E, 40N) between 2014-01-01T11:45:00 and 2014-01-01T12:15:00, combine spatial and temporal conditions:
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));
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);
-
The following
comparison operators are supported:=,<>,>,>=,<, and<=. For example, to select cities with a population greater than 15 million, use the conditionPERSONS > 15000000, wherePERSONSis the population field. -
The
BETWEENoperator specifies a range. For example:PERSONS BETWEEN 1000000 AND 3000000. -
comparison operators support text values. You can specify a text value to the right of the=operator, such asCITY_NAME = 'Beijing'. You can also use theLIKEoperator, which functions similarly to its usage in SQL. For example,CITY_NAME LIKE 'N%'selects all cities whose names start with 'N'. -
You can compare two attributes. For example,
MALE > FEMALEselects cities where the male population exceeds the female population. -
You can use
arithmetic operators such as+,-,*, and/to form expressions. For example, thefilterconditionUNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07selects all cities with an unemployment rate greater than 7%. -
The
INoperator selects attributes whose value is in a given set, similar to its usage in SQL. For example,ID IN ('cities.1', 'cities.12')orCITY_NAME IN ('Beijing', 'Shanghai', 'Guangzhou'). -
You can use any filter function from GeoServer. For example,
strToLowerCase(CITY_NAME) like '%m%'finds cities with a name containing 'm', case-insensitively. -
For geometric filtering, use
BBOX. For example,BBOX(the_geom, -90, 40, -60, 45)selects cities within the specified bounding box.
String filter = " name = ‘bob’"
val q = new Query(sft.getTypeName, ECQL.toFilter(filter))
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(q); The preceding example filters the query based on the value of the name field.
### Specify columns to return
To specify which columns to return, configure the Query object's parameters.
String[] returnFields=…
Query query = new Query(schema, ECQL.toFilter(ecqlPredicate));
query.setPropertyNames(returnFields);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);### Specify a sort order
To sort the results, create a SortBy object.
String sortField=…
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SortBy[] sort = new SortBy[]{ff.sort(sortField, order)};
query.setSortBy(sort);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);