Table of Contents
Filter
Each Style-Rule can optionally have a Filter attached. Mapnik walks through all Rules of a Style and checks if it has a Filter specified and if this Filter matches the Object currently rendered. Filters compare a Feature's attributes against the specified rules. When the Datasource is a Postgis Database, the Filter operates on the tables columns, for Shapefiles the dbf columns are used.
In XML character entities are used to construct filters. You can use the following characters to specify value-comparisons:
- Greater Than:
>
- Greater Than or Equal:
>=
- Less Than:
<
- Less Than or Equal:
<=
- Equal:
=
Filters can be combined with the following operators:
- A
and
B - A
or
B not
A
And they can be combined in complex rules using brackets: (
and )
.
Attributes can be compared against Regular expressions using the .match
operator.
Examples in XML
Matches all objects that have an attribute "amenity" with a value of "restaurant":
<Filter>[amenity] = 'restaurant'</Filter>
Match if a value is NULL:
<Filter>[amenity] = 'restaurant' and not ([name] = null)</Filter>
Also, doing modulo is possible:
<Filter>[height] % 50 = 0</Filter>
NEW in Mapnik 2.1.x: Matches all features that contain point geometries:
<Filter>[mapnik::geometry_type]=point</Filter>
Note: the geometry types that can be matched include: point
,linestring
,polygon
, or collection
(multiple different types per feature).
Matches all Objects that have an attribute "CARTO" with a value that compares greater or equal 2 and lower then 5:
<Filter>[CARTO] >= 2 and [CARTO] < 5</Filter>
Matches all Objects that have an attribute "waterway" with a value of "canal" a) without a "tunnel" attribute or b) with a "tunnel" attribute that has a value different from "yes" and "true".
<Filter>[waterway] = 'canal' and not ([tunnel] = 'yes' or [tunnel] = 'true')</Filter>
Example using an Regular expression, matching all Objects with an attribute "place" with a value of "town" and an attribute "population" with a value consisting of exactly 5 characters where the first one is one of 5, 6, 7 or 8 and the remaining 4 characters are digits.
<Filter>[place] = 'town' and [population].match('[5-9]\d\d\d\d')</Filter>
Examples in Python
In python filters can be set using the following syntax:
f = Filter("[name] = 'value'")
See also
- The OpenStreetMap Stylesheet which uses Filters in many ways.
- The special Filters ElseFilter and AlsoFilter