Each Ganos query is encapsulated in a Query object of GeoTools. Query conditions in a Query are constructed by using Common Query Language (CQL) statements. The Query object also supports other features. For example, you can use it to return a specified attribute column or sort the order of a specified attribute. Data is returned as a SimpleFeatureCollection. You can use an iterator to scan all SimpleFeatures and then parse the requested data. The following sections describe how to query spatio-temporal objects. For more information, see the GanosClient class of the sample code in the Quick start topic.

Construct query conditions

Query conditions are the Common Query Language (CQL) representation of filters. CQL is similar to the WHERE clause in SQL. You can use query conditions to filter data.

Spatial queries

  • Spatial queries support the following predicates. "Expression" in the following table represents a geometry in the WKT representation, which can be a point, line, or plane. | Syntax | Description | | --- | --- | | INTERSECTS(Expression , Expression) | Indicates whether two geometries are intersected.For example, it can indicate whether two roads are intersected. | | DISJOINT(Expression , Expression) | Indicates whether two geometries are disjoint. | | CONTAINS(Expression , Expression) | Indicates whether the first geometry contains the second geometry. | | WITHIN(Expression , Expression) | It is the contrary of the CONTAINS predicate. It indicates whether the first geometry is within the second geometry. | | TOUCHES(Expression , Expression) | Indicates whether two geometries are touched. If they are touched, they have at least one touch point. | | CROSSES(Expression , Expression) | Indicates whether two geometries cross each other. The two geometries cross each other if only a part of them are in common. | | BBOX ( Expression , Xmin , Ymin , Xmax , Ymax [ , CRS ]) | Indicates whether the geometry [Expression] is intersected by the quadrilateral plane [Xmin,Ymin,Xmax,Ymax].

A coordinate reference system (CRS) is a string that contains spatial reference system (SRS) code. For example, 'EPSG:1234'. By default, the CRS of the queried graph is used. |

  • Example 1: Query all geometries, such as restaurants, vessels, and vehicles within the spatial range (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: Query all vehicles within a radius of 5 kilometers.

    • First construct a geometry (plane) within a radius of 5 kilometers, for example, (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 retrieve all 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 query = new Query(schema, ECQL.toFilter(stFilter));
			

Spatio-temporal queries

Spatio-temporal queries are also widely used in different scenarios. For example, you may want to query the trajectories of all vehicles within the range of (20E, 30N, 130E, 40N) in the last two hours. Temporal queries in HBase Ganos support the following predicates. "Expression" in the following table specifies the name of the date column. "Time" specifies the date string in UTC format.

Syntax Description
Expression BEFORE Time Indicates any date earlier than "Time".
Expression BEFORE OR DURING Time Period Indicates any date earlier than "Time Period" or within "Time Period".
Expression DURING Time Period Indicates any date within "Time Period".
Expression DURING OR AFTER Time Period Indicates any date within "Time Period" or later than "Time Period".
Expression AFTER Time Indicates any date later than "Time".

HBase Ganos allows you to use the following syntax to specify a time period:

Syntax Description
Time / Time A time period defined by the beginning time and end time.
Duration / Time A time period earlier than "Time".
Time / Duration A time period later than "Time".
  • Example: If you want to query all vehicle trajectories located within (120E, 30N, 130E, 40N) from 2014-01-01T11:45:00 to 2014-01-01T12:15:00, you must use a combination of spatial queries and temporal queries:

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

Attribute queries

  • Supported comparison operators are =, <>, >, >=, <, and <=. If you want to select a city with a population greater than 15 million, set the query condition to PERSONS>15000000. PERSONS represents the population field.
  • BETWEEN is used to specify a specific range, for example, PERSONS BETWEEN 1000000 AND 3000000.
  • The comparison operators support string values. You can specify a string value on the right side of the equal sign (=). For example, CITY_NAME='Beijing' specifies the Beijing city. You can also use the LIKE operator. For example, CITY_NAME LIKE 'N%' specifies cities whose names start with the letter "N".
  • You can compare two attributes. For example, MALE > FEMALE specifies cities where the population of males is greater than females.
  • Supported arithmetic operators are +, -, *, and /. For example, UNEMPLOY/(EMPLOYED + UNEMPLOY) > 0.07 specifies all cities with an unemployment rate greater than 7%.
  • You can use the IN operator to specify attributes whose values are within the specified range. For example, use ID IN ('cities.1', 'cities.12') or CITY_NAME IN ('Beijing', 'Shanghai', 'Guangzhou') to query cities whose names contain the specified values.
  • You can use any 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".
  • You can use the geometric filter bounding box (BBOX). For example, use BBOX(the_geom, -90, 40, -60, 45) to query cities whose geographic coordinates are within the spatial rage of (-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 values in the name column are filtered.

Specify the names of the returned columns

You can configure the parameters of the Query object to specify the columns to be returned. Example:

String[] returnFields=... //The names of the returned columns.

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 the SortBy object to specify the columns to be sorted. Example:

String sortField=... //Specify the names of the columns to be sorted.

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

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

query.setSortBy(sort);

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