data-raw/sf_brain_outer_super_region.R

#' Super-regions of an adult human brain.
#'
#' A remix of \code{\link{sf_brain_outer}}. Ideal for visualizing gene expression
#' data from the Brainspan developmental transcriptome or GTEx.
#'
#' @format A simple feature collection with 8 features and 1 field
#' \describe{
#'   \item{id}{short name of brain region}
#'   \item{geometry}{the sf shape corresponding to the region}
#' }
#' @source \href{https://www.gnu.org/licenses/gpl-3.0.en.html}{GPL-3.0} by \url{https://github.com/ethanbahl/cerebroViz}
#'
#' @concept remix
#' @concept brain
#' @concept human
#'
#' @family brains
#'
#' @examples
#' require(sf)
#' sf_brain <- sffs::sf_brain_outer_super_region
#' plot(sf_brain, key.length = 1)
"sf_brain_outer_super_region"


require(devtools)
require(usethis)
require(tidyverse)
require(here)
require(xml2)
require(sf)


#### Path to svg ####
### Be sure to use `here()` ###
name <- "sf_brain_outer_super_region"
input_dir <- here("data-raw", name)
input_svg <- here("data-raw", name, str_c(name, ".svg"))


#### convert SVG to geoJSON ####
###  requires the external node-based application `svg2geojson` ###
sf <- paste0("svg2geojson  -o ", input_svg) %>%
  system(intern = TRUE) %>%
  paste0(collapse = " ") %>%
  st_read()

###  and ensure it is valid ####
sf <- st_make_valid(sf)

#### extract geometry IDs         ####
#### could also be added manually ####
xml_file <- read_xml(input_svg) %>%
  xml_children()
ids <- xml_file[xml_name(xml_file) == 'path'] %>%
  xml_attr('class')
sf <- sf %>%
  mutate(id = ids) %>%
  select(id, geometry)

#### write out the simple features ###
write_sf(sf, gsub(".svg", ".geojson", input_svg))

#### Final assignment and export of data ####
assign(name, sf)

usethis::use_data(sf_brain_outer_super_region, overwrite = TRUE)
tkoomar/sffs documentation built on July 6, 2020, 9:36 a.m.