Skip to content

Commit

Permalink
version 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha authored and cran-robot committed Dec 16, 2019
0 parents commit bdc2666
Show file tree
Hide file tree
Showing 27 changed files with 1,651 additions and 0 deletions.
31 changes: 31 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,31 @@
Package: SpatialKDE
Type: Package
Title: Kernel Density Estimation for Spatial Data
Version: 0.5.0
URL: https://jancaha.github.io/SpatialKDE/index.html,
https://github.com/JanCaha/SpatialKDE
Authors@R:
person(given = "Jan",
family = "Caha",
role = c("aut", "cre"),
email = "jan.caha@outlook.com",
comment = c(ORCID = "0000-0003-0165-0606"))
Description: Calculate Kernel Density Estimation (KDE) for spatial data.
The algorithm is inspired by the tool 'Heatmap' from 'QGIS'. The method is described by:
Hart, T., Zandbergen, P. (2014) <doi:10.1108/PIJPSM-04-2013-0039>,
Nelson, T. A., Boots, B. (2008) <doi:10.1111/j.0906-7590.2008.05548.x>,
Chainey, S., Tompson, L., Uhlig, S.(2008) <doi:10.1057/palgrave.sj.8350066>.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
VignetteBuilder: knitr
LinkingTo: Rcpp
Imports: Rcpp, sf, dplyr, glue, magrittr, rlang, methods, raster
Suggests: tmap, sp, knitr, testthat (>= 2.1.0)
NeedsCompilation: yes
Packaged: 2019-12-16 08:14:56 UTC; Caha
Author: Jan Caha [aut, cre] (<https://orcid.org/0000-0003-0165-0606>)
Maintainer: Jan Caha <jan.caha@outlook.com>
Repository: CRAN
Date/Publication: 2019-12-16 11:40:06 UTC
2 changes: 2 additions & 0 deletions LICENSE
@@ -0,0 +1,2 @@
YEAR: 2019
COPYRIGHT HOLDER: Jan Caha
26 changes: 26 additions & 0 deletions MD5
@@ -0,0 +1,26 @@
45dac6d9b8643dcc67e022fdc27f2026 *DESCRIPTION
82179e94a9c57510eeb0dfa196cd97cb *LICENSE
f9384b67978f362e89b618ef0b1b5a48 *NAMESPACE
156fd5b034e4172f87effa1677d5cc1b *NEWS.md
46ff90eda42db6d37a5a20a97cecc8d1 *R/RcppExports.R
e11ac6cfc676e4a3d0ebc35c4492e8de *R/create_grid.R
bb39c117653c3bf19fcde4d7aea1dd6c *R/create_raster.R
eefb7801ac5594fea1f9bad03ccfc4a8 *R/kde.R
ad3f3c64fcdb96e66b1859bd35c48478 *R/utils-pipe.R
ea034c24e4e96a741e65721835b733cf *R/utils.R
6e0dc28feda82e7c39bb6b1638ad036b *R/zzz.R
fc09e35d33437d635f6361e42c35696e *README.md
0e95615d823df27d4b2a94e7be3a749a *build/vignette.rds
512913a6d2935ccb9dc5d5dc73640404 *inst/doc/SpatialKDE.R
abf6cca3ed36a52d85d0c50a4c33a8d2 *inst/doc/SpatialKDE.Rmd
f70b055123458caa7ef0afa06fc47d4b *inst/doc/SpatialKDE.html
fc333f44de04b8cb7aec0ce9c2d3ff15 *man/create_grid.Rd
89e5d758105098f7ca26068280fd22ad *man/create_raster.Rd
00277ed05f610057238f0007553ded4e *man/kde.Rd
1f7896a1b866ff9ae89ba35be7c7b6f1 *man/pipe.Rd
c8e651984bf03903e92a85a54f4e79b4 *src/RcppExports.cpp
746686d03189a416b9c16fb0536c1489 *src/kde.cpp
70b197edb62a1a7250b6a1aa009a5ecd *tests/testthat.R
78b8bef43e80f100e302e047fabaa9af *tests/testthat/test-create_grids_raster.R
9ba68f18046da3b5bd6f4474671a677b *tests/testthat/test-kde.R
abf6cca3ed36a52d85d0c50a4c33a8d2 *vignettes/SpatialKDE.Rmd
38 changes: 38 additions & 0 deletions NAMESPACE
@@ -0,0 +1,38 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(create_grid_hexagonal)
export(create_grid_rectangular)
export(create_raster)
export(kde)
importFrom(Rcpp,evalCpp)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(magrittr,"%>%")
importFrom(methods,setMethod)
importFrom(raster,crs)
importFrom(raster,isLonLat)
importFrom(raster,raster)
importFrom(raster,values)
importFrom(raster,xyFromCell)
importFrom(rlang,.data)
importFrom(rlang,enquo)
importFrom(rlang,quo_text)
importFrom(sf,st_as_sf)
importFrom(sf,st_bbox)
importFrom(sf,st_buffer)
importFrom(sf,st_centroid)
importFrom(sf,st_convex_hull)
importFrom(sf,st_coordinates)
importFrom(sf,st_covered_by)
importFrom(sf,st_geometry)
importFrom(sf,st_geometry_type)
importFrom(sf,st_intersects)
importFrom(sf,st_is_longlat)
importFrom(sf,st_make_grid)
importFrom(sf,st_sf)
importFrom(sf,st_union)
useDynLib(SpatialKDE)
13 changes: 13 additions & 0 deletions NEWS.md
@@ -0,0 +1,13 @@
# SpatialKDE 0.4.0

* rename of `create_raster_rectangular()` to `create_grid_rectangular()` and `create_raster_hexagonal()` to `create_grid_hexagonal()` to avoid confusion about the type of outcome.

* fix of inner workings of `create_grid_rectangular()` and `create_grid_hexagonal()`

* fix usage of kernell in function `kde()`

* Added a `NEWS.md` file to track changes to the package.

# SpatialKDE 0.3.2

* Website via pkgdown
7 changes: 7 additions & 0 deletions R/RcppExports.R
@@ -0,0 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

kde_estimate <- function(fishnet, points, bw, kernel, scaled = FALSE, decay = 1) {
.Call('_SpatialKDE_kde_estimate', PACKAGE = 'SpatialKDE', fishnet, points, bw, kernel, scaled, decay)
}

90 changes: 90 additions & 0 deletions R/create_grid.R
@@ -0,0 +1,90 @@
#' Create grid
#'
#' Create grid of equaly spaced rectangles or hexagons. The distance between centre points
#' in both x and y dimension is equal to \code{cell_size}. The function is effectively a wrapper around
#' \code{\link[sf]{st_make_grid}} with a little bit of preprocessing including generation of grid only inside
#' \code{\link[sf]{st_convex_hull}}.
#'
#' @param geometry \code{\link[sf]{sf}} \code{data.frame} containing geometry which should be cover by
#' the grid.
#' @param cell_size \code{numeric} specifing the distance for equally spaced centers of polygons
#' (rectangular or hexagonal).
#' @param side_offset \code{numeric} specifing the side offset, distance added to the convex hull
#' of input geometry to generate grid for KDE. Good estimate is usually the same value as band width of KDE.
#' @param only_inside \code{logical} specifing if the grid cells should be generated only inside of the
#' geometry. Default value is \code{FALSE}.
#'
#' @return \code{\link[sf]{sf}} \code{data.frame}.
#' @export
#'
#' @describeIn create_grid Create rectangular grid
#'
#' @examples
#' library(sf)
#' nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% st_transform(32031)
#' grid <- create_grid_hexagonal(nc, cell_size = 100000)
#' grid <- create_grid_rectangular(nc, cell_size = 100000, only_inside = TRUE)
#'
create_grid_rectangular <- function(geometry, cell_size, side_offset = 0, only_inside = FALSE){

.create_grid(geometry, cell_size, side_offset, only_inside, square = TRUE)
}

#' @export
#'
#' @describeIn create_grid Create hexagonal grid
create_grid_hexagonal <- function(geometry, cell_size, side_offset = 0, only_inside = FALSE){

.create_grid(geometry, cell_size, side_offset, only_inside, square = FALSE)
}

#' @importFrom sf st_geometry st_union st_convex_hull st_buffer st_make_grid
#' @importFrom sf st_sf st_intersects st_covered_by
#' @importFrom rlang .data
#' @importFrom dplyr mutate filter select
.create_grid <- function(geometry,
cell_size,
side_offset = 0,
only_inside = FALSE,
square = TRUE) {

.validate_sf(geometry)

.validate_sideoffset(side_offset)

.validate_cellsize(cell_size)

if (!(typeof(only_inside) == "logical")) {
stop(glue::glue(
"Parameter `only_inside` must be \"logical\". Currently it is of type: `{typeof(only_inside)}`."
))
}

buff_convex_hull <- geometry %>%
sf::st_geometry() %>%
sf::st_union() %>%
sf::st_convex_hull() %>%
sf::st_buffer(side_offset)

grid <- buff_convex_hull %>%
sf::st_make_grid(cellsize = cell_size,
what = "polygons",
square = square) %>%
sf::st_sf()

if (only_inside) {

grid <- grid %>%
dplyr::mutate(covered = as.numeric(sf::st_covered_by(grid, buff_convex_hull))) %>%
dplyr::filter(!is.na(.data$covered))

} else {

grid <- grid %>%
dplyr::mutate(intersect = as.numeric(sf::st_intersects(grid, buff_convex_hull))) %>%
dplyr::filter(!is.na(.data$intersect))
}

grid %>%
dplyr::select()
}
42 changes: 42 additions & 0 deletions R/create_raster.R
@@ -0,0 +1,42 @@
#' Create raster
#'
#' Create raster of equaly spaced cells. The distance between centre of cells
#' in both x and y dimension is equal to \code{cell_size}.
#'
#' @param geometry \code{\link[sf]{sf}} \code{data.frame} containing geometry which should be cover by
#' the raster.
#' @param cell_size \code{numeric} specifing the distance for equally spaced cells.
#' @param side_offset \code{numeric} specifing the side offset, distance added to the convex hull
#' of input geometry to generate raster for KDE. Good estimate is usually the same value as band width of KDE.
#'
#' @return \code{\link[raster]{Raster-class}}
#' @export
#'
#' @importFrom sf st_convex_hull st_buffer st_geometry st_union st_as_sf
#' @importFrom raster raster
#'
#' @examples
#' library(sf)
#' nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% st_transform(32031)
#' raster <- create_raster(nc, cell_size = 100000)
#'
create_raster <- function(geometry, cell_size, side_offset = 0){

.validate_sf(geometry)

.validate_sideoffset(side_offset)

.validate_cellsize(cell_size)

buffered_geometry <- geometry %>%
sf::st_geometry() %>%
sf::st_union() %>%
sf::st_convex_hull() %>%
sf::st_buffer(side_offset) %>%
sf::st_as_sf()

raster <- raster::raster(buffered_geometry,
resolution = c(cell_size, cell_size))

raster
}

0 comments on commit bdc2666

Please sign in to comment.