To use the HBase Ganos API to perform spatio-temporal queries, you must first create an org.geotools.data.Query object. Then, specify the filter conditions, returned columns, and sorting parameters, and use DataStore to submit queries to your HBase Ganos cluster. The result is returned as a SimpleFeature collection.

You can use Common Query Language (CQL) statements to specify conditions in the Query object. CQL is a query language provided by Open Geospatial Consortium (OGC) for the Catalogue Web Services specification. Compared with XML-based coding languages, CQL is encoded in text syntax that we are more familiar with and provides better readability and adaptability.

1.Common Query Language

You can use Common Query Language (CQL) statements to specify conditions in the Query object. CQL is a query language provided by Open Geospatial Consortium (OGC) for the Catalogue Web Services specification. Compared with XML-based coding languages, CQL is encoded in text syntax that we are more familiar with and provides better readability and adaptability.

  • The comparison operators include: equals (=), not-equal-to (<>), greater-than (>), greater-than-or-equal-to (≥), less-than (<), and less-than-or-equal-to (≤). To query a city with a population greater than 15 million, you can use the PERSONS > 15000000 condition. The PERSONS column specifies the population size.
  • The BETWEEN operator is used to specify a range. For example, PERSONS BETWEEN 1000000 AND 3000000.
  • The comparison operators support values of the STRING type. You can specify a STRING value on the right side of the equal sign (=) operator. For example, use the CITY_NAME = 'Beijing' statement to query the city whose name is Beijing. You can also use the LIKE operator, which is used in the same way as the LIKE operator in SQL. For example, use the CITY_NAME LIKE 'N%' statement to query all cities whose names start with the letter N.
  • Compare two properties. For example, use MALE > FEMALE to query a city where the number of male residents is greater than female residents.
  • Arithmetic operators include: plus (+), minus (-), multiply (*), and divide (/). For example, you can use the UNEMPLOY / (EMPLOYED + UNEMPLOY) > 0.07 condition to query all cities whose unemployment rate is greater than 7%.
  • You can use the IN operator to specify properties whose values are within the specified range. The IN operator is used in the same way as the IN operator in SQL. For example, specify the ID IN ('cities.1', 'cities.12') condition, or use the CITY_NAME IN ('Beijing', 'Shanghai', 'Guangzhou') statement to query cities whose names are within the specified values.
  • You can use all filter functions in Geoserver. For example, use the strToLowerCase (CITY_NAME) like '%m%' statement to convert the city names to all lowercase and then query all cities whose names contain the letter m. In this example, the letter M is not case-sensitive.
  • Use the bounding box (BBOX) for the geometric filter. For example, execute the BBOX (the_geom, -90, 40, -60, 45) statement to select cities whose geographic coordinates are within the spatial range of (-90, 40, -60, 45). For more information about CQL, visit ECQL Reference.

2. Query spatial relationships

The following table lists the predicates defined in CQL that are used to query spatial relationships:

Syntax Description
INTERSECTS(Expression , Expression) Evaluates whether two spatial features intersect.
DISJOINT(Expression , Expression) Evaluates whether two spatial features are disjoint.
CONTAINS(Expression , Expression) Evaluates whether the first feature topologically contains the second feature.
WITHIN(Expression , Expression) Evaluates whether the first feature is topologically contained in the second feature.
TOUCHES(Expression , Expression) Evaluates whether two features touch. Features touch if they have at least one point in common.
CROSSES(Expression , Expression) Evaluates whether two spatial features cross. Features cross if they have some but not all interior points in common.
EQUALS(Expression , Expression) Evaluates whether two spatial features are topologically equal.
BBOX ( Expression , Number , Number , Number , Number [ , CRS ]) Tests whether a spatial feature intersects a bounding box specified by its minimum and maximum X and Y values. The optional CRS is a string that contains an SRS code. Example: 'EPSG:1234'. The default value is the CRS of the queried layer.

For example, to obtain all features located in the spatial range (120E, 30N, 130E, 40N), run the following command:

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);
		

Or to obtain all elements in the polygon built by (46.9 48.9, 47.1 48.9, 47.1 49.1, 46.9 49.1, 46.9 48.9), run the following command:

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));
		

3. Spatio-temporal queries

The following table lists the temporal predicates supported by HBase Ganos.

Syntax Description
Expression BEFORE Time Evaluates whether a time value is before a point in time.
Expression BEFORE OR DURING Time Period Evaluates whether a time value is before or within a time period.
Expression DURING Time Period Evaluates whether a time value is within a time period.
Expression DURING OR AFTER Time Period Evaluates whether a time value is within or after a time period.
Expression AFTER Time Evaluates whether a time value is after a point in time.

The following table lists the formats of time period supported by HBase Ganos.

Syntax Description
Time / Time Specifies the period defined by a beginning time and end time.
Duration / Time Specifies the period before a given time.
Time / Duration Specifies the period after a given time.

Note: HBase Ganos does not support queries that contain only time conditions. You must perform spatio-temporal queries.

For example, if you want to query all features located in (120E, 30N, 130E, 40N), with the time between 2014-01-01T11:45:00 and 2014-01-01T12:15:00, run the following command:

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);
			

4. Property queries

After you create an index on a property column, you can run the following command to perform a property query on this column.

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 value of the name column is limited.

5. Specify returned columns

You can specify parameters of the Query object to specify columns to be returned. For example:

String[] returnFields=... // Specify columns to be returned.
 Query query = new Query(schema, ECQL.toFilter(ecqlPredicate));
 query.setPropertyNames(returnFields);
 SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);

6. Specify the sorting order

You can create a SortBy object to specify columns to be returned. For example:

String sortField=... // Specify columns for sorting.
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SortBy[] sort = new SortBy[]{ff.sort(sortField, order)};
query.setSortBy(sort);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);