Querying data
To perform a spatiotemporal query using the Ganos API, first build an org.geotools.data.Query object to specify parameters such as filter conditions, the columns to return, and the sort order. Then, submit the query to a Ganos cluster through the DataStore. The query results are returned as a SimpleFeature collection.
CQL: Common Query Language
The filter conditions in a Query object use CQL query statements. Common Query Language (CQL) is a query language from the Open Geospatial Consortium (OGC) for the Catalog Web Service specification. Unlike XML-based languages, CQL uses a familiar text-based syntax that is highly readable and adaptable.
Comparison operators include =,
<>,>,>=,<, and<=. For example, to select cities with a population greater than 15,000,000, the condition isPERSONS>15000000. In this example,PERSONSis the field that stores the population count.The BETWEEN operator specifies a range. For example:
PERSONS BETWEEN 1000000 AND 3000000.Comparison operators support text values. You must specify the text value on the right side of the operator. For example, CITY_NAME='Beijing' selects the city of Beijing. The
LIKEoperator is also available and works the same as in SQL. For example,CITY_NAME LIKE 'N%'queries for all cities whose names start with 'N'.You can compare two properties. For example,
MALE > FEMALEqueries for cities where the male population is greater than the female population.Arithmetic expressions can be constructed using
+,-,*, and/. For example, the filter conditionUNEMPLOY/(EMPLOYED + UNEMPLOY) > 0.07queries for all cities with an unemployment rate greater than 7%.You can use the IN operator to select properties within a given set of values. This works the same as in SQL, such as 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 that have 'm' in their name, regardless of the case.
For geometric filtering, you can use BBOX. For example, BBOX(the_geom, -90, 40, -60, 45) selects cities within the bounding box (-90, 40, -60, 45). For more information, see the ECQL Reference.
Spatial relationship queries
The spatial relationship query predicates defined in CQL are listed in the following table:
Syntax | Description |
INTERSECTS(Expression , Expression) | Checks if two spatial features intersect. |
DISJOINT(Expression , Expression) | Checks if two spatial features are disjoint. |
CONTAINS(Expression , Expression) | Checks if the first feature topologically contains the second feature. |
WITHIN(Expression , Expression) | Checks if the first feature is topologically within the second feature. |
TOUCHES(Expression , Expression) | Checks if two features touch, which means they have at least one point in common. |
CROSSES(Expression , Expression) | Checks if two spatial features cross, which means they have some but not all interior points in common. |
EQUALS(Expression , Expression) | Checks if two spatial features are equal. |
BBOX ( Expression , Number , Number , Number , Number [ , CRS ]) | Tests if a spatial feature intersects with a bounding box specified by its minimum and maximum X and Y values. The optional CRS is a string that contains a Spatial Reference System (SRS) code, such as 'EPSG:1234'. By default, the CRS of the query layer is used. |
For example, to retrieve all features 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); Alternatively, to retrieve all elements within the polygon constructed from the points (46.9 48.9, 47.1 48.9, 47.1 49.1, 46.9 49.1, 46.9 48.9):
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));Spatiotemporal queries
HBase Ganos supports the following temporal query predicates:
Syntax | Syntax |
Expression BEFORE Time | The expression occurs before the specified time. |
Expression BEFORE OR DURING Time Period | The expression occurs before or during the specified time period. |
Expression DURING Time Period | The time falls within the Time Period. |
Expression DURING OR AFTER Time Period | The expression occurs during or after the specified time period. |
Expression AFTER Time | The time occurs after the time period. |
HBase Ganos supports multiple ways to express time:
Syntax | Description |
Time / Time | An interval defined by a start time and an end time. |
Duration / Time | The time range before the specified time. |
Time / Duration | A time interval that starts at Time. |
HBase Ganos does not currently support standalone temporal queries. Temporal queries must be combined with spatial queries.
To query for features that are located within (120E, 30N, 130E, 40N) and have a timestamp between 2014-01-01T11:45:00 and 2014-01-01T12:15:00:
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); Property queries
After creating an index for a property field, you can perform a property query on that column:
String filter = " name = 'bob'";
val q = new Query(sft.getTypeName, ECQL.toFilter(filter));
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query); The preceding example applies a filter condition to the value of the name field.
Specify columns to return
You can specify which columns to return by configuring the parameters of the Query object.
String[] returnFields=... // Specify the field names to return
Query query = new Query(schema, ECQL.toFilter(ecqlPredicate));
query.setPropertyNames(returnFields);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query); Specify the sort order
You can use the SortBy object parameter to specify which columns to return.
String sortField=... // Specify the field name for sorting
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SortBy[] sort = new SortBy[]{ff.sort(sortField, order)};
query.setSortBy(sort);
SimpleFeatureCollection features=ds.getFeatureSource(schema).getFeatures(query);