Sf: Error in CPL_read_ogr

Created on 19 Jun 2019  路  10Comments  路  Source: r-spatial/sf

Any ideas why this error happens?
I am using the sf library to read a .shp file..appreciate any help!

Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : Open failed.

reprex

All 10 comments

Please provide a reproducible example.

thank you, did you want more info?
image
or
image

Maybe load library sf first, so that %>% also works?

The error you see in those screenshots (sigh) is related to the fact that the function %>% cannot be found. Load sf, dplyr or magrittr to get it.

Also, when providing a reproducible example you should also provide your input data. Otherwise it is hardly _reproducible_, is it?

thank you both, it doesn't seem to be a matter of loading libraries as they are loaded, I include them every time with the two columns (e.g. here::here()) because there seems to be conflicts between packages (e.g. here with lubridate)
here's a repress of the larger workflow:
image
image
image

Please provide a real example. Also try not using here::here(), I guess that the fact that a shapefile is actually at least three files is confusing for here (probably a wrong guess - doesn't find a GPKG either). I installed here, and tried to set it to the spData installation, but it disregarded its own logic - the .here file was where it should be, but here::here() returned my HOME directory anyway, despite being told where to look for the file:

library(sf)
here::set_here(system.file("shapes", package="spData")[1])
file.exists(file.path(system.file("shapes", package="spData")[1], ".here")) # TRUE
col <- st_read(here::here("columbus.shp")) # failed
# looks in /home/rsb
col <- read_sf(here::here("columbus.shp")) #failed
# looks in /home/rsb
col <- read_sf(system.file("shapes/columbus.shp", package="spData")[1]) # succeeded
col <- st_read(system.file("shapes/columbus.shp", package="spData")[1]) # succeeded
wrld <- read_sf(here::here("world.gpkg")) # failed
# looks in /home/rsb
wrld <- st_read(system.file("shapes/world.gpkg", package="spData")[1]) # succeeds

So, don't use here, not fit for purpose. I have removed here, a classic case of breaking the "as simple as possible, but not more so" rule.

Thanks for taking the time to report your issue @NoushinN. It seems your issue is very specific to the data you are reading and your computer configuration (OS, installation, packages available, ...). For that reason, to understand the root of your problem, we'd need public data (provide a URL, for instance) with which you can reproduce that error. There are multiple causes ranging from missing file/wrong path to more complex.

Also, rather than post screenshots of your example, you can simply copy and paste the text generated by the reprex package. You can embed code directly in your github post (just like you did in your initial post), and include your session info. If you're new to reprex, it's worth having a look at examples.

@rsbivand and all

Thank you for your comments and help. Realizing that the here() package in combination with sf() to read the .shp files from a shp-files folder was the issue, problem is solved now by specifying file.path before reading .shp file. Here's a sample:

# this works now: 
ct <- sf::st_read("shp-files/lct_000a16a_e.shp") %>%
  dplyr::filter(PRNAME == "British Columbia / Colombie-Britannique") %>%
  st_transform(3005)
# instead of:
ct <- sf:: st_read(here("shp-files", "lct_000a16a_e.shp")) %>%
  filter(PRNAME == "British Columbia / Colombie-Britannique") %>%
  st_transform(3005)

I'm having the same problem with Error in CPL_read_ogr combined with SEC_E_MESSAGE_ALTERED that is appearing after reading a few lines from the wfs service.

Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  : 
  No layers in datasource.
In addition: Warning messages:
1: In CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
  GDAL Error 1: schannel: next InitializeSecurityContext failed: SEC_E_MESSAGE_ALTERED (0x8009030F) - A mensagem ou assinatura fornecida para verifica莽茫o foi alterada
2: In CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
  GDAL Error 1: schannel: next InitializeSecurityContext failed: SEC_E_MESSAGE_ALTERED (0x8009030F) - A mensagem ou assinatura fornecida para verifica莽茫o foi alterada

reprex

library(sf)
library(XML)
library(dplyr)

base_url <- "http://www.flora-on.pt"
endpoint <-"wfs"
q <- list(request = "GetCapabilities")
res <- httr::GET(url = httr::modify_url(base_url, path = endpoint), query = q)
res$url

txt <- httr::content(res, type = "text")
xml <- xml2::read_xml(txt)
doc <-  xmlParse(xml)
xmldf <- xmlToDataFrame(nodes = getNodeSet(doc, "//FeatureType"))
df <- xmlToDataFrame(nodes = xmlChildren(xmlRoot(doc)[["FeatureTypeList"]]), stringsAsFactors = F)

sflist <- list()
for(i in df$Name[1:100]){
  qf <- list(request = "GetFeature", typeName = i)
  file <- tempfile(fileext = ".gml")
  try(httr::GET(url = base_url, path = endpoint, query = qf, httr::write_disk(path = file)))
  spfloraon <- sf::read_sf(file)
  if(nrow(spfloraon) != 0){
    spfloraon <- spfloraon %>% st_set_crs(32629) %>% select(2, 3, 6)
    sflist[[i]] <- spfloraon
    }
  Sys.sleep(1)
}

Hi @pecard

Thanks for this report. I suggest you open a new issue for your specific case and link to this issue, since this one is closed. Could you include the output from sf::sf_extSoftVersion() and devtools::session_info() or sessionInfo() and tell us where the error happens exactly (what's the value of i). I could not reproduce your bug by running your reprex (It works for me with the latest sf version on ubuntu). I know it's not helping much, but at least it tells us it's a problem specific to your system.

If you can, try to strip as much code as you can from your reprex, it will make it easier to find the exact cause. For example, maybe there's one file that fails, so you can just provide the code for this one file rather than the complete loop.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nosferican picture Nosferican  路  3Comments

kendonB picture kendonB  路  3Comments

tiernanmartin picture tiernanmartin  路  3Comments

kendonB picture kendonB  路  4Comments

dkyleward picture dkyleward  路  4Comments