Learn R Programming

⚠️There's a newer version (0.4.4.6) of this package.Take me there.

spNetwork

A R package to perform spatial analysis on networks.

The package’s website is available here

What is this package ?

This package can be used to perform several types of analysis on geographical networks. This type of network have spatial coordinates associated with their nodes. They can be directed or undirected. In the actual development version the implemented methods are:

  • Network Kernel Density Estimate, a method estimating density of a point pattern constrained on a network (see the vignettes Network Kernel Density Estimate and Details about NKDE).
  • Spatial weight matrices based on network distances, which can be used in a great number of traditional methods in spatial analysis (see the vignette Spatial Weight Matrices).
  • Network k Functions, used to investigate the spatial distribution of a set of points on a network at several scales (see the vignette Network k Functions).
  • K nearest neighbours, to calculate for each point on a network its K nearest neighbour (see the function network_knn).
  • Graph analysis, using the functions of the package igraph (see the vignette Building graphs)
  • Isochrones, to delineate accessible area around points localized on a network (see the vignette Calculating isochrones)

Calculation on network can be long, efforts were made to reduce computation time by implementing several core functions with Rcpp and RcppArmadillo and by using multiprocessing when possible.

Installing

you can install the CRAN version of this package with the following code in R.

install.packages("spNetwork")

To use all the new features before they are available in the CRAN version, you can download the development version.

devtools::install_github("JeremyGelb/spNetwork")

The packages uses mainly the following packages in its internal structure :

  • igraph
  • sp
  • rgeos
  • maptools
  • raster
  • future
  • future.apply
  • data.table
  • SearchTrees
  • Rcpp
  • RcppArmadillo

Some examples

We provide here some short examples of several features. Please, check the vignettes for more details.

  • realizing a kernel network density estimate
library(spNetwork)
library(tmap)
library(rgdal)

# loading the dataset
networkgpkg <- system.file("extdata", "networks.gpkg",
                           package = "spNetwork", mustWork = TRUE)
eventsgpkg <- system.file("extdata", "events.gpkg",
                          package = "spNetwork", mustWork = TRUE)
mtl_network <- readOGR(networkgpkg,layer="mtl_network",verbose = FALSE)
bike_accidents <- readOGR(eventsgpkg,layer="bike_accidents", verbose = FALSE)


# generating sampling points at the middle of lixels
samples <- lines_points_along(mtl_network, 50)

# calculating densities
densities <- nkde(mtl_network,
                 events = bike_accidents,
                 w = rep(1,nrow(bike_accidents)),
                 samples = samples,
                 kernel_name = "quartic",
                 bw = 300, div= "bw",
                 method = "discontinuous",
                 digits = 2, tol =  0.1,
                 grid_shape = c(1,1),
                 max_depth = 8,
                 agg = 5, sparse = TRUE,
                 verbose = FALSE)

densities <- densities*1000
samples$density <- densities



tm_shape(samples) + 
  tm_dots(col = "density", size = 0.05, palette = "viridis",
          n = 7, style = "kmeans")

* Building a spatial matrix based on network distance

library(spdep)

# creating a spatial weight matrix for the accidents
listw <- network_listw(bike_accidents,
                       mtl_network,
                       mindist = 10,
                       maxdistance = 400,
                       dist_func = "squared inverse",
                       line_weight = 'length',
                       matrice_type = 'W',
                       grid_shape = c(1,1),
                       verbose=FALSE)

# using the matrix to find isolated accidents (more than 500m)
no_link <- sapply(listw$neighbours, function(n){
  if(n == 0){
    return(TRUE)
  }else{
    return(FALSE)
  }
})

bike_accidents$isolated <- as.factor(ifelse(no_link,
                                  "isolated","not isolated"))

tm_shape(mtl_network) + 
  tm_lines(col = "black") +
  tm_shape(bike_accidents) + 
  tm_dots(col = "isolated", size = 0.1,
          palette = c("isolated" = "red","not isolated" = "blue"))

Note that you can use this in every spatial analysis you would like to perform. With the converter function of spdep (like listw2mat), you can convert the listw object into regular matrix if needed

  • Calculating k function
# loading the data
networkgpkg <- system.file("extdata", "networks.gpkg",
                           package = "spNetwork", mustWork = TRUE)
eventsgpkg <- system.file("extdata", "events.gpkg",
                          package = "spNetwork", mustWork = TRUE)

main_network_mtl <- rgdal::readOGR(networkgpkg,layer="main_network_mtl", verbose = FALSE)
mtl_theatres <- rgdal::readOGR(eventsgpkg,layer="mtl_theatres", verbose = FALSE)

# calculating the k function
kfun_theatre <- kfunctions(main_network_mtl, mtl_theatres,
                           start = 0, end = 5000, step = 50, 
                           width = 1000, nsim = 50, resolution = 50,
                           verbose = FALSE, conf_int = 0.05)
kfun_theatre$plotg

Work in progress

New methods will be probably added in the future, but we will focus on performance for the next release. Do no hesitate to open an issue here if you have suggestion or if you encounter a bug.

Features that will be added to the package in the future:

  • temporal NKDE, a two dimensional kernel density estimation in network space and time
  • accessibility measures based on distance matrix between population locations and services

Authors

  • Jeremy Gelb - Creator and maintainer

Contribute

To contribute to spNetwork, please follow these guidelines.

Please note that the spNetwork project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

spNetwork version 0.1.1 is licensed under GPL2 License.

Acknowledgments

  • Hat tip to Philippe Apparicio for his support during the development
  • Hat tip to Hadley Wickham and his helpful book R packages

Copy Link

Version

Install

install.packages('spNetwork')

Monthly Downloads

559

Version

0.2.1

License

GPL-2

Maintainer

Jeremy Gelb

Last Published

October 30th, 2021

Functions in spNetwork (0.2.1)

aggregate_points

Events aggregation
add_vertices_lines

Add vertices to a SpatialLinesDataFrame
build_graph_directed

Directed network generation
bw_cv_likelihood_calc

Bandwidth selection by likelihood cross validation
build_graph

Network generation
adaptive_bw.mc

Adaptive bandwidth (multicore)
build_quadtree

Build a quadtree
build_grid

Spatial grid
add_center_lines

Add center vertex to lines
adaptive_bw

Adaptive bandwidth
calc_isochrones

Isochrones calculation
calc_gamma

Gamma parameter for Abramson<U+2019>s adaptive bandwidth
bw_cvl_calc.mc

Bandwidth selection by Cronie and Van Lieshout's Criterion (multicore version)
bw_cvl_calc

Bandwidth selection by Cronie and Van Lieshout's Criterion
bw_cv_likelihood_calc.mc

Bandwidth selection by likelihood cross validation (multicore version)
corrfactor_continuous_sparse

A function to calculate the necessary information to apply the Diggle correction factor with a continuous method (sparse)
clean_events

Clean events geometries
discontinuousWorker_int

The worker function to calculate discontinuous NKDE (with ARMADILLO and Integer matrix)
discontinuousWorker_sparse

The worker function to calculate discontinuous NKDE (with ARMADILLO and sparse matrix)
corrfactor_discontinuous

A function to calculate the necessary informations to apply the Diggle correction factor with a discontinuous method
corrfactor_continuous

A function to calculate the necessary information to apply the Diggle correction factor with a continuous method
corrfactor_discontinuous_sparse

A function to calculate the necessary information to apply the Diggle correction factor with a discontinuous method (sparse)
continuousWorker

The worker function to calculate continuous NKDE (with ARMADILLO and integer matrix)
check_geometries

Geometry sanity check
gaussian_kernelos

c++ gaussian kernel for one distance
epanechnikov_kernel

Epanechnikov kernel
cosine_kernel

Cosine kernel
closest_points

Find closest points
cross_gfun

Base cross g-function
corrfactor_simple

Simple NKDE border correction
lines_extremities

Get lines extremities
epanechnikov_kernel_cpp

c++ epanechnikov kernel
gaussian_kernel_scaledos

c++ scaled gaussian kernel for one distance
continuousfunction

The main function to calculate continuous NKDE (with ARMADILO and sparse matrix)
gaussian_kernel

Gaussian kernel
lines_points_along

Points along lines
cosine_kernel_cpp

c++ cosine kernel
cross_kfunc

Base cross k-function
gaussian_kernel_cpp

c++ gaussian kernel
correction_factor

Border correction for NKDE
dist_mat_dupl

Distance matrix with dupicated
discontinuousfunction

The main function to calculate discontinuous NKDE (ARMA and sparse matrix)
lines_center

Centre points of lines
lines_direction

Unify lines direction
get_loo_values_discontinuous

The exported function to calculate discontinuous NKDE likelihood cv
cosine_kernelos

c++ cosine kernel for one distance
get_loo_values_continuous

The exported function to calculate continuous NKDE likelihood cv
get_loo_values_simple

The exported function to calculate simple NKDE likelihood cv
continuousWorker_sparse

The worker function to calculate continuous NKDE (with ARMADILLO and sparse matrix)
cross_kfunctions.mc

Network cross k and g functions (multicore, maturing)
cross_kfunctions

Network cross k and g functions (maturing)
epanechnikov_kernelos

c++ epanechnikov kernel for one distance
heal_edges

Heal edges
nearestPointOnSegment

Nearest point on segment
nearestPointOnLine

Nearest point on Line
esc_kernel_loo

The recursive function to calculate continuous NKDE likelihood cv
kfunc

Base k-function
network_knn.mc

K-nearest points on network (multicore version)
network_knn

K-nearest points on network
nearest_lines

Nearest line for points
gaussian_kernel_scaled

Scaled gaussian kernel
quartic_kernel_cpp

c++ quartic kernel
gaussian_kernel_scaled_cpp

c++ scale gaussian kernel
nkde_worker_bw_sel

Worker function for bandwidth selection by likelihood cross validation
reverse_lines

Reverse lines
nkde

Network Kernel density estimate
network_knn_worker

worker function for K-nearest points on network
nkde_worker_bw_sel_cvl

Worker function for bandwidth selection by Cronie and Van Lieshout's Criterion
gm_mean

Geometric mean
network_listw_worker

network_listw worker
prepare_data

Prior data preparation
plot_graph

Plot graph
graph_checking

Topological error
cut_lines_at_distance

Cut lines at a specified distance
lixelize_lines

Cut lines into lixels
kfunctions.mc

Network k and g functions (multicore, maturing)
kfunctions

Network k and g functions (maturing)
gfunc

Base g-function
sanity_check_knn

Sanity check for the knn functions
quartic_kernelos

c++ quartic kernel for one distance
select_dist_function

Select the distance to weight function
split_by_grid_abw.mc

Split data with a grid for the adaptive bw function (multicore)
direct_lines

Make a network directed
prepare_elements_netlistw

Data preparation for network_listw
esd_kernel_loo

The function to calculate discontinuous NKDE likelihood cv
quartic_kernel

Quartic kernel
split_graph_components

Split graph components
uniform_kernel

Uniform kernel
triweight_kernelos

c++ triweight kernel for one distance
select_kernel

Select kernel function
simplify_network

Simplify a network
snapPointsToLines2

Snap points to lines
split_border

Split boundary of polygon
split_by_grid

Split data with a grid
ess_kernel

Worker for simple NKDE algorithm
uniform_kernelos

c++ uniform kernel for one distance
uniform_kernel_cpp

c++ uniform kernel
network_listw

Network distance listw
tricube_kernel_cpp

c++ tricube kernel
split_lines_at_vertex

Split lines at vertices in a SpatialLinesDataFrame
simple_lines

LineString to simple Line
network_listw.mc

Network distance listw (multicore)
remove_loop_lines

Remove loops
surrounding_points

Points along polygon boundary
remove_mirror_edges

Remove mirror edges
simple_nkde

Simple NKDE algorithm
tricube_kernelos

c++ tricube kernel for one distance
sp_char_index

Coordinates to unique character vector
spatial_request

Spatial request
triweight_kernel_cpp

c++ triweight kernel
triweight_kernel

Triweight kernel
nkde.mc

Network Kernel density estimate (multicore)
lixelize_lines.mc

Cut lines into lixels (multicore)
triangle_kernel

triangle kernel
triangle_kernel_cpp

c++ triangle kernel
randomize_distmatrix

Points on network randomization
nkde_worker

NKDE worker
randomize_distmatrix2

Points on network randomization simplified
split_by_grid.mc

Split data with a grid (multicore)
split_by_grid_abw

Split data with a grid for the adaptive bw function
triangle_kernelos

c++ triangle kernel for one distance
tricube_kernel

Tricube kernel