ApsaraDB for HBase Ganos (HBase Ganos) is developed on top of GeoTools. This allows you to use a Query object to query spatio-temporal objects. You can use Common Query Language (CQL) statements to define query conditions. HBase Ganos also provides other features for data queries. For example, you can specify attribute columns to return or sort specified attribute columns. Data is returned as a SimpleFeatureCollection object. You can use an iterator to scan all SimpleFeature objects for the required data.

Define query conditions

Query conditions are also referred to as filters. You can use CQL statements to define query conditions. A CQL statement is similar with the WHERE clause in an SQL statement. You can use query conditions to filter data.

Query spatial data

The following table describes the predicates that can be used to query spatial data. In each syntax, Expression specifies a spatial object in well-known text (WKT). A spatial object can be a point, a line, or a polygon. | Syntax | Description | | -- | | INTERSECTS(Expression, Expression) | Queries whether two spatial objects intersect. For example, you can use this syntax to query whether two roads intersect. | | DISJOINT(Expression, Expression) | Queries whether two spatial objects are disjoint. | | CONTAINS(Expression, Expression) | Queries whether the first spatial object contains the second spatial object. | | WITHIN(Expression, Expression) | Queries whether the first object is within the second spatial object. WITHIN is the opposite of CONTAINS. | | TOUCHES(Expression, Expression) | Queries whether two objects touch. Two objects that touch have at least one common point. | | CROSSES(Expression, Expression) | Queries whether two spatial objects 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 the spatial object specified by [Expression] intersects with the geographic extent [Xmin, Ymin, Xmax, Ymax].

The CRS parameter is optional. A coordinate reference system (CRS) 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 table is used. |

  • Example 1: Query the spatial objects within the geographic extent (120E, 30N, 130E, 40N). The objects can be restaurants, vessels, and vehicles.
    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: Query all the vehicles within a radius of 5 kilometers.
    • Build a polygon that covers the area within 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 objects: ```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 spatio-temporal data
You can query spatial data based on the preceding description. This section describes how to query spatio-temporal data in different scenarios. For example, you may want to query the trajectories of all the vehicles within the geographic extent (20E, 30N, 130E, 40N) in the last 2 hours. HBase Ganos supports the following predicates for temporal queries. In each syntax, Expression specifies the name of a date column and Time specifies a 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 within a specified time period. |
| Expression DURING Time Period                | Queries trajectory points within a specified time period.                          |
| Expression DURING OR AFTER Time Period       | Queries trajectory points within or after a specified time period.                    |
| Expression AFTER Time                        | Queries trajectory points after a specified time period.                          |

The following table describes the time expressions supported by HBase Ganos.

| Syntax                                          | Description                                           |
| --- | --- |
| Time / Time                                   | A time period that is between the specified start time and the specified end time                 |
| Duration / Time                               | A time period that is before a specified point in time                           |
| Time / Duration                               | A time period that is after a specified point in time                           |

Example: If you want to query the trajectory data of all the vehicles located within (120E, 30N, 130E, 40N) from 2014-01-01T11:45:00 to 2014-01-01T12:15:00, perform a spatio-temporal query. You can use the following sample code:
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
  • HBase Ganos supports the following comparison operators: equals (=), not-equal-to (<>), greater-than (>), greater-than-or-equal-to (≥), less-than (<), and less-than-or-equal-to (≤). To query cities that have a population of more than 15 million, you can define PERSONS > 15000000 as the query condition. PERSONS specifies the population size.
  • You can use the BETWEEN operator to specify a range. For example, you 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 way as 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, use MALE > FEMALE to query cities in which the population of male residents is greater than the population of female residents.
  • HBase Ganos supports the following arithmetic operators: plus (+), minus (-), multiply (*), and divide (/). For example, you can use UNEMPLOY/(EMPLOYED + UNEMPLOY) > 0.07 to query all the cities whose unemployment rate is greater than 7%.
  • You can use the IN operator to specify an attribute whose values are within a specified range. You can use the IN operator in the same way as 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.
  • HBase Ganos 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 within the geographic extent (-90, 40, -60, 45).
String filter = " name = 'bob'"
val q = new Query(sft.getTypeName, ECQL.toFilter(filter))
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);            
In the preceding example, the name parameter is used as a filter. 
Specify the columns to be returned
You can configure the parameters of a Query object to specify the columns to return. The following code provides an example: 
String[] returnFields=… // Specify the columns to return.
Query query = new Query(schema, ECQL.toFilter(ecqlPredicate));
query.setPropertyNames(returnFields);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);
Specify a sorting method
You can configure the parameters of a SortBy object to sort the specified columns. The following code provides an example:

String sortField=… //Specify the columns that you want to sort.
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

SortBy[] sort = new SortBy[]{ff.sort(sortField, order)};

query.setSortBy(sort);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);