From 38bc9e1492584f5fc6b9dd35dc70ce7d1bf458b4 Mon Sep 17 00:00:00 2001 From: AndySouth Date: Wed, 3 Aug 2016 14:37:37 +0100 Subject: [PATCH] add checking that the data exist --- NAMESPACE | 1 + R/check_data_exist.r | 63 +++++++++++++++++++++++++++++ R/ne_download.r | 4 +- R/ne_file_name.r | 24 +++++++---- inst/extdata/data_list_cultural.csv | 18 +++++++++ inst/extdata/data_list_physical.csv | 19 +++++++++ man/check_data_exist.Rd | 34 ++++++++++++++++ man/ne_download.Rd | 4 +- 8 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 R/check_data_exist.r create mode 100644 inst/extdata/data_list_cultural.csv create mode 100644 inst/extdata/data_list_physical.csv create mode 100644 man/check_data_exist.Rd diff --git a/NAMESPACE b/NAMESPACE index 2b11b7e..cd51b8f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(check_data_exist) export(check_rnaturalearthdata) export(check_rnaturalearthhires) export(install_rnaturalearthdata) diff --git a/R/check_data_exist.r b/R/check_data_exist.r new file mode 100644 index 0000000..252db4a --- /dev/null +++ b/R/check_data_exist.r @@ -0,0 +1,63 @@ +#' check whether the requested data exist on Natural Earth +#' +#' checks from a list dependent on type, category and scale. If it returns FALSE the data may still exist on the website. +#' Doesn't yet do checking on raster names because I found the naming convention too tricky. +#' +#' @param scale scale of map to return, one of \code{110}, \code{50}, \code{10} or \code{'small'}, \code{'medium'}, \code{'large'} +#' @param type type of natural earth file to download one of 'countries', 'map_units', 'map_subunits', 'sovereignty', 'states' +#' OR the portion of any natural earth vector url after the scale and before the . +#' e.g. for 'ne_50m_urban_areas.zip' this would be 'urban_areas' +#' @param category one of natural earth categories : 'cultural', 'physical', 'raster' + +#' @examples +#' check_data_exist( scale = 110, category = 'cultural', type = 'countries' ) +#' # type not in list for this category +#' check_data_exist( scale = 110, category = 'physical', type = 'airports' ) +#' # type in list but scale shows FALSE +#' check_data_exist( scale = 110, category = 'cultural', type = 'airports' ) +#' +#' @return TRUE or FALSE +#' @export + +check_data_exist <- function(scale = 110, + type, + category = c('cultural', 'physical', 'raster') +) +{ + + # check permitted category + category <- match.arg(category) + + # todo doesn't yet check raster + # I would need to create a data_list_raster.csv file + if (category=='raster') return(TRUE) + + # check on permitted scales, convert names to numeric + scale <- check_scale(scale) + + + df_data <- read.csv( system.file("extdata", paste0("data_list_", category, ".csv"), package = "rnaturalearth") ) + + + #first check if type is within the list + if ( ! type %in% df_data$type ) + { + warning(type," seems not to be in the list for category=", category, " maybe try the other category of c('cultural', 'physical')") + return(FALSE) + } + + #df_data[df_data$type=='roads', 'scale110'] + + exist <- df_data[df_data$type==type,paste0("scale",scale)] + + + if (!exist) + { + warning("your combination of type, category, scale", + " seem not to exist in the list of Natural Earth data so you may get a download fail message.", + " Check ?ne_download or http://www.naturalearthdata.com/features/ to see data availability.") + } + + return(exist) + +} \ No newline at end of file diff --git a/R/ne_download.r b/R/ne_download.r index 5b9ce43..b89405c 100644 --- a/R/ne_download.r +++ b/R/ne_download.r @@ -32,8 +32,8 @@ #' reefs \tab - \tab - \tab y \cr #' category = 'cultural', type = '[below]' \cr #' populated_places \tab y \tab y \tab y \cr -#' admin_0_boundary_lines_land \tab y \tab y \tab y \cr -#' admin_0_breakaway_disputed_areas \tab - \tab y \tab y \cr +#' boundary_lines_land \tab y \tab y \tab y \cr +#' breakaway_disputed_areas \tab - \tab y \tab y \cr #' airports \tab - \tab y \tab y \cr #' ports \tab - \tab y \tab y \cr #' urban_areas \tab - \tab y \tab y \cr diff --git a/R/ne_file_name.r b/R/ne_file_name.r index c201874..15e4641 100644 --- a/R/ne_file_name.r +++ b/R/ne_file_name.r @@ -27,16 +27,26 @@ ne_file_name <- function(scale = 110, scale <- check_scale(scale) # check permitted category - category <- match.arg(category) - # type is left unchecked so users can specify any natearth filename + category <- match.arg(category) - # some combinations are not available - if ( type=='map_subunits' & scale==110 ) - stop("The combination of type=",type,"and scale=",scale,"is not available in Natural Earth") + # check type against lists in package to warn user if they try something + # that seems not to exist + # however continue to try the download to give user flexibility + # to get something I may have missed out of my list or has been added more recently + check_data_exist( scale = scale, category = category, type = type ) + # add admin_0 to known types - if (type=='countries' | type=='map_units' | type=='map_subunits' | type=='sovereignty' | type=='tiny_countries' ) - type <- paste0('admin_0_',type) + if ( type %in% c('countries', 'map_units', 'map_subunits', 'sovereignty', 'tiny_countries', + 'boundary_lines_land', + 'pacific_groupings', + 'breakaway_disputed_areas', + 'boundary_lines_disputed_areas', + 'boundary_lines_maritime_indicator') ) + { + type <- paste0('admin_0_',type) + } + # add admin_1 to known types # this actually just expands 'states' to the name including lakes diff --git a/inst/extdata/data_list_cultural.csv b/inst/extdata/data_list_cultural.csv new file mode 100644 index 0000000..36f4a80 --- /dev/null +++ b/inst/extdata/data_list_cultural.csv @@ -0,0 +1,18 @@ +type,scale110, scale50,scale10 +countries,TRUE,TRUE,TRUE +map_units,TRUE,TRUE,TRUE +map_subunits,FALSE,TRUE,TRUE +sovereignty,TRUE,TRUE,TRUE +tiny_countries,TRUE,TRUE,TRUE +states,FALSE,TRUE,TRUE +populated_places,TRUE,TRUE,TRUE +boundary_lines_land,TRUE,TRUE,TRUE +pacific_groupings,TRUE,TRUE,TRUE +breakaway_disputed_areas,FALSE,TRUE,TRUE +boundary_lines_disputed_areas,FALSE,TRUE,TRUE +boundary_lines_maritime_indicator,FALSE,TRUE,TRUE +airports,FALSE,TRUE,TRUE +ports,FALSE,TRUE,TRUE +urban_areas,FALSE,TRUE,TRUE +roads,FALSE,FALSE,TRUE +railroads,FALSE,FALSE,TRUE diff --git a/inst/extdata/data_list_physical.csv b/inst/extdata/data_list_physical.csv new file mode 100644 index 0000000..94f4cb9 --- /dev/null +++ b/inst/extdata/data_list_physical.csv @@ -0,0 +1,19 @@ +type,scale110, scale50,scale10 +coastline,TRUE,TRUE,TRUE +land,TRUE,TRUE,TRUE +ocean,TRUE,TRUE,TRUE +rivers_lake_centerlines,TRUE,TRUE,TRUE +lakes,TRUE,TRUE,TRUE +glaciated_areas,TRUE,TRUE,TRUE +antarctic_ice_shelves_polys,TRUE,TRUE,TRUE +geographic_lines,TRUE,TRUE,TRUE +graticules_1,TRUE,TRUE,TRUE +graticules_5,TRUE,TRUE,TRUE +graticules_10,TRUE,TRUE,TRUE +graticules_15,TRUE,TRUE,TRUE +graticules_20,TRUE,TRUE,TRUE +graticules_30,TRUE,TRUE,TRUE +wgs84_bounding_box,TRUE,TRUE,TRUE +playas,FALSE,TRUE,TRUE +minor_islands,FALSE,FALSE,TRUE +reefs,FALSE,FALSE,TRUE diff --git a/man/check_data_exist.Rd b/man/check_data_exist.Rd new file mode 100644 index 0000000..817efde --- /dev/null +++ b/man/check_data_exist.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/check_data_exist.r +\name{check_data_exist} +\alias{check_data_exist} +\title{check whether the requested data exist on Natural Earth} +\usage{ +check_data_exist(scale = 110, type, category = c("cultural", "physical", + "raster")) +} +\arguments{ +\item{scale}{scale of map to return, one of \code{110}, \code{50}, \code{10} or \code{'small'}, \code{'medium'}, \code{'large'}} + +\item{type}{type of natural earth file to download one of 'countries', 'map_units', 'map_subunits', 'sovereignty', 'states' +OR the portion of any natural earth vector url after the scale and before the . +e.g. for 'ne_50m_urban_areas.zip' this would be 'urban_areas'} + +\item{category}{one of natural earth categories : 'cultural', 'physical', 'raster'} +} +\value{ +TRUE or FALSE +} +\description{ +checks from a list dependent on type, category and scale. If it returns FALSE the data may still exist on the website. +Doesn't yet do checking on raster names because I found the naming convention too tricky. +} +\examples{ +check_data_exist( scale = 110, category = 'cultural', type = 'countries' ) +# type not in list for this category +check_data_exist( scale = 110, category = 'physical', type = 'airports' ) +# type in list but scale shows FALSE +check_data_exist( scale = 110, category = 'cultural', type = 'airports' ) + +} + diff --git a/man/ne_download.Rd b/man/ne_download.Rd index 8bbc32a..4a1ebc7 100644 --- a/man/ne_download.Rd +++ b/man/ne_download.Rd @@ -50,8 +50,8 @@ A non-exhaustive list of datasets available according to \code{scale} specified reefs \tab - \tab - \tab y \cr category = 'cultural', type = '[below]' \cr populated_places \tab y \tab y \tab y \cr - admin_0_boundary_lines_land \tab y \tab y \tab y \cr - admin_0_breakaway_disputed_areas \tab - \tab y \tab y \cr + boundary_lines_land \tab y \tab y \tab y \cr + breakaway_disputed_areas \tab - \tab y \tab y \cr airports \tab - \tab y \tab y \cr ports \tab - \tab y \tab y \cr urban_areas \tab - \tab y \tab y \cr