Error when making web feature requests (WFS) from R markdown using "sf" geospatial library

Hello all,

I am trying to make a web feature service (WFS) request from an R markdown file. The code runs without error from a regular R script, but throws an error when run from an R markdown script. Because the WFS server will only return 100 records at a time, it is necessary to make a series of requests until all 3,272 records are received. This is accomplished with a for-loop with results appended to an empty data frame. Within the loop, the series of individual WFS requests are built and executed. The part of the code that fails is with the actual request to the WFS server (e.g. covid_cases <- sf::read_sf(request)). The code is presented below, which generates the following error:

Error in CPL_get_layers(dsn, options, do_count) : Open failed.

CODE

library(sf)
jhu_wfs_link <- "Query: Cases (ID: 0)"

#Import file with county objectID numbers, this is used to simply get a list of the objectIDs that must be retrieved in the query
#The following few lines simply create a vector of integer values corresponding to the objectIds that are used to build individual requests as we loop through
covid_counties <- read_csv(str_glue("../R script/jhu_covid_counties.csv"),
col_types = cols(
OBJECTID = col_double(),
Province_State = col_character(),
Last_Update = col_character(),
Confirmed = col_character(),
Deaths = col_character()))

#Pull out just the OBJECTID values
covid_counties <- covid_counties %>% select(OBJECTID)

The JHU WFS API will only accept requests of 100 records at a time, so....

break the data frame into chunks of 100 records (from a total collection of 3272 records)

grp_size <- 100
covid_grps <- covid_counties %>% group_split(group_id=row_number() %/% grp_size)
covid_grps_cnt <- length(covid_grps)

#Create empty data frame to append WFS query results
state_sums <- data.frame()

#Now loop though the group lists in "covid_grps" and make a series of requests to the WFS service
#Append resultant sublists to data frame
for (x in 1:covid_grps_cnt) {
covid_objects <- str_c(covid_grps[]$OBJECTID, collapse=",")
#print(covid_objects)
url <- httr::parse_url(jhu_wfs_link)

#build unique set of query parameters for request
url$query <- list(f="json",
    inSR="4326",
    outSR="4326",
    returnGeometry="true",
    outFields="OBJECTID, Confirmed, Deaths, Province_State, Last_Update",
    returnM="false",
    returnZ="false"
    )

#build url for query request
request <- build_url(url)

print(request)

sf::st_layers(request)
covid_cases <- sf::read_sf(request)
print(covid_cases)

#append to data.frame
state_sums <- rbind(state_sums, covid_cases)
} #end for-loop

The problem only occurs when these requests are made from a markdown file. It runs to completion without error in a regular R script.

Thanks much for any feedback, Bill

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.