Skip to content

QGIS & read_zip_shapefile #197

@dvirlar2

Description

@dvirlar2

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions