Last active
September 5, 2018 08:14
-
-
Save FrieseWoudloper/9b6561da581230d0c240739ca77cec44 to your computer and use it in GitHub Desktop.
R script om te demonstreren hoe je WFS paging kunt implementeren in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sf) | |
library(tmap) | |
library(magrittr) | |
library(httr) | |
library(xml2) | |
url <- parse_url("geodata.nationaalgeoregister.nl/bag/wfs?") | |
url$scheme <- "https" | |
# Request server limit | |
url$query <- list(service = "wfs", | |
version = "2.0.0", | |
request = "GetCapabilities") | |
server_limit <- build_url(url) %>% GET() %>% content(as = "text", encoding="UTF-8") %>% read_xml() %>% | |
xml_find_first("//ows:Constraint[@name='CountDefault']/ows:DefaultValue") %>% xml_integer() | |
# Request number of hits - verblijfobjecten in Zuidhorn | |
url$query <- list(service = "wfs", | |
version = "2.0.0", | |
request = "GetFeature", | |
typename = "bag:verblijfsobject", | |
cql_filter = "woonplaats = 'Zuidhorn'", | |
outputFormat = NULL, | |
resultType = "hits") | |
hits <- build_url(url) %>% GET() %>% content(as = "text", encoding="UTF-8") %>% read_xml() %>% | |
xml_find_first("//@numberMatched") %>% xml_integer() | |
# Request verblijfsobjecten in Zuidhorn using paging | |
url$query <- list(service = "wfs", | |
version = "2.0.0", | |
request = "GetFeature", | |
typename = "bag:verblijfsobject", | |
cql_filter = "woonplaats = 'Zuidhorn'", | |
outputFormat = "application/json", | |
resultType = "results", | |
sortBy = "bag:identificatie") | |
zuidhorn <- lapply(seq(0, hits, server_limit), function(x) { | |
url$query$startindex <- x | |
build_url(url) %>% st_read(stringsAsFactors = F) | |
}) %>% do.call(rbind, .) | |
nrow(zuidhorn) | |
qtm(zuidhorn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment