R/GeoFunctions.R

Defines functions boundaries distcenter distance perimeter contig area

Documented in area boundaries contig distance distcenter perimeter

library(rgdal)
library(spdep)
library(rgeos)
library(measurements)


################################################## 

# GEOGRAPHIC FUNCTIONS

################################################## 


#' A function to compute the spatial units' areas
#'
#' @usage area(spatobj = NULL, folder = NULL, shape = NULL)
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @return A area vector
#' @description The function is based on \pkg{rgdal} package and can be 
#' used with a shape file or an R spatial object (SpatialPolygonsDataFrame).
#' @examples  area(segdata) 
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' area(folder = foldername, shape = shapename)
#' @seealso  Other spatial functions used for segregation indices 
#' computation: \code{\link{contig}}, \code{\link{perimeter}}, 
#' \code{\link{distance}}, \code{\link{distcenter}}, 
#' \code{\link{boundaries}}
#' @importFrom methods slot
#' @export

area <- function(spatobj = NULL, folder = NULL, shape = NULL) {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    provi <- slot(spatobj, "polygons")
    area <- sapply(provi, slot, "area")
    return(area)
}

#' A function to compute the contiguity matrix
#'
#' @usage contig(spatobj = NULL, folder = NULL, shape = NULL, queen = FALSE)
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @param queen = TRUE for queen criteria, FALSE (by default)  for rook criteria
#' @return A first order contiguity (adjacency) matrix, where each 
#' element [\emph{i,j}] equals 1 if \emph{i}-th  and \emph{j}-th  
#' spatial units are adjacent, 0 otherwise (queen or rook criteria)
#' @description The function is based on \pkg{rgdal} and 
#' \pkg{spdep} packages and can be used with a shape file 
#' or an R spatial object (SpatialPolygonsDataFrame).
#' @examples  contig(segdata) 
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' contig(folder = foldername, shape = shapename)
#' @seealso  Other spatial functions used for segregation indices 
#' computation: \code{\link{area}}, \code{\link{perimeter}}, 
#' \code{\link{distance}}, \code{\link{distcenter}}, 
#' \code{\link{boundaries}}
#' @export

contig <- function(spatobj = NULL, folder = NULL, shape = NULL, queen = FALSE) {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    data_ngb <- spdep::poly2nb(spatobj, queen = queen)
    contig <- spdep::nb2mat(data_ngb, style = "B", zero.policy = TRUE)
    return(contig)
}


#' A function to compute the spatial units' perimeters
#'
#' @usage perimeter(spatobj = NULL, folder = NULL, shape = NULL)
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @return A perimeter vector 
#' @description The function is based on on \pkg{rgdal} and \pkg{rgeos}  
#' packages and can be used with a shape file or an R spatial object 
#' (SpatialPolygonsDataFrame).
#' @examples  perimeter(segdata)  
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' perimeter(folder = foldername, shape = shapename)
#' @seealso  Other spatial functions used for segregation indices 
#' computation:  \code{\link{area}}, \code{\link{contig}}, 
#' \code{\link{distance}}, \code{\link{distcenter}}, 
#' \code{\link{boundaries}}
#' @export

perimeter <- function(spatobj = NULL, folder = NULL, shape = NULL) {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    perim <- vector(length = length(spatobj))
    for (i in 1:length(spatobj)) perim[i] <- rgeos::gLength(spatobj[i, ])
    return(perim)
}


#' A function to compute the distance matrix between centroids 
#' of spatial units
#'
#' @usage distance(spatobj = NULL, folder = NULL, shape = NULL,
#' distin = 'm',  distout = 'm', diagval = '0')
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @param distin - input metric conversion, based on  \pkg{measurements} package and 
#' includes conversions from 'm', 'km', 'inch', 'ft', 'yd', 'mi', 'naut_mi', etc.
#' @param distout - output metric conversion, based on  \pkg{measurements} package and 
#' includes conversions to 'm', 'km', 'inch', 'ft', 'yd', 'mi', 'naut_mi', etc.
#' @param diagval -  the user has the choice of the definition of the diagonal: 
#' diagval = '0'  (by default) for an 'empty' diagonal and diagval = 'a'
#' to compute  the diagonal as 0.6 * square root (spatial units area) (White, 1983) 
#' @return A matrix with the distance between centroids
#' @description The function is based on \pkg{rgdal} and \pkg{rgeos} 
#' packages and can be used with a shape file or an R spatial object 
#' (SpatialPolygonsDataFrame).
#' @examples  distance(segdata) 
#' 
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' distance(folder = foldername, shape = shapename)
#' @seealso  Other spatial functions used for segregation indices 
#' computation: \code{\link{area}}, \code{\link{contig}}, 
#' \code{\link{perimeter}}, \code{\link{distcenter}}, 
#' \code{\link{boundaries}}
#' @export

distance <- function(spatobj = NULL, folder = NULL, shape = NULL, distin = "m", distout = "m", diagval = "0") {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    dist <- matrix(0, nrow = length(spatobj), ncol = length(spatobj))
    centroids <- vector("list", length(spatobj))
    for (i in 1:length(spatobj)) centroids[[i]] <- rgeos::gCentroid(spatobj[i, ])
    for (i in 1:(length(spatobj) - 1)) for (j in (i + 1):length(spatobj)) dist[i, j] <- rgeos::gDistance(centroids[[i]], centroids[[j]])
    dist <- dist + t(dist)
    if (diagval == "a") {
        a <- area(spatobj = spatobj, folder = folder, shape = shape)
        diag(dist) <- sqrt(a) * 0.6
    }
    dist <- measurements::conv_unit(dist, from = distin, to = distout)
    return(dist)
}

#' A function to compute the distance from spatial units centroids 
#' to the center
#'
#' @usage distcenter(spatobj = NULL, folder = NULL, shape = NULL, 
#' center = 1, distin = 'm',  distout = 'm')
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @param center - the row number of the center
#' @param distin - input metric conversion, based on  \pkg{measurements} package and 
#' includes conversions from 'm', 'km', 'inch', 'ft', 'yd', 'mi', 'naut_mi', etc.
#' @param distout - output metric conversion, based on  \pkg{measurements} package and 
#' includes conversions to 'm', 'km', 'inch', 'ft', 'yd', 'mi', 'naut_mi', etc.
#' @return A vector with the distance to the center's centroid
#' @description The function is based on \pkg{rgdal} and \pkg{rgeos} 
#' packages and it can be used with a shape file or an R 
#' spatial object (SpatialPolygonsDataFrame).
#' @examples  distcenter(segdata, center = 46) 
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' distcenter(folder = foldername, shape = shapename, center = 19)
#' @seealso  Other spatial functions used for segregation indices 
#' computation: \code{\link{area}}, \code{\link{contig}}, 
#' \code{\link{perimeter}}, \code{\link{distance}}, 
#' \code{\link{boundaries}}
#' @export


distcenter <- function(spatobj = NULL, folder = NULL, shape = NULL, center = 1, distin = "m", distout = "m") {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    distcenter <- vector(length = length(spatobj))
    centroids <- vector("list", length(spatobj))
    for (i in 1:length(spatobj)) centroids[[i]] <- rgeos::gCentroid(spatobj[i, ])
    for (i in 1:length(spatobj)) distcenter[i] <- rgeos::gDistance(centroids[[i]], centroids[[center]])
    distcenter <- measurements::conv_unit(distcenter, from = distin, to = distout)
    return(distcenter)
}

#' A function to compute the matrix of common boundaries
#'
#' @usage boundaries(spatobj = NULL, folder = NULL, shape = NULL)
#' @param folder - a character vector with the folder (directory) 
#' name indicating where the shapefile with the geographic information 
#' is located.
#' @param shape - a character vector with the name of the shapefile 
#' (without the .shp extension) which contains the geographic information
#' @param spatobj - a spatial object (SpatialPolygonsDataFrame) containing 
#' geographic information
#' @return A common boundaries matrix
#' @description The function is based on \pkg{rgdal} and \pkg{rgeos} 
#' packages and it can be used with a shape file 
#' or an R spatial object (SpatialPolygonsDataFrame).
#' @examples  boundaries(segdata) 
#' foldername <- system.file('extdata', package = 'OasisR')
#' shapename <- 'segdata'
#' boundaries(folder = foldername, shape = shapename)
#' @seealso  Other spatial functions used for segregation indices 
#' computation:  \code{\link{area}}, \code{\link{contig}}, 
#' \code{\link{perimeter}}, \code{\link{distance}}, 
#' \code{\link{distcenter}}
#' @export


boundaries <- function(spatobj = NULL, folder = NULL, shape = NULL) {
    if (is.null(spatobj)) 
        spatobj <- rgdal::readOGR(dsn = folder, layer = shape)
    boundaries <- contig(spatobj)
    mat <- upper.tri(boundaries, diag = FALSE) * 1
    boundaries <- boundaries * mat
    for (i in 1:(length(spatobj) - 1)) for (j in (i + 1):length(spatobj)) {
        if (boundaries[i, j] != 0) 
            boundaries[i, j] <- rgeos::gLength(rgeos::gIntersection(spatobj[i, ], spatobj[j, ]))
    }
    boundaries <- boundaries + t(boundaries)
    return(boundaries)
}

Try the OasisR package in your browser

Any scripts or data that you put into this service are public.

OasisR documentation built on July 2, 2020, 3:14 a.m.