Translates an R expression into a CQL2
text filter for the filter argument of pdok_read(). Field names are
written as bare names (see pdok_list_queryables() for the fields a layer
supports); values are taken from your R session, so variables and vectors
work as usual.
Usage
pdok_where(expr)
# S3 method for class 'pdok_where'
as.character(x, ...)
# S3 method for class 'pdok_where'
print(x, ...)Value
An object of class pdok_where, for the filter argument of
pdok_read(). as.character() returns the CQL2 text it renders to (one
string per batched request).
Supported syntax
| R | CQL2 |
field == value, field != value | field = value, field <> value |
field < v, field <= v, field > v, field >= v | idem |
field %in% c(...) | field IN (...) |
a & b, a \| b, !a | a AND b, a OR b, NOT (a) |
has(field, "value") | field LIKE '%value%' (field contains the value) |
like(field, "Lange%") | field LIKE 'Lange%' (% = wildcard) |
between(field, lo, hi) | field BETWEEN lo AND hi |
is.na(field), !is.na(field) | field IS NULL, field IS NOT NULL |
casei(field) == value | casei(field) = casei(value) (case-insensitive) |
accenti(field) == value | accent-insensitive comparison |
has() matches the value anywhere in the field, with any wildcards in the
value escaped. It is made for fields that hold several comma-joined values,
such as gebruiksdoel in the BAG: has(gebruiksdoel, "onderwijsfunctie")
also matches a building registered as
"logiesfunctie,onderwijsfunctie", where == would not.
has(), like(), between(), casei() and accenti() only have meaning
inside pdok_where(); they are translated, never executed as R functions. The
left-hand side of a comparison is always the field, the right-hand side is
always an R value. For CQL2 features beyond this table, pass a CQL2 string
to filter directly.
Finding field names and values
Only the fields a layer publishes as queryables can be filtered on.
pdok_list_queryables() lists them for one layer;
pdok_list_layers(dataset, queryables = TRUE) shows them for every layer
of a dataset at once. The values a field holds are not part of the API:
consult the dataset's own documentation (for the BAG, the
Catalogus BAG defines the permitted
values of fields such as gebruiksdoel and status), or read a small
sample of the layer with pdok_read() and inspect the column.
Long %in% vectors
PDOK's servers reject over-long request URLs, so a %in% over more than
300 values (after deduplication) is sent as multiple requests of 300 values
each, and pdok_read() combines the results. This happens automatically;
it does require that the %in% is combined with any other conditions by
& (not inside | or !), and at most one %in% per filter can be this
long. Row order follows the service, not your input vector.
Examples
pdok_where(postcode == "3811MZ")
#> <pdok_where> CQL2 filter:
#> postcode = '3811MZ'
pdok_where(
casei(woonplaats_naam) == "amersfoort" & like(openbare_ruimte_naam, "Lange%")
)
#> <pdok_where> CQL2 filter:
#> casei(woonplaats_naam) = casei('amersfoort') AND openbare_ruimte_naam LIKE 'Lange%'
# Values come from your session, so vectors and variables work:
codes <- c("3811MZ", "3811MS")
pdok_where(postcode %in% codes & status != "Naamgeving ingetrokken")
#> <pdok_where> CQL2 filter:
#> postcode IN ('3811MZ', '3811MS') AND status <> 'Naamgeving ingetrokken'
# has(): the field contains the value (multi-value fields such as
# the BAG gebruiksdoel, where a building can have several uses)
pdok_where(has(gebruiksdoel, "onderwijsfunctie"))
#> <pdok_where> CQL2 filter:
#> gebruiksdoel LIKE '%onderwijsfunctie%'
