-
Notifications
You must be signed in to change notification settings - Fork 20
Open
Description
Context for Issue:
I was trying to teach an intern how to create a spatialVector for a dataset, and we ran into the following error message when trying to read in the shapefiles:
> upstream_pid <- selectMember(dp, "sysmeta@fileName", "upstream_points.zip")
> shapefile <- arcticdatautils::read_zip_shapefile(mn, upstream_pid)
Error in arcticdatautils::read_zip_shapefile(mn, upstream_pid) :
Zipped directory must contain one and only one .shp file
Ways we tried to debug the issue:
- There's only 1 pid for the
upstream_pidobject, so no doubles there - There's only 1
.shpfile in the zip folder - The same
.shpfile can be read in properly using thesf::st_read()function that theread_zip_shapefileeventually calls in the function. - Tested the
read_zip_shapefilefunction on a file from the Kapsar dataset, and didn't get an error. The main difference between these two is that the dataset we're trying to process has a.qpjfile for QGIS.
Not sure if function is having explicit difficulty with the .qpj file from QGIS, but didn't have time to debug today. The read_zip_shapefile function can be found below.
Function Code:
Lines 438 to 482 in d08d3e0
| #' Read a shapefile from a pid | |
| #' | |
| #' Read a shapefile 'sf' from a pid that points to the zipped directory of the shapefile and associated files | |
| #' on a given member node. | |
| #' | |
| #' @param mn (MNode) A DataOne Member Node | |
| #' @param pid (character) An object identifier | |
| #' | |
| #' @return shapefile (sf) The shapefile as an `sf` object | |
| #' | |
| #' @export | |
| #' | |
| #' @author Jeanette Clark jclark@@nceas.ucsb.edu | |
| #' | |
| #' @examples | |
| #' \dontrun{ | |
| #' cn <- dataone::CNode('PROD') | |
| #' adc <- dataone::getMNode(cn,'urn:node:ARCTIC') | |
| #' pid <- "urn:uuid:294a365f-c0d1-4cc3-a508-2e16260aa70c" | |
| #' | |
| #' shapefile <- read_zip_shapefile(adc, pid) | |
| #' } | |
| read_zip_shapefile <- function(mn, pid){ | |
| stopifnot(methods::is(mn, 'MNode')) | |
| stopifnot(is.character(pid)) | |
| if (!requireNamespace("sf")) { | |
| stop(call. = FALSE, | |
| "The package 'sf' must be installed to run this function. ", | |
| "Please install it and try again.") | |
| } | |
| temp <- tempfile() | |
| writeBin(dataone::getObject(mn, pid), temp) | |
| zip_contents <- utils::unzip(temp, exdir = tempfile()) | |
| if (length(grep("shp", tools::file_ext(zip_contents))) != 1){ | |
| stop("Zipped directory must contain one and only one .shp file") | |
| } | |
| shapefile <- sf::st_read(zip_contents[grep("shp", tools::file_ext(zip_contents))], quiet = T, stringsAsFactors = F) | |
| unlink(temp) | |
| return(shapefile) | |
| } |
Metadata
Metadata
Assignees
Labels
No labels