Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QGIS & read_zip_shapefile #197

Open
dvirlar2 opened this issue Aug 30, 2022 · 1 comment
Open

QGIS & read_zip_shapefile #197

dvirlar2 opened this issue Aug 30, 2022 · 1 comment

Comments

@dvirlar2
Copy link
Contributor

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_pid object, so no doubles there
  • There's only 1 .shp file in the zip folder
  • The same .shp file can be read in properly using the sf::st_read() function that the read_zip_shapefile eventually calls in the function.
  • Tested the read_zip_shapefile function 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 .qpj file 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:

arcticdatautils/R/helpers.R

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)
}

@jeanetteclark
Copy link
Collaborator

jeanetteclark commented Sep 8, 2022

found and fixed, in 5722e9c. can you reinstall (from main) and test @dvirlar2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants