Skip to content

Commit

Permalink
add checking that the data exist
Browse files Browse the repository at this point in the history
  • Loading branch information
andysouth committed Aug 3, 2016
1 parent d325c05 commit 38bc9e1
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
63 changes: 63 additions & 0 deletions R/check_data_exist.r
Original file line number Diff line number Diff line change
@@ -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)

}
4 changes: 2 additions & 2 deletions R/ne_download.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions R/ne_file_name.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions inst/extdata/data_list_cultural.csv
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions inst/extdata/data_list_physical.csv
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions man/check_data_exist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/ne_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 38bc9e1

Please sign in to comment.