All Products
Search
Document Center

Query spatio-temporal objects

Last Updated: Jul 09, 2021

Ganos is built on top of GeoTools. This allows you to query spatio-temporal objects on ApsaraDB for Lindorm (Lindorm). You can use Common Query Language (CQL) statements to define query conditions. Ganos also provides other features for data queries. For example, you can specify an attribute column to return or sort a specific attribute column. Data is returned as a SimpleFeatureCollection. You can use an iterator to scan all SimpleFeatures and then parse the required data. This topic shows you how to query spatio-temporal objects. For more information, see Sample code of the GanosClient class in Quick start.

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 to the WHERE clause in an SQL statement. You can use query conditions to filter data.

Query spatial data

  • The following table describes the spatial predicates that can be used to query spatial data. In the table, Expression specifies a spatial object in well-known text (WKT). A spatial object can be a point, a line, or a plane.

Syntax

Description

INTERSECTS(Expression , Expression)

Queries whether two spatial objects intersect. For example, you can 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 spatial object is within the second spatial object. WITHIN is the opposite of CONTAINS.

TOUCHES(Expression , Expression)

Queries whether the following conditions are met: The boundary of a spatial object intersects that of the other spatial object. However, the interiors of the two spatial objects do not intersect.

CROSSES(Expression , Expression)

Queries whether two spatial objects cross. The intersection must contain points that are interior to both spatial objects and are not equal to one of the spatial objects.

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

Queries whether a spatial object that is specified by Expression intersects 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 layer is used.

  • Example 1: Query the spatial objects within the geographic extent (120E, 30N, 130E, 40N). The objects can be restaurants, vessels, and vehicles.

// Query a datastore.
DataStore ds = DataStoreFinder.getDataStore(params);
// Define query conditions.
String schema = "schmaName";
String stFilter = "bbox(geom, 120,30,130,40)"
Query query = new Query(schema, ECQL.toFilter(stFilter));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);
  • Example 2: Query the vehicles within a radius of 5 kilometers.

    • Construct a plane 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 query all objects.

    // Query a datastore.
    DataStore ds = DataStoreFinder.getDataStore(params);
    // Define query conditions.
    String schema = "schmaName";
    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));
    // Obtain the query result.
    SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);

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. You may want to query the trajectories of all the vehicles within the geographic extent (20E, 30N, 130E, 40N) in the last 2 hours.

Lindorm Ganos supports the following predicates for temporal queries. In the following table, Expression specifies the name of the date column and Time specifies the date string in the UTC format.

Syntax

Description

Expression BEFORE Time

Queries whether a time value is before a point in time.

Expression BEFORE OR DURING Time Period

Queries whether a time value is before or within a time period.

Expression DURING Time Period

Queries whether a time value is within a time period.

Expression DURING OR AFTER Time Period

Queries whether a time value is within or after a time period.

Expression AFTER Time

Queries whether a time value is after a point in time.

Lindorm Ganos supports various expressions for temporal queries. The following table describes the expressions.

Time / Time

The time period that is defined by the start time and the end time.

Duration / Time

The time period that is before a point in time.

Time / Duration

The time period that is after a point in time.

  • Example: If you want to query the trajectories of the vehicles that are within the geographic extent (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:

// Define query conditions.
String schema = "schmaName";
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));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);

Query attributes

  • Lindorm Ganos supports the following comparison operators: =, <>, >, >=, <, and <=. If you want to select a city that has a population of more than 15 million, define the query condition PERSONS > 15000000. PERSONS specifies the population.

  • You can use the BETWEEN operator to specify a range, for example, 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 Beijing city. You can also use the LIKE operator in the same way as that in SQL. 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 males is greater than that of females.

  • Lindorm Ganos supports the following arithmetic operators: plus (+), minus (-), multiply (*), and divide (/). For example, use UNEMPLOY/(EMPLOYED + UNEMPLOY) > 0.07 to query cities whose unemployment rate is greater than 7 percent.

  • You can use the IN operator to specify an attribute whose values are within a 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.

  • Lindorm Ganos supports all 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 geographic extent (-90, 40, -60, 45).

// Define query conditions.
String schema = "schemaName";
String filter = "name = 'bob'";
Query query = new Query(schema, ECQL.toFilter(filter));
// Obtain the query result.
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);

Specify columns to return

You can configure the parameters of a Query object to specify the columns to return. The following code provides an example:

// Specify columns to return.
String[] returnFields={"geom"};
// Define query conditions.
String schema = "schmaName";
String stFilter = "bbox(geom, 120,30,130,40)"
Query query = new Query(schema, ECQL.toFilter(stFilter));
query.setPropertyNames(returnFields);
// Obtain the query result.
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="name" //Specify 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);