Skip to content

Commit a059ef2

Browse files
author
Michael Harper
committed
Update package tests
1 parent eed3092 commit a059ef2

27 files changed

Lines changed: 459 additions & 75 deletions

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export(display_polygon)
88
export(extract_id)
99
export(list_polygons)
1010
export(merge_delineated_polygons)
11+
import(assertthat)
1112
import(leaflet)

R/assess_polygon.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ assess_polygons <- function(polygons, survey_points, settlement_points, ornl, me
5656

5757
#' Assess a single polygon
5858
#'
59-
#' Internal function that is used
59+
#' Internal function that is used to diagnose a single Polygon. Please use the
60+
#' \code{\link{assess_polygons}} function which is designed to use this function
61+
#' across a list of polygons.
62+
#'
6063
#' @param polygon_path the relative file path to the polygon shapefile
6164
#' @param survey_points Optional. The survey points where the delineation should occur
6265
#' @param settlement_points Optional. The settlement type points layer

R/check_crs.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
#' Validate the CRS of a shapefile
22
#'
3-
#' Will extract the coordinate reference system from a polygon and check that it
3+
#' Will extract the coordinate reference system from a SpatialObject and check that it
44
#' matches a specified reference.
55
#'
6+
#' @param input an object of spatial class.
67
#' @param crs_ref the CRS to be compared against. Defaults to WGS84
8+
#' @param warn print a warning message if they do not match. Default is TRUE
79
#'
810
#' @export
911
#' @author Michael Harper
1012
#'
1113
check_crs <- function(input,
12-
crs_ref = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"){
14+
crs_ref = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0",
15+
warn = TRUE){
1316

17+
# Check that the objects are correct
18+
assertthat::assert_that(grepl("Spatial",class(input)),
19+
msg = paste0(deparse(substitute(input)), " is not of class 'Spatial'"))
20+
21+
# Convert crs_ref into CRS object and compare with shapefile
1422
ref <- sp::CRS(crs_ref)
15-
crs_match <- compareCRS(ref, crs(shp))
23+
crs_match <- raster::compareCRS(ref, raster::crs(input))
24+
25+
# Return error message if not matching
26+
if(!crs_match & warn) warning("The CRS of the file does not match the reference")
27+
28+
# Return results
1629
return(crs_match)
1730
}

R/display_polygon.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
#' @export
2323
#'
2424
#' @example R/examples/display_polygon_1.R
25+
#' @import assertthat
2526
#'
2627
display_polygon <- function(shp_path, survey_points = NULL, settlement_point = NULL){
2728

29+
# Check inputs
30+
assertthat::assert_that(has_extension(shp_path, "shp"))
31+
assertthat::assert_that(is.readable(shp_path))
32+
if(!is.null(survey_points)) assertthat::assert_that(is.spatial(survey_points))
33+
if(!is.null(settlement_point)) assertthat::assert_that(is.spatial(settlement_point))
34+
2835
# Load the shapefile
2936
shp <- raster::shapefile(x = shp_path)
3037

@@ -70,12 +77,12 @@ display_polygon <- function(shp_path, survey_points = NULL, settlement_point = N
7077
if(!is.null(survey_points)){
7178

7279
# Load survey point
73-
survey_points_data <- shapefile(survey_points)
74-
survey_points_data$FID <- 0:(nrow(survey_points_data)-1) # Add FID to object
80+
survey_points_id <- survey_points
81+
survey_points_id$FID <- 0:(nrow(survey_points_id)-1) # Add FID to object
7582

7683
# Find the coordinate ID which matches
7784
id <- extract_id(shp_path) # Extract the ID from the file name
78-
survey_points_id <- survey_points_data[survey_points_data$FID == id,]
85+
survey_points_id <- survey_points_id[survey_points_id$FID == id,]
7986
coords <- coordinates(survey_points_id)
8087

8188
# Add markers to map

R/extract_id.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
#'
1616
extract_id <- function(filepath){
1717

18+
# Make sure input is character
19+
assertthat::assert_that(is.character(filepath),
20+
msg = paste0(deparse(substitute(filepath)), " is not a character vector"))
21+
1822
id <- basename(filepath) %>%
19-
stringr::str_extract(pattern = "[:digit:]{3}") %>%
23+
stringr::str_extract(pattern = "[:digit:]{1,3}") %>%
2024
as.numeric()
2125

2226
return(id)

R/merge_delineated_polygons.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#'
1515
merge_delineated_polygons <- function(shp_paths, message = TRUE){
1616

17+
# Check that paths exist
18+
for(i in shp_paths){
19+
assertthat::assert_that(assertthat::is.readable(i))
20+
}
21+
1722
# Load the polygons and merge into single object
1823
if(message) message("Loading Polygons")
1924
shp_loaded <- pbapply::pblapply(shp_paths, function(x) raster::shapefile(x = x))

R/utilities.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#' Check if an object is a spatial class
3+
#'
4+
is.spatial <- function(object){
5+
assertthat::assert_that(grepl("Spatial",class(object)),
6+
msg = paste0(deparse(substitute(object)), " is not of class a spatial object"))}

docs/articles/Basics.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)