Skip to content

Commit

Permalink
Merge pull request #20 from jhollist/spatial_updates
Browse files Browse the repository at this point in the history
Spatial updates
  • Loading branch information
jhollist authored Sep 15, 2023
2 parents 75c289a + 6ca7944 commit 0d9102a
Show file tree
Hide file tree
Showing 31 changed files with 304 additions and 243 deletions.
File renamed without changes.
98 changes: 98 additions & 0 deletions .github/workflows/check-standard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions

on: [push, pull_request]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: 'oldrel'}
- {os: macos-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-latest, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v2

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v3
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: "Install spatial libraries on linux"
if: runner.os == 'Linux'
run: sudo apt-get install libgdal-dev libproj-dev libgeos-dev libudunits2-dev

- name: "Install spatial libraries on macOS"
if: runner.os == 'macOS'
run: |
# conflicts with gfortran from r-lib/actions when linking gcc
# rm '/usr/local/bin/gfortran'
brew install pkg-config gdal proj geos sqlite3
- name: Install dependencies on windows and mac
if: runner.os != 'Linux'
run: |
remotes::install_deps(dependencies = TRUE, type = "binary")
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Install dependencies linux
if: runner.os == 'Linux'
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: lakemorpho
Type: Package
Title: Lake Morphometry Metrics
Version: 1.2.1.9999
Version: 1.3.0
Authors@R: c(
person(given = "Jeffrey W.", family = "Hollister",
role = c("aut","cre"),
Expand Down Expand Up @@ -32,9 +32,7 @@ Imports:
methods,
stats,
grDevices,
rgdal,
raster,
rgeos,
sp,
sf,
geosphere,
Expand Down
8 changes: 2 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import(geosphere)
import(grDevices)
import(methods)
import(raster)
import(rgdal)
import(rgeos)
import(sp)
importFrom(cluster,ellipsoidhull)
importFrom(rgeos,gLength)
importFrom(sf,st_as_sf)
importFrom(sf,st_bbox)
importFrom(sf,st_cast)
importFrom(sf,st_centroid)
importFrom(sf,st_coordinates)
importFrom(sf,st_crs)
importFrom(sf,st_geometry)
Expand All @@ -38,9 +37,6 @@ importFrom(sf,st_sf)
importFrom(sf,st_sfc)
importFrom(sf,st_transform)
importFrom(sf,st_within)
importFrom(sp,CRS)
importFrom(sp,SpatialLines)
importFrom(sp,proj4string)
importFrom(stats,dist)
importFrom(stats,lm)
importFrom(stats,median)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
lakemorpho 1.2.1 (2022-04-25)
lakemorpho 1.99.0 (2023-09-XX)
==========================

## Major Changes
- dropping rgdal and rgeos, switching all vector to sf, coerces sp to sf.
- removed travis and appveyor


## Bug fixes
- @aarohall caught a bug with user supplied catchment returning a Null
surrounding landscape.
Expand Down
2 changes: 1 addition & 1 deletion R/calcLakeMetrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

calcLakeMetrics <- function(inLakeMorpho, bearing, pointDens, slope_quant=0.5,
correctFactor = 1, zmax = NULL) {
if (class(inLakeMorpho) != "lakeMorpho") {
if (!inherits(inLakeMorpho, "lakeMorpho")) {
return(warning("Input data is not of class 'lakeMorpho'. Run lakeSurround Topo first."))
}
allMet <- list(surfaceArea = lakeSurfaceArea(inLakeMorpho),
Expand Down
8 changes: 4 additions & 4 deletions R/lakeFetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
lakeFetch <- function(inLakeMorpho, bearing, addLine = TRUE) {

inputName <- deparse(substitute(inLakeMorpho))
if (class(inLakeMorpho) != "lakeMorpho") {
if (!inherits(inLakeMorpho, "lakeMorpho")) {
stop("Input data is not of class 'lakeMorpho'. Run lakeSurround Topo or lakeMorphoClass first.")
}
result <- NA
Expand Down Expand Up @@ -78,7 +78,7 @@ lakeFetch <- function(inLakeMorpho, bearing, addLine = TRUE) {
# needs centroid, not all coords
# Slight difference with original which returned the label point which is
# kinda the centroid...
centPts[[1]] <- data.frame(st_coordinates(sf::st_centroid(lakedd)))
centPts[[1]] <- data.frame(st_coordinates(sf::st_centroid(sf::st_geometry(lakedd))))
names(centPts[[1]]) <- c("lon", "lat")
centPts[[2]] <- destPoint(as.matrix(centPts[[1]]), perpbear1, 100)
i <- length(centPts)
Expand Down Expand Up @@ -113,7 +113,7 @@ lakeFetch <- function(inLakeMorpho, bearing, addLine = TRUE) {
allLinesSL <- st_sfc(geometry = allLines, crs = st_crs("+proj=longlat +datum=WGS84"))
# clip out lines that are inside lake
lakeLinesSL <- sf::st_intersection(allLinesSL, lakedd)
lakeLinesSL_proj <- sf::st_transform(lakeLinesSL, st_crs(st_as_sf(inLakeMorpho$lake))$proj4string)
lakeLinesSL_proj <- sf::st_transform(lakeLinesSL, st_crs(inLakeMorpho$lake))

# Determine the longest
lakeLinesSL_proj <- sf::st_as_sf(sf::st_cast(sf::st_cast(lakeLinesSL_proj, "MULTILINESTRING"),"LINESTRING"))
Expand All @@ -135,5 +135,5 @@ lakeFetch <- function(inLakeMorpho, bearing, addLine = TRUE) {
class(inLakeMorpho) <- "lakeMorpho"
assign(inputName, inLakeMorpho, envir = parent.frame())
}
return(round(result,4))
return(round(as.numeric(result),4))
}
20 changes: 9 additions & 11 deletions R/lakeMajorAxisLength.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@
#' internal waves in the bottom boundary layer of ice-covered Lake Mueggelsee,
#' Germany. Aquatic ecology, 43(3), pp.641-651.
#'
#' @importFrom rgeos gLength
#' @importFrom cluster ellipsoidhull
#' @examples
#' data(lakes)
#' inputLM <- lakeSurroundTopo(exampleLake, exampleElev)
#' lakeMajorAxisLength(inputLM)


lakeMajorAxisLength <- function(inLakeMorpho, addLine = TRUE) {

if (class(inLakeMorpho) != "lakeMorpho") {
if (!inherits(inLakeMorpho, "lakeMorpho")) {
stop("Input data is not of class 'lakeMorpho'. Run lakeSurround Topo or lakeMorphoClass first.")
}

result <- NA
#Change to perhaps deal with noLD
lakeShoreLine <- as(inLakeMorpho$lake, "SpatialLines")
lakeShorePoints <- as(lakeShoreLine, "SpatialPoints")
lakeShoreCoords <- coordinates(lakeShorePoints)
lakeShoreLine <- sf::st_cast(inLakeMorpho$lake, "MULTILINESTRING")
lakeShorePoints <- sf::st_cast(sf::st_geometry(lakeShoreLine), "MULTIPOINT")
lakeShoreCoords <- sf::st_coordinates(lakeShorePoints)[,-3]

# https://stackoverflow.com/questions/18278382/how-to-obtain-the-lengths-of-semi-axes-of-an-ellipse-in-r
elpshull <- predict(cluster::ellipsoidhull(lakeShoreCoords))
Expand All @@ -48,16 +47,15 @@ lakeMajorAxisLength <- function(inLakeMorpho, addLine = TRUE) {
} else {
myLine.max <- elpshull[round(dist2center,8) == round(max(dist2center),8),]
}

myLine <- st_sfc(sf::st_linestring(myLine.max), crs = sf::st_crs(inLakeMorpho$lake))

myLine <- sp::SpatialLines(list(Lines(list(Line(myLine.max)), "1")),
proj4string = sp::CRS(sp::proj4string(inLakeMorpho$lake)))

result <- rgeos::gLength(myLine)

result <- as.numeric(sf::st_length(myLine))
if (addLine) {
myName <- deparse(substitute(inLakeMorpho))
inLakeMorpho$majoraxisLengthLine <- NULL
inLakeMorpho <- c(inLakeMorpho, majoraxisLengthLine = myLine)
inLakeMorpho$majoraxisLengthLine <- myLine
class(inLakeMorpho) <- "lakeMorpho"
assign(myName, inLakeMorpho, envir = parent.frame())
}
Expand Down
2 changes: 1 addition & 1 deletion R/lakeMaxDepth.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' lakeMaxDepth(inputLM)

lakeMaxDepth <- function(inLakeMorpho, slope_quant = 0.5, correctFactor = 1) {
if (class(inLakeMorpho) != "lakeMorpho") {
if (!inherits(inLakeMorpho, "lakeMorpho")) {
stop("Input data is not of class 'lakeMorpho'. Run lakeSurround Topo or lakeMorphoClass first.")
}
if(is.null(inLakeMorpho$elev)){
Expand Down
33 changes: 22 additions & 11 deletions R/lakeMaxLength.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,25 @@
#' - Lake Morphometry (2nd ed.). Gainesville: Florida LAKEWATCH,
#' Department of Fisheries and Aquatic Sciences.
#' \href{http://edis.ifas.ufl.edu/pdffiles/FA/FA08100.pdf}{Link}
#' @import sp rgeos methods
#' @import sp methods
#' @importFrom stats dist
#' @examples
#' library(lakemorpho)
#' data(lakes)
#' inputLM <- lakeSurroundTopo(exampleLake, exampleElev)
#' lakeMaxLength(inputLM,50)


lakeMaxLength <- function(inLakeMorpho, pointDens, addLine = TRUE) {
if (class(inLakeMorpho) != "lakeMorpho") {

if (!inherits(inLakeMorpho, "lakeMorpho")) {
stop("Input data is not of class 'lakeMorpho'. Run lakeSurround Topo or lakeMorphoClass first.")
}
result <- NA
lakeShorePoints <- spsample(as(inLakeMorpho$lake, "SpatialLines"), pointDens, "regular")@coords
#lakeShorePoints <- spsample(as(inLakeMorpho$lake, "SpatialLines"), pointDens, "regular")@coords
lakeShorePoints <- st_coordinates(sf::st_sample(st_cast(inLakeMorpho$lake,
"MULTILINESTRING"),
pointDens, type = "regular"))
dm <- dist(lakeShorePoints)
md <- nrow(lakeShorePoints)
x0 <- lakeShorePoints[which(lower.tri(matrix(1, md, md)) == 1, arr.ind = TRUE)[, 1], ][, 1][order(dm, decreasing = TRUE)] #[30:md]
Expand All @@ -50,24 +56,29 @@ lakeMaxLength <- function(inLakeMorpho, pointDens, addLine = TRUE) {
y1 <- lakeShorePoints[which(lower.tri(matrix(1, md, md)) == 1, arr.ind = TRUE)[, 2], ][, 2][order(dm, decreasing = TRUE)] #[30:md]
xydf <- data.frame(x0, x1, y0, y1)
xylist <- split(xydf, rownames(xydf))
myLines <- SpatialLines(lapply(xylist, function(x) Lines(list(Line(matrix(as.numeric(x), 2, 2))), row.names(x))),
proj4string = CRS(proj4string(inLakeMorpho$lake)))
myInd <- gContains(inLakeMorpho$lake, myLines, byid = TRUE)
#myLines_old <- SpatialLines(lapply(xylist, function(x) Lines(list(Line(matrix(as.numeric(x), 2, 2))), row.names(x))),
# proj4string = CRS(st_crs(inLakeMorpho$lake)$wkt))
myLines <- st_sfc(lapply(xylist,
function(x) st_linestring(matrix(as.numeric(x),2,2))),
crs = sf::st_crs(inLakeMorpho$lake))
#myInd_old <- gContains(as(inLakeMorpho$lake, "Spatial"), myLines_old, byid = TRUE)

myInd <- sf::st_contains(inLakeMorpho$lake, myLines, sparse = FALSE)[1,]
if (sum(myInd) == 0) {
return(NA)
}
if(capabilities("long.double")){
myLine <- myLines[myInd][gLength(myLines[myInd], byid = TRUE) == max(gLength(myLines[myInd], byid = TRUE))]
myLine <- myLines[myInd][sf::st_length(myLines[myInd]) == max(sf::st_length(myLines[myInd]))]
} else {
myLine <- myLines[myInd][round(gLength(myLines[myInd], byid = TRUE),8) == round(max(gLength(myLines[myInd], byid = TRUE)),8)]
myLine <- myLines[myInd][round(sf::st_length(myLines[myInd]),8) == round(max(sf::st_length(myLines[myInd])),8)]
}

result <- gLength(myLine)

result <- as.numeric(sf::st_length(myLine))
if (addLine) {
myName <- deparse(substitute(inLakeMorpho))
inLakeMorpho$maxLengthLine <- NULL
inLakeMorpho <- c(inLakeMorpho, maxLengthLine = myLine)
inLakeMorpho$maxLengthLine <- myLine
class(inLakeMorpho) <- "lakeMorpho"
assign(myName, inLakeMorpho, envir = parent.frame())
}
Expand Down
Loading

0 comments on commit 0d9102a

Please sign in to comment.