Skip to content

Instantly share code, notes, and snippets.

@ateucher
Created July 31, 2018 20:11
Show Gist options
  • Save ateucher/8bdc1a5ceb3d34e97b5c559c882daaf4 to your computer and use it in GitHub Desktop.
Save ateucher/8bdc1a5ceb3d34e97b5c559c882daaf4 to your computer and use it in GitHub Desktop.
bcdc_to_sf <- function(layer, crs = 3005) {
if (!requireNamespace("httr") || !requireNamespace("sf")) {
stop("You must have the sf and httr packages installed.")
}
base_url <- sprintf("https://openmaps.gov.bc.ca/geo/pub/%s/ows", layer)
srs <- paste0("epsg:", crs)
response <- httr::GET(base_url,
query = list(service = "WFS",
version = "2.0.0",
request = "GetFeature",
typeName = layer,
outputFormat = "json",
SRSNAME = srs))
httr::stop_for_status(response)
sf::read_sf(httr::content(response, as = "text"))
}
# Example usage
herds <- bcdc_to_sf("WHSE_WILDLIFE_INVENTORY.GCPB_CARIBOU_POPULATION_SP")
plot(herds[, "HERD_NAME"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment