Skip to content

Commit

Permalink
Merge pull request #38 from ropensci/fix-crs2
Browse files Browse the repository at this point in the history
Fix crs (2)
  • Loading branch information
robitalec committed Feb 24, 2021
2 parents 28d41bd + 1338353 commit b2e47f6
Show file tree
Hide file tree
Showing 21 changed files with 243 additions and 98 deletions.
49 changes: 34 additions & 15 deletions R/build_lines.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
#' Build Lines
#'
#'
#' \code{build_lines} creates a \code{SpatialLines} object from a \code{data.table}. The function accepts a \code{data.table} with relocation data, individual identifiers a sorting column and a \code{projection}. The relocation data is transformed into \code{SpatialLines} for each individual and optionally, each \code{splitBy}. Relocation data should be in two columns representing the X and Y coordinates.
#'
#' The \code{projection} argument expects a character string defining the EPSG code. For example, for UTM zone 21N (EPSG 32736), the projection argument is "+init=epsg:32736". See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}.
#'
#' The \code{sortBy} is used to order the input \code{data.table} when creating \code{SpatialLines}. It must a \code{POSIXct} to ensure the rows are sorted by date time.
#'
#' The \code{splitBy} argument offers further control building \code{SpatialLines}. If in your \code{DT}, you have multiple temporal groups (e.g.: years) for example, you can provide the name of the column which identifies them and build \code{SpatialLines} for each individual in each year.
#'
#' \code{build_lines} is used by \code{group_lines} for grouping overlapping lines created from relocations.
#'
#' @return \code{build_lines} returns a \code{SpatialLines} object with a line for each individual (and optionally \code{splitBy} combination).
#'
#' An error is returned when an individual has less than 2 relocations, making it impossible to build a line.
#' \code{build_lines} creates a \code{SpatialLines} object from a \code{data.table}.
#' The function accepts a \code{data.table} with relocation data, individual
#' identifiers a sorting column and a \code{projection}. The relocation data
#' is transformed into \code{SpatialLines} for each individual and optionally,
#' each \code{splitBy}. Relocation data should be in two columns representing
#' the X and Y coordinates.
#'
#' The \code{projection} argument expects a character string defining the EPSG
#' code. For example, for UTM zone 36N (EPSG 32736), the projection argument is
#' 'EPSG:32736'. See \url{https://spatialreference.org} for a list of
#' EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ
#' for handling projections, see more at
#' \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}.
#'
#' The \code{sortBy} is used to order the input \code{data.table} when creating
#' \code{SpatialLines}. It must a \code{POSIXct} to ensure the rows are sorted
#' by date time.
#'
#' The \code{splitBy} argument offers further control building \code{SpatialLines}.
#' If in your \code{DT}, you have multiple temporal groups (e.g.: years) for
#' example, you can provide the name of the column which identifies them and
#' build \code{SpatialLines} for each individual in each year.
#'
#' \code{build_lines} is used by \code{group_lines} for grouping overlapping
#' lines created from relocations.
#'
#' @return \code{build_lines} returns a \code{SpatialLines} object with a line
#' for each individual (and optionally \code{splitBy} combination).
#'
#' An error is returned when an individual has less than 2 relocations, making
#' it impossible to build a line.
#'
#' @inheritParams group_lines
#' @inheritParams build_polys
Expand All @@ -36,7 +54,7 @@
#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
#'
#' # EPSG code for example data
#' utm <- '+init=epsg:32736'
#' utm <- 'EPSG:32736'
#'
#' # Build lines for each individual
#' build_lines(DT, projection = utm, id = 'ID', coords = c('X', 'Y'),
Expand Down Expand Up @@ -131,13 +149,14 @@ build_lines <-
if (length(lst) == 0) {
return(NULL)
} else {
proj4string <- sp::CRS(projection)
l <- lapply(seq_along(lst), function(i) {
sp::SpatialLines(list(sp::Lines(sp::Line(
cbind(lst[[i]][[coords[1]]],
lst[[i]][[coords[2]]])
),
names(lst)[[i]])),
proj4string = sp::CRS(projection))
proj4string = proj4string)
})
return(do.call(sp::rbind.SpatialLines, l))
}
Expand Down
72 changes: 52 additions & 20 deletions R/build_polys.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
#' Build Polygons
#'
#' \code{build_polys} creates a \code{SpatialPolygons} object from a \code{data.table}. The function accepts a \code{data.table} with relocation data, individual identifiers, a \code{projection}, \code{hrType} and \code{hrParams}. The relocation data is transformed into \code{SpatialPolygons} for each individual and optionally, each \code{splitBy}. Relocation data should be in two columns representing the X and Y coordinates.
#'
#' The \code{DT} must be a \code{data.table}. If your data is a \code{data.frame}, you can convert it by reference using \code{\link[data.table:setDT]{data.table::setDT}}.
#'
#' The \code{id}, \code{coords} (and optional \code{splitBy}) arguments expect the names of respective columns in \code{DT} which correspond to the individual identifier, X and Y coordinates, and additional grouping columns.
#'
#' The \code{projection} argument expects a character string defining the EPSG code. For example, for UTM zone 21N (EPSG 32736), the projection argument is "+init=epsg:32736". See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}.
#'
#' The \code{hrType} must be either one of "kernel" or "mcp". The \code{hrParams} must be a named list of arguments matching those of \code{adehabitatHR::kernelUD} and \code{adehabitatHR::getverticeshr} or \code{adehabitatHR::mcp}.
#'
#' The \code{splitBy} argument offers further control building \code{SpatialPolygons}. If in your \code{DT}, you have multiple temporal groups (e.g.: years) for example, you can provide the name of the column which identifies them and build \code{SpatialPolygons} for each individual in each year.
#'
#' \code{group_polys} uses \code{build_polys} for grouping overlapping polygons created from relocations.
#'
#' @return \code{build_polys} returns a \code{SpatialPolygons} object with a polyon for each individual (and optionally \code{splitBy} combination).
#'
#' An error is returned when \code{hrParams} do not match the arguments of the \code{hrType} \code{adehabitatHR} function.
#' \code{build_polys} creates a \code{SpatialPolygons} object from a
#' \code{data.table}. The function accepts a \code{data.table} with
#' relocation data, individual identifiers, a \code{projection},
#' \code{hrType} and \code{hrParams}. The relocation data is transformed
#' into \code{SpatialPolygons} for each individual and optionally, each
#' \code{splitBy}. Relocation data should be in two columns representing
#' the X and Y coordinates.
#'
#' The \code{DT} must be a \code{data.table}. If your data is a
#' \code{data.frame}, you can convert it by reference using
#' \code{\link[data.table:setDT]{data.table::setDT}}.
#'
#' The \code{id}, \code{coords} (and optional \code{splitBy}) arguments
#' expect the names of respective columns in \code{DT} which correspond
#' to the individual identifier, X and Y coordinates, and additional
#' grouping columns.
#'
#' The \code{projection} argument expects a character string defining
#' the EPSG code. For example, for UTM zone 36N (EPSG 32736), the projection
#' argument is "EPSG:32736". See \url{https://spatialreference.org}
#' for a list of EPSG codes. Please note, R spatial has followed updates
#' to GDAL and PROJ for handling projections, see more at
#' \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}. It is likely
#' that \code{build_polys} will return "Warning in proj4string(xy) :
#' CRS object has comment, which is lost in output" due to these changes.
#'
#' The \code{hrType} must be either one of "kernel" or "mcp". The
#' \code{hrParams} must be a named list of arguments matching those
#' of \code{adehabitatHR::kernelUD} and \code{adehabitatHR::getverticeshr}
#' or \code{adehabitatHR::mcp}.
#'
#' The \code{splitBy} argument offers further control building
#' \code{SpatialPolygons}. If in your \code{DT}, you have multiple
#' temporal groups (e.g.: years) for example, you can provide the
#' name of the column which identifies them and build \code{SpatialPolygons}
#' for each individual in each year.
#'
#' \code{group_polys} uses \code{build_polys} for grouping overlapping
#' polygons created from relocations.
#'
#' @return \code{build_polys} returns a \code{SpatialPolygons} object
#' with a polyon for each individual (and optionally \code{splitBy}
#' combination).
#'
#' An error is returned when \code{hrParams} do not match the arguments
#' of the \code{hrType} \code{adehabitatHR} function.
#'
#'
#' @inheritParams group_polys
#' @param spPts alternatively, provide solely a SpatialPointsDataFrame with one column representing the ID of each point.
#' @param projection character string defining the EPSG code. For example, for UTM zone 21N (EPSG 32736), the projection argument is "+init=epsg:32736". See details.
#' @param spPts alternatively, provide solely a SpatialPointsDataFrame with one
#' column representing the ID of each point.
#' @param projection character string defining the projection to be passed to
#' \code{sp::CRS}. For example, for UTM zone 36S (EPSG 32736),
#' the projection argument is 'EPSG:32736'. See details.
#' @export
#'
#' @family Build functions
Expand All @@ -38,7 +70,7 @@
#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
#'
#' # EPSG code for example data
#' utm <- '+init=epsg:32736'
#' utm <- 'EPSG:32736'
#'
#' # Build polygons for each individual using kernelUD and getverticeshr
#' build_polys(DT, projection = utm, hrType = 'kernel',
Expand Down
2 changes: 1 addition & 1 deletion R/extdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' @format A data.table with 14297 rows and 5 variables: \describe{
#' \item{ID}{individual identifier} \item{X}{X coordinate of the relocation
#' (UTM 21N)} \item{Y}{Y coordinate of the relocation (UTM 21N)}
#' (UTM 36N)} \item{Y}{Y coordinate of the relocation (UTM 36N)}
#' \item{datetime}{character string representing the date time}
#' \item{population}{sub population within the individuals} }
#'
Expand Down
2 changes: 1 addition & 1 deletion R/get_gbi.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#' DT[, yr := year(datetime)]
#'
#' # EPSG code for example data
#' utm <- '+init=epsg:32736'
#' utm <- 'EPSG:32736'
#'
#' group_polys(DT, area = FALSE, hrType = 'mcp',
#' hrParams = list(percent = 95),
Expand Down
11 changes: 9 additions & 2 deletions R/group_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
#' sorting, timegroup (generated by \code{group_times}) and additional grouping
#' columns.
#'
#' The \code{projection} argument expects a character string defining the EPSG code. For example, for UTM zone 21N (EPSG 32736), the projection argument is "+init=epsg:32736". See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}.
#' The \code{projection} argument expects a character string defining
#' the EPSG code. For example, for UTM zone 36N (EPSG 32736), the projection
#' argument is "EPSG:32736". See \url{https://spatialreference.org}
#' for a list of EPSG codes. Please note, R spatial has followed updates
#' to GDAL and PROJ for handling projections, see more at
#' \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}. It is likely
#' that \code{build_polys} will return "Warning in proj4string(xy) :
#' CRS object has comment, which is lost in output" due to these changes.
#'
#' The \code{sortBy} is used to order the input \code{data.table} when creating
#' \code{SpatialLines}. It must a \code{POSIXct} to ensure the rows are sorted
Expand Down Expand Up @@ -87,7 +94,7 @@
#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
#'
#' # EPSG code for example data
#' utm <- '+init=epsg:32736'
#' utm <- 'EPSG:32736'
#'
#' \donttest{group_lines(DT, threshold = 50, projection = utm, sortBy = 'datetime',
#' id = 'ID', coords = c('X', 'Y'))}
Expand Down
11 changes: 9 additions & 2 deletions R/group_polys.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
#' the names of respective columns in \code{DT} which correspond to the
#' individual identifier, X and Y coordinates, and additional grouping columns.
#'
#' The \code{projection} argument expects a character string defining the EPSG code. For example, for UTM zone 21N (EPSG 32736), the projection argument is "+init=epsg:32736". See \url{https://spatialreference.org} for a list of EPSG codes. Please note, R spatial has followed updates to GDAL and PROJ for handling projections, see more at \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}.
#' The \code{projection} argument expects a character string defining the EPSG
#' code. For example, for UTM zone 36N (EPSG 32736), the projection argument is
#' 'EPSG:32736'. See \url{https://spatialreference.org} for a list of EPSG
#' codes. Please note, R spatial has followed updates to GDAL and PROJ for
#' handling projections, see more at
#' \url{https://www.r-spatial.org/r/2020/03/17/wkt.html}. It is likely
#' that \code{build_polys} will return "Warning in proj4string(xy) :
#' CRS object has comment, which is lost in output" due to these changes.
#'
#' The \code{hrType} must be either one of "kernel" or "mcp". The
#' \code{hrParams} must be a named list of arguments matching those of
Expand Down Expand Up @@ -68,7 +75,7 @@
#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
#'
#' # EPSG code for example data
#' utm <- '+init=epsg:32736'
#' utm <- 'EPSG:32736'
#'
#' group_polys(DT, area = FALSE, hrType = 'mcp',
#' hrParams = list(percent = 95), projection = utm,
Expand Down
14 changes: 7 additions & 7 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For more details, see the [blog post](https://ropensci.org/blog/2018/12/04/spats


## News
New edge-list generating functions added:
Edge-list generating functions added:

* `edge_nn`
* `edge_dist`
Expand All @@ -47,23 +47,23 @@ and dyad id function:

(feedback welcome as always!)

Both documented further in a new vignette: [Using edge list and dyad id functions](https://docs.ropensci.org/spatsoc/articles/using-edge-and-dyad.html).
Both documented further in a vignette: [Using edge list and dyad id functions](https://docs.ropensci.org/spatsoc/articles/using-edge-and-dyad.html).


Also, our article describing `spatsoc` was just published at Methods in Ecology and Evolution. [Link here](https://doi.org/10.1111/2041-210X.13215) and thanks to reviewers and editors at [rOpenSci](https://github.com/ropensci/software-review/issues/237) and at [MEE](https://besjournals.onlinelibrary.wiley.com/journal/2041210x).
Also, our article describing `spatsoc` is published at Methods in Ecology and Evolution. [Link here](https://doi.org/10.1111/2041-210X.13215). Thanks to reviewers and editors at [rOpenSci](https://github.com/ropensci/software-review/issues/237) and at [MEE](https://besjournals.onlinelibrary.wiley.com/journal/2041210x).


More detailed news [here](https://docs.ropensci.org/spatsoc/news/index.html).


## Installation
Install the latest version with `remotes`.

```{r install}
remotes::install_github('ropensci/spatsoc')
# or CRAN
# Stable release
install.packages('spatsoc')
# Development version
remotes::install_github('ropensci/spatsoc')
```

`spatsoc` depends on `rgeos` and requires [GEOS](https://trac.osgeo.org/geos/) installed on the system.
Expand Down
2 changes: 1 addition & 1 deletion man/DT.Rd

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

44 changes: 32 additions & 12 deletions man/build_lines.Rd

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

Loading

0 comments on commit b2e47f6

Please sign in to comment.