Query spatio-temporal objects

Updated at:

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 spatial objects intersect, for example, whether two roads cross.

DISJOINT(Expression , Expression)

Checks if two spatial objects are disjoint, meaning they do not share any points.

CONTAINS(Expression , Expression)

Checks if the first spatial object contains the second spatial object.

WITHIN(Expression , Expression)

Checks if the first spatial object is within the second spatial object. This is the opposite of CONTAINS.

TOUCHES(Expression , Expression)

Checks if two spatial objects touch, meaning they have at least one boundary point in common but no interior points.

CROSSES(Expression , Expression)

Checks if two spatial objects cross. The objects must have some interior points in common but not be completely contained by each other.

BBOX(Expression , Xmin , Ymin , Xmax , Ymax [ , CRS ])

Checks if the spatial object specified by Expression intersects with the bounding box defined by [Xmin,Ymin,Xmax,Ymax].

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 contains predicate to 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 Time.

Expression BEFORE OR DURING Time Period

Finds entries before or during Time Period.

Expression DURING Time Period

Finds entries during Time Period.

Expression DURING OR AFTER Time Period

Finds entries during or after Time Period.

Expression AFTER Time

Finds entries after Time.

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 and extends backward for the specified Duration.

Time / Duration

An interval that starts at Time and extends forward for the specified Duration.

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);
Note
  • The following comparison operators are supported: =, <>, >, >=, <, and <=. For example, to select cities with a population greater than 15 million, use the condition PERSONS > 15000000, where PERSONS is the population field.

  • The BETWEEN operator 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 as CITY_NAME = 'Beijing'. You can also use the LIKE operator, 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 > FEMALE selects cities where the male population exceeds the female population.

  • You can use arithmetic operators such as +, -, *, and / to form expressions. For example, the filter condition UNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07 selects all cities with an unemployment rate greater than 7%.

  • The IN operator selects attributes whose value is in a given set, similar to its usage in SQL. For example, ID IN ('cities.1', 'cities.12') or CITY_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);