OGC filter is a standardized language to filter features based on one or more conditions. This quick start offers examples of the most common use cases for working with OGC filters. For more detail about the concepts and API, see The OGC filter model.
Querying features from a model
The most common use case for OGC filter is to query features from an ILcdModel
using a filter condition.
You can use such a filter condition to select model features based on their properties and their spatial and temporal characteristics.
This example illustrates how you can set up a filter condition to query just the roads of the type highway
from a model containing road features:
//Create an OGC filter condition that checks whether a property equals a value.
//The code relies on a static import of TLcdOGCFilterFactory.
//import static com.luciad.ogc.filter.model.TLcdOGCFilterFactory.*;
ILcdOGCCondition condition = eq(property("roadType"), literal("highway"));
//Query the model with the OGC filter condition.
Stream<Object> roadFeatures = roadFeaturesModel.query(ILcdModel.filter(condition));
The example relies on TLcdOGCFilterFactory
, a convenience class that you can use to create common filter conditions quickly.
You can also create filter conditions by relying on the classes in the com.luciad.ogc.filter.model
package that implement ILcdOGCCondition
.
To ensure high-performance data access, filtering must happen as close as possible to the data source. As such, LuciadLightspeed takes these steps in its filtering logic for feature models:
|
Limiting the amount of visualized data
When you are visualizing features on a map, you can permanently set filter conditions on
the visualized layers and models. You do so by defining a TLcdModelQueryConfiguration
or by using an SLD styling file. You can find more information about this use case in the Dealing with large vector data sets article.
Evaluating single features for a condition
You can use OGC filter to evaluate whether a single feature meets a filter condition.
The first step is to create a TLcdOGCFilter
for the desired filter condition. This example shows
how you can evaluate a single road feature for the highway
type condition, as opposed to the evaluation of all model features in Querying features from a model:
//Create an OGC filter condition that checks whether a property equals a value.
ILcdOGCCondition condition = eq(property("roadType"), literal("highway"));
//Create an OGC filter for the condition.
TLcdOGCFilter filter = new TLcdOGCFilter(condition);
The TLcdOGCFilter
instance contains the definition of the filter only. The second step is to create an ILcdFilter
for the OGC filter, and use it to test whether a feature matches the OGC filter:
ILcdFilter filter = ogcFilter.asPredicate(new TLcdOGCFilterContext());
boolean accepted = filter.accept(roadFeature);
Persisting OGC filters
Next to using OGC filters to query and evaluate features, you can also persist OGC filters in XML. The OGC filter language includes a standardized XML format for this.
To encode an OGC filter to an XML file, you can use TLcdOGCFilterEncoder
:
TLcdOGCFilter filter = ...;
TLcdOGCFilterEncoder encoder = new TLcdOGCFilterEncoder();
String outputFile = "path/to/file.xml";
encoder.encode(filter, outputFile);
You can also store the XML representation into a String
or an OutputStream
:
TLcdOGCFilter
into an OutputStream
TLcdOGCFilter filter = ...;
try(Outputstream os = ...){
new TLcdOGCFilterEncoder().encode(filter, os);
}
TLcdOGCFilter
into a String
TLcdOGCFilter filter = ...;
String xmlEncodedFilter = TLcdOGCFilterEncoder.encodeToString(filter);
To decode an OGC filter from an XML file, you can use TLcdOGCFilterDecoder
:
TLcdOGCFilterDecoder decoder = new TLcdOGCFilterDecoder();
TLcdOGCFilter filter = (TLcdOGCFilter) decoder.decode(filterSourceName);
return filter;
If the filter XML isn’t stored in a file, you can either decode it directly from an InputStream
, or use the static utility method on TLcdOGCFilterDecoder
to decode directly from a String
.
For example:
String
String filterAsXML = ...;
TLcdOGCFilter filter = TLcdOGCFilterDecoder.decodeFromString(filterAsXML);
InputStream
try(InputStream is = createInputStream()){
TLcdOGCFilter filter = (TLcdOGCFilter) new TLcdOGCFilterDecoder().decode(is);
}
Customizing OGC filters
The OGC filter language comes with a set of predefined filter conditions that compare the properties of objects, and their spatial and temporal characteristics. You can also create your own filter condition, by defining a custom filter function. You can find more information about this use case in the How to add a custom function to OGC filter article.