Skip to content

Commit

Permalink
make requested CRAN changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrobwilliams committed Feb 17, 2021
1 parent c63247d commit cf6e74b
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CRAN-RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This package was submitted to CRAN on 2021-01-14.
Once it is accepted, delete this file and tag the release (commit 45bc3fc).
This package was submitted to CRAN on 2021-02-17.
Once it is accepted, delete this file and tag the release (commit c63247d).
2 changes: 1 addition & 1 deletion R/dms2dd.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dms2dd <- function(lon, lat) {

coords <- strsplit(c(lon[i], lat[i]), '\\s')

card_dir <- vapply(coords, function(x) length(x) == 4, FUN.VALUE = T)
card_dir <- vapply(coords, function(x) length(x) == 4, FUN.VALUE = TRUE)

if (all(card_dir)) {

Expand Down
2 changes: 1 addition & 1 deletion R/overlap.weight.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' crs = CRS(st_crs(polys_t)$proj4string))
#' overlap.weight(raster_t, polys_t)

overlap.weight <- function(raster, polygons, count = F, warn = T) {
overlap.weight <- function(raster, polygons, count = FALSE, warn = TRUE) {

polygons <- poly.conv(polygons, raster)

Expand Down
10 changes: 5 additions & 5 deletions R/point.poly.dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' crs = 4326)
#' point.poly.dist(points, polys)

point.poly.dist <- function(point, poly, max = T, by_element = F) {
point.poly.dist <- function(point, poly, max = TRUE, by_element = FALSE) {

## convert to sfc if necessary
if (inherits(poly, 'Spatial')) poly <- st_as_sfc(poly)
Expand All @@ -40,10 +40,10 @@ point.poly.dist <- function(point, poly, max = T, by_element = F) {
## calculate distance from point to every border vertex
if (st_is_longlat(poly)) {

if (!'geosphere' %in% rownames(utils::installed.packages())) {
stop(paste0('Package geosphere required to calculate distances for ',
'unprojected data\n',
'Install with install.packages("geosphere")\n'))
if (!requireNamespace("geosphere")) {
stop(paste0("Package geosphere required to calculate distances for ",
"unprojected data\n",
"Install with: install.packages(\"geosphere\")\n"))
}

dists <- do.call(rbind,
Expand Down
1 change: 1 addition & 0 deletions R/theme_rw.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#'
#' A ggplot theme with no grid elements or gray background.
#'
#' @return A ggplot [theme][ggplot2::theme] object.
#' @export
#'
#' @examples
Expand Down
4 changes: 4 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This is a resubmission. In this version I have:

* Fixed the Date field in DESCRIPTION from 2020-01-14 to 2021-02-17
* I forgot to confirm the initial resubmission on 2021-01-14, so I am resubmitting again
* Used TRUE and FALSE throughout
* Added a \value to theme_rw.Rd
* Used requireNamespace instead of installed.packages()
* Reset user's par() in vignette spatial-utility-function.Rmd

## Test environments

Expand Down
2 changes: 1 addition & 1 deletion man/overlap.weight.Rd

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

2 changes: 1 addition & 1 deletion man/point.poly.dist.Rd

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

3 changes: 3 additions & 0 deletions man/theme_rw.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-dms2dd.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ test_that("dms2dd works with heterogeneous input", {
"71° 3' 32\" W"),
lat = c("47° 36' 22\"",
"42° 21' 36\" N"),
stringsAsFactors = F)
stringsAsFactors = FALSE)
expect_equal(dms2dd(ll[, 'lon'], ll[, 'lat'])[1,2], 47.60611, tolerance = 1e-3)
})

test_that("dms2dd works with 0 values", {
ll <- data.frame(lon = c("71° 0' 32\" W"),
lat = c("0° 21' 36\" N"),
stringsAsFactors = F)
stringsAsFactors = FALSE)
expect_equal(dms2dd(ll[, 'lon'], ll[, 'lat'])[1,1], -71.00889, tolerance = 1e-3)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-overlap.weight.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ test_that("overlap.weight works with SpatialPolygonsDataFrame", {
})

test_that("overlap.weight works with count", {
expect_equal(overlap.weight(raster_t, polys_t, count = T)[4,4], 2)
expect_equal(overlap.weight(raster_t, polys_t, count = TRUE)[4,4], 2)
})

test_that("overlap.weight works silently", {
expect_silent(overlap.weight(raster_t, polys_t, warn = F))
expect_silent(overlap.weight(raster_t, polys_t, warn = FALSE))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-pointpolydist.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("point.poly.dist works with arguments", {
expect_equal(point.poly.dist(points_t, poly_t, max = FALSE), 61895.43,
tolerance = 1e-3)
expect_equal(point.poly.dist(points_t, poly_t, max = FALSE,
by_element = T)[2], 61895.43,
by_element = TRUE)[2], 61895.43,
tolerance = 1e-3)
})

Expand Down
28 changes: 18 additions & 10 deletions vignettes/spatial-utility-functions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ knitr::opts_chunk$set(
fig.height = 3,
fig.align = 'center'
)
## old graphical parameters
oldpar <- par(mfrow = c(1,2))
```

This package contains convenience functions for carrying out GIS operations
that I have repeatedly encountered in my research. The following packages are
used in this vignette:

```{r setup, message = F, warning = F, results = 'hide'}
```{r setup, message = FALSE, warning = FALSE, results = 'hide'}
library(sf)
library(raster)
library(RWmisc)
Expand Down Expand Up @@ -58,15 +61,15 @@ par(mfrow = c(1, 3))
## plot raw raster values
plot(raster_t, main = 'raw')
plot(polys_t, add = T)
plot(polys_t, add = TRUE)
## plot count of overlapping polygons
plot(overlap.weight(raster_t, polys_t, count = T), main = "count")
plot(polys_t, add = T)
plot(overlap.weight(raster_t, polys_t, count = TRUE), main = "count")
plot(polys_t, add = TRUE)
## plot overlap-weighted raster values
plot(overlap.weight(raster_t, polys_t), main = "weighted")
plot(polys_t, add = T)
plot(polys_t, add = TRUE)
```

# Projecting spatial objects to UTM
Expand Down Expand Up @@ -109,7 +112,7 @@ geographic and projected data.
nc_centroids <- st_centroid(nc)
## calculate maximum distance
point.poly.dist(nc_centroids[53,]$geometry, nc[53,]$geometry, max = T)
point.poly.dist(nc_centroids[53,]$geometry, nc[53,]$geometry, max = TRUE)
```

The following illustration depicts the line connecting the centroid of the
Expand Down Expand Up @@ -137,19 +140,24 @@ nearest_point <- rbind(st_coordinates(nc_points[nearest_ind,]),
## plot
par(mar = rep(0,4))
plot(nc[53,]$geometry)
plot(nc_centroids[53,]$geometry, add = T)
plot(farthest_point, add = T, col = 'red')
plot(nearest_point, add = T, col = 'blue')
plot(nc_centroids[53,]$geometry, add = TRUE)
plot(farthest_point, add = TRUE, col = 'red')
plot(nearest_point, add = TRUE, col = 'blue')
```

Carrying out the same calculations using built-in `sf` functions takes roughly
twice as long to execute.

```{r point-poly-dist-benchmark}
microbenchmark::microbenchmark(pk = point.poly.dist(nc_centroids[53,]$geometry,
nc[53,]$geometry, max = T),
nc[53,]$geometry, max = TRUE),
sf = st_distance(st_cast(st_geometry(nc[53,]),
'POINT')[farthest_ind],
nc_centroids[53,]),
times = 100)
```

```{r oldpar, echo = FALSE}
par(oldpar)
```

0 comments on commit cf6e74b

Please sign in to comment.