OpenLayers Blog

All the maps that are fit to blog

CQL Parsing

January 31st, 2011 by Tim Schaub · 3 Comments

When using rule based styling or constructing queries for features, application developers often hand-code their filters. For example, if you wanted to style or select a subset of US State features that started with the letters B through O, you might create a filter that looked like this:


var filter = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.AND,
    filters: [
        new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
            property: "STATE_ABBR",
            value: "B"
        }),
        new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO,
            property: "STATE_ABBR",
            value: "O"
        }),
    ]
});

Using the OGC Common Query Language, this same filter would be expressed with the following string:

STATE_ABBR >= 'B' AND STATE_ABBR <= 'O'

With the new OpenLayers.Format.CQL parser, you can now convert from one to the other. This makes hand-coded rule based styling far easier to manage, and it provides a way to concisely serialize filters for services that support CQL. See the OGC Catalog Specification for more detail on CQL, and find some good examples in the GeoServer documentation.

Thanks to David Winslow for this excellent contribution.

Tags: Features

3 responses so far ↓

  • 1 Twitter Trackbacks for CQL Parsing [openlayers.org] on Topsy.com // Jan 31, 2011 at 6:04 pm

    [...] CQL Parsing openlayers.org/blog/2011/01/31/cql-parsing/ – view page – cached When using rule based styling or constructing queries for features, application developers often hand-code their filters. For example, if you wanted to style or select a subset of US State features that started with the letters B through O, you might create a filter that looked like this: var filter = new OpenLayers.Filter.Logical({ type: OpenLayers.Filter.Logical.AND, filters: [ new... Read moreWhen using rule based styling or constructing queries for features, application developers often hand-code their filters. For example, if you wanted to style or select a subset of US State features that started with the letters B through O, you might create a filter that looked like this: var filter = new OpenLayers.Filter.Logical({ type: OpenLayers.Filter.Logical.AND, filters: [ new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO, property: "STATE_ABBR", value: "B" View page [...]

  • 2 Ian Turton // Jan 31, 2011 at 9:36 pm

    There is a typo in the example file, it says /cql-filter.js instead of /cql-format.js. Looks really good though.

  • 3 Tim Schaub // Feb 1, 2011 at 12:12 pm

    Thanks Ian. Fixed now.

Leave a Comment