Skip to contents

Filtering by area answers “give me this layer, but only within that area”. This article is about the other kind of question: “give me only the features with these properties” — the addresses with one postal code, the streets whose name starts with “Lange”, the thousands of buildings whose id appears in your own data. Attribute filters run on the PDOK server, so only the matching features are downloaded — often the difference between transferring a few rows and a few hundred thousand.

Server-side filtering follows the OGC CQL2 standard, which PDOK offers on every dataset with an OGC API Features service. It was introduced on the BAG first (announced on the PDOK Geoforum) and has since been enabled across the platform. The server rejects a filter it cannot handle with a clear error — it never silently returns unfiltered data.

library(pdokr)
library(tmap)
#> Registered S3 method overwritten by 'stars':
#>   method                  from
#>   st_interpolate_aw.stars sf

Which fields can you filter on?

Each layer publishes its queryables: the fields a server-side filter may reference. pdok_list_queryables() retrieves them for one layer; pdok_list_layers(dataset, queryables = TRUE) adds them as a column for every layer of a dataset at once.

pdok_list_queryables("kadaster/bag", "adres")
#> # A tibble: 11 × 3
#>    queryable                     type           description                     
#>    <chr>                         <chr>          <chr>                           
#>  1 bronhouder_identificatie      string         Filter features by this property
#>  2 geometry                      geometry-point NA                              
#>  3 huisnummer                    string         Filter features by this property
#>  4 identificatie                 string         Filter features by this property
#>  5 openbare_ruimte_identificatie string         Filter features by this property
#>  6 openbare_ruimte_naam          string         Filter features by this property
#>  7 postcode                      string         Filter features by this property
#>  8 provincie_naam                string         Filter features by this property
#>  9 status                        string         Filter features by this property
#> 10 woonplaats_identificatie      string         Filter features by this property
#> 11 woonplaats_naam               string         Filter features by this property

The queryables are chosen per layer and are often a small subset of the layer’s attributes. The generalized municipalities of CBS, for example, can be filtered on the year but not on the municipality name:

pdok_list_queryables("cbs/gebiedsindelingen", "gemeente_gegeneraliseerd")
#> # A tibble: 2 × 3
#>   queryable type                  description                     
#>   <chr>     <chr>                 <chr>                           
#> 1 geometry  geometry-multipolygon NA                              
#> 2 jaarcode  integer               Filter features by this property

A filter on any other field is rejected, with the server’s explanation and a pointer back to the queryables:

pdok_read(
  "cbs/gebiedsindelingen", "gemeente_gegeneraliseerd",
  filter = pdok_where(statnaam == "Utrecht")
)
#> Error in `pdok_read()`:
#> ! PDOK request failed (HTTP 400).
#> ✖ Server message: failed to parse CQL filter: error: property 'statnaam' cannot
#>   be used in CQL filter, is not a queryable property
#> ℹ See `pdok_list_queryables()` for the fields this layer can be filtered on.
#> ℹ URL:
#>   <https://api.pdok.nl/cbs/gebiedsindelingen/ogc/v1/collections/gemeente_gegeneraliseerd/items?f=json&limit=1000&filter=statnaam%20%3D%20%27Utrecht%27>

So check the queryables first. For a field that is not queryable, load the layer (within an area, if it is large) and subset the sf in R as usual.

Filter with R syntax: pdok_where()

pdok_where() translates a familiar R expression into the CQL2 filter language for you. Field names are written as bare names; values come from your R session, so variables and vectors work as usual.

pdok_read(
  "kadaster/bag", "adres",
  filter = pdok_where(postcode == "3811MZ")
)
#> Simple feature collection with 5 features and 23 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 5.378649 ymin: 52.15402 xmax: 5.378716 ymax: 52.15404
#> Geodetic CRS:  WGS 84
#> # A tibble: 5 × 24
#>   id        adresseerbaar_object…¹ adresseerbaar_object…² bronhouder_identific…³
#> * <chr>     <chr>                  <chr>                  <chr>                 
#> 1 4453af23… 0307010000596802       Verblijfsobject        0307                  
#> 2 b0a6f64f… 0307010000550915       Verblijfsobject        0307                  
#> 3 87c2a9c9… 0307010000550917       Verblijfsobject        0307                  
#> 4 35afd5a9… 0307010000606107       Verblijfsobject        0307                  
#> 5 7362cca1… 0307010000445569       Verblijfsobject        0307                  
#> # ℹ abbreviated names: ¹​adresseerbaar_object_identificatie,
#> #   ²​adresseerbaar_object_type, ³​bronhouder_identificatie
#> # ℹ 20 more variables: bronhouder_naam <chr>, huisletter <chr>,
#> #   huisnummer <chr>, identificatie <chr>, openbare_ruimte_identificatie <chr>,
#> #   openbare_ruimte_naam <chr>, openbare_ruimte_naam_kort <chr>,
#> #   openbare_ruimte_rdf_seealso <chr>, openbare_ruimte_status <chr>,
#> #   postcode <chr>, provincie_afkorting <chr>, provincie_naam <chr>, …

The same works on every dataset. The CBS neighborhoods, for instance, are queryable by municipality name, which fetches exactly one municipality’s neighborhoods without any spatial filtering:

buurten <- pdok_read(
  "cbs/wijken-en-buurten-2025", "buurten",
  filter = pdok_where(gemeentenaam == "Amersfoort")
)
nrow(buurten)
#> [1] 145

Conditions combine with &, | and !, and %in% matches against a vector:

codes <- c("3811MZ", "3811MS")

addresses <- pdok_read(
  "kadaster/bag", "adres",
  filter = pdok_where(postcode %in% codes & huisnummer != "1")
)
nrow(addresses)
#> [1] 24

like() matches string patterns (% is the wildcard), and casei() makes a comparison case-insensitive:

lange <- pdok_read(
  "kadaster/bag", "adres",
  filter = pdok_where(
    casei(woonplaats_naam) == "amersfoort" &
      like(openbare_ruimte_naam, "Lange%")
  )
)
sort(unique(lange$openbare_ruimte_naam))
#> [1] "Lange Beekstraat" "Langegracht"      "Langestraat"
tmap_mode("view")
#> ℹ tmap modes "plot" - "view"
#> ℹ toggle with `tmap::ttm()`

tm_basemap(pdok_basemap("grijs")) +
  tm_shape(lange) +
  tm_dots(fill = "openbare_ruimte_naam", size = 0.5, id = "openbare_ruimte_naam",
          fill.legend = tm_legend("Street")) +
  tm_credits("Kaartgegevens © Kadaster")

The full translation table — including between(), is.na() and the accent-insensitive accenti() — is in ?pdok_where. A pdok_where object prints the CQL2 it stands for, which is a handy way to learn the language:

pdok_where(casei(woonplaats_naam) == "amersfoort" & huisnummer >= 10)
#> <pdok_where> CQL2 filter:
#> casei(woonplaats_naam) = casei('amersfoort') AND huisnummer >= 10

Combining with an area filter

An attribute filter and a spatial filter answer different halves of one question, and they work together in a single call: the server applies the attribute filter (and the bounding box), and pdok_read() then clips the result to the exact shape.

Without the attribute filter this would download every address in the municipality’s bounding box — hundreds of thousands of features. With it, the server sends only the “Lange…” addresses, and the exact clip to the boundary happens locally as usual.

amersfoort <- pdok_geocode("Amersfoort", type = "gemeente")

pdok_read(
  "kadaster/bag", "adres",
  filter_by = amersfoort, # spatial: within this boundary
  filter = pdok_where(like(openbare_ruimte_naam, "Lange%")) # attribute: on the server
) |>
  nrow()
#> [1] 392

Which values can a field take?

The queryables tell you which fields exist, not which values they hold. For the BAG the authoritative source is the official Catalogus BAG, whose domain values chapter defines the permitted values — for gebruiksdoel, for example, the eleven fixed uses (woonfunctie, onderwijsfunctie, winkelfunctie, …). Other datasets have their own documentation.

You can also just look at the data: read a modest sample and inspect the column.

buildings <- pdok_read("kadaster/bag", "pand", bbox = amersfoort, max_features = 500)
sort(unique(unlist(strsplit(buildings$gebruiksdoel, ","))))
#> [1] "bijeenkomstfunctie"      "industriefunctie"       
#> [3] "kantoorfunctie"          "onderwijsfunctie"       
#> [5] "overige gebruiksfunctie" "sportfunctie"           
#> [7] "winkelfunctie"           "woonfunctie"

The strsplit() is there because of a BAG subtlety that matters for filtering, covered next.

Multi-value fields: has()

As the sample shows, gebruiksdoel often holds several comma-joined values: a building inherits the use of every unit in it, so a school with a gym and a caretaker’s flat is registered as, say, "onderwijsfunctie,sportfunctie,woonfunctie". An equality test only matches buildings whose entire value is "onderwijsfunctie" and silently misses the rest. has() expresses what you usually mean — the field contains this value:

schools <- pdok_read(
  "kadaster/bag", "pand",
  filter_by = amersfoort,
  filter = pdok_where(has(gebruiksdoel, "onderwijsfunctie"))
)
nrow(schools)
#> [1] 176

# with == instead of has() we would have missed the combined-use buildings:
nrow(pdok_read(
  "kadaster/bag", "pand",
  filter_by = amersfoort,
  filter = pdok_where(gebruiksdoel == "onderwijsfunctie")
))
#> [1] 130

Thousands of ids at once

A common pattern is a table from another source that carries BAG ids — for example DUO’s school locations, which have a BAG_ID column (see Combining with external data). With %in% you can fetch exactly those features, whatever their number: PDOK limits how long a request URL may be, so pdok_where() automatically spreads a long value list over multiple requests (300 values each) and pdok_read() combines the results.

# Stand-in for ids from your own data: 700 addresses around the center
ids <- pdok_read(
  "kadaster/bag", "adres",
  filter = pdok_where(like(postcode, "3811%")),
  max_features = 700
)$identificatie

# 700 ids -> three requests behind the scenes, one sf back
mine <- pdok_read(
  "kadaster/bag", "adres",
  filter = pdok_where(identificatie %in% ids)
)
nrow(mine)
#> [1] 700

Row order follows the service, not your id vector, so join on the id column rather than relying on position.

Writing CQL2 yourself

pdok_where() covers the common cases, but filter also accepts a raw CQL2 text string, passed to the server unchanged — useful for CQL2 features the translation does not expose.

wijersstraat <- pdok_read(
  "kadaster/bag", "adres",
  filter = "postcode = '3811MZ' AND huisnummer = '1'"
)
paste(wijersstraat$openbare_ruimte_naam, wijersstraat$huisnummer)
#> [1] "Wijersstraat 1" "Wijersstraat 1"

A quick tour of the language (the CQL2 standard has the full grammar):

CQL2 Meaning
postcode = '3811MZ', <>, <, <=, >, >= comparisons; strings in single quotes
a AND b, a OR b, NOT (a) logic (AND binds tighter than OR)
postcode IN ('3811MZ', '3811MS') value lists
openbare_ruimte_naam LIKE 'Lange%' patterns; % = any characters
huisnummer BETWEEN 1 AND 9 ranges
casei(...), accenti(...) case-/accent-insensitive, applied to both sides
huisletter IS NULL missing values
S_INTERSECTS(geometry, POINT(5.387 52.156)) spatial predicates, coordinates in lon/lat

Spatial CQL2 functions such as S_INTERSECTS work, with two things to know. First, always combine them with a bbox: without one the server tests the geometry against the whole country and a single point takes 15 seconds, while with a bounding box the same request answers in about a second. Second, the server’s cost grows with the number of points and the size of the box, so this is a tool for a neighborhood or a city, not a province. For polygons the plain filter_by is faster than a server-side S_INTERSECTS, and for a small set of points pdok_read() already adds the spatial filter for you (see the filter_by argument), so writing S_INTERSECTS by hand is rarely needed.

The server validates every filter and pdokr passes its explanation on, so a mistake is easy to diagnose:

pdok_read("kadaster/bag", "adres", filter = "postcode == '3811MZ'")
#> Error in `pdok_read()`:
#> ! PDOK request failed (HTTP 400).
#> ✖ Server message: failed to parse CQL filter: syntax error at column 10:
#>   extraneous input '=' expecting {BooleanLiteral, CASEI, ACCENTI,
#>   NumericLiteral, '(', '-', DATE, TIMESTAMP, Identifier,
#>   CharacterStringLiteral}
#> ℹ URL:
#>   <https://api.pdok.nl/kadaster/bag/ogc/v2/collections/adres/items?f=json&limit=1000&filter=postcode%20%3D%3D%20%273811MZ%27>

Where to next