summaryrefslogtreecommitdiff
path: root/misc/openlayers/examples/cql-format.js
blob: 9b4a21072d6c3162f6b597219f43ee12ae6b75dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

// use a CQL parser for easy filter creation
var format = new OpenLayers.Format.CQL();

// this rule will get a filter from the CQL text in the form
var rule = new OpenLayers.Rule({
    // We could also set a filter here.  E.g.
    // filter: format.read("STATE_ABBR >= 'B' AND STATE_ABBR <= 'O'"),
    symbolizer: {
        fillColor: "#ff0000",
        strokeColor: "#ffcccc",
        fillOpacity: "0.5"
    }    
});

var states = new OpenLayers.Layer.Vector("States", {
    styleMap: new OpenLayers.StyleMap({
        "default": new OpenLayers.Style(null, {rules: [rule]})
    })
});

var map = new OpenLayers.Map({
    div: "map",
    layers: [
        new OpenLayers.Layer.WMS(
            "OpenLayers WMS",
            "http://maps.opengeo.org/geowebcache/service/wms",
            {layers: "openstreetmap", format: "image/png"}
        ),
        states
    ],
    center: new OpenLayers.LonLat(-101, 39),
    zoom: 3
});

// called when features are fetched
function loadFeatures(data) {
    var features = new OpenLayers.Format.GeoJSON().read(data);
    states.addFeatures(features);
}

// update filter and redraw when form is submitted
var cql = document.getElementById("cql");
var output = document.getElementById("output");
function updateFilter() {
    var filter;
    try {
        filter = format.read(cql.value);
    } catch (err) {
        output.value = err.message;
    }
    if (filter) {
        output.value = "";
        rule.filter = filter;
        states.redraw();
    }
    return false;
}
updateFilter();
var form = document.getElementById("cql_form");
form.onsubmit = updateFilter;