Leaflet wms filter

1,355 views
Skip to first unread message

David Alvey

unread,
May 31, 2013, 9:07:00 AM5/31/13
to leafl...@googlegroups.com
I need to be able to filter the results from a wms request so rather than showing the full A road network I could show the B5235 for example.
In the geoserver url you use: Link to example

How do I do this in leaflet?

Any ideas folks?

Bryan McBride

unread,
Jun 1, 2013, 1:17:12 AM6/1/13
to leafl...@googlegroups.com
You should be able to just append a GeoServer cql_filter to one of the existing options like so:

    layers: "Base_Map:Strategy%20-%20B%20Roads",
    format: "image/png&cql_filter=NUMBER0='B5235'",
    transparent: true
});

Kind of a hack, but it should work.

BRYAN

David Alvey

unread,
Jun 3, 2013, 4:49:57 AM6/3/13
to leafl...@googlegroups.com
You are a star!

Had to change it a bit as could not get that quiet working but with a bit of modification I got it working.

var FilterLayer = new L.TileLayer.WMS("http://wms2.derbyshire.gov.uk/wms", {
            layers: "Base_Map:Strategy%20-%20B%20Roads",
            format: "image/png&filter=<PropertyIsEqualTo><PropertyName>NUMBER0</PropertyName><Literal>B5053</Literal></PropertyIsEqualTo>",
            transparent: true
        });

Example

Thanks again!

Neil

unread,
Jun 3, 2013, 12:20:48 PM6/3/13
to leafl...@googlegroups.com
Hi,

There is a slightly less hacky way of doing the same thing which is a bit more manageable.
Once you have your Geoserver WMS layer inititalised you can use:

var cqlFilter = "NUMBER0%3D'B5235>"

FilterLayer.setParams({CQL_FILTER:cqlFilter })

This way, you can dynamically update the filter and apply it to the layer as and when you need to update the layer selection.

There is often a bit of faffing around making sure that you have all your URL components encoded correctly (e.g. representing white space as "%20" and equals as "%3D", but once you have a working pattern, it's very helpful.
Reply all
Reply to author
Forward
0 new messages