Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy geo packages #16

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: geoGraph
Type: Package
Title: Walking through the geographic space using graphs
Version: 1.1.1.9002
Version: 1.1.1.9003
Authors@R: c(
person("Thibaut", "Jombart", role = "aut"),
person("Andrea", "Manica", email = "[email protected]", role = c("aut", "cre")))
Maintainer: Andrea Manica <[email protected]>
Description: Classes and methods for spatial graphs interfaced with support for GIS shapefiles.
License: GPL (>=2)
Language: en-GB
Language: en-US
URL: https://github.com/EvolEcolGroup/geograph,
https://evolecolgroup.github.io/geograph/
BugReports: https://github.com/EvolEcolGroup/geograph/issues
Expand All @@ -23,17 +23,16 @@ Depends:
methods,
graph
Imports:
fields,
RBGL,
rnaturalearth,
rnaturalearthdata,
sp,
sf,
magrittr
fields,
RBGL,
rnaturalearth,
sf,
magrittr
Suggests:
testthat,
knitr,
rmarkdown
rmarkdown,
rnaturalearthdata
RoxygenNote: 7.2.3
Collate:
'classes.R'
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ exportMethods(setDistCosts)
exportMethods(setEdges)
import(graph)
import(methods)
import(sp)
import(sf)
importFrom(graphics,identify)
importFrom(graphics,locator)
importFrom(graphics,segments)
importFrom(magrittr,"%>%")
importFrom(rnaturalearth,ne_countries)
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# geoGraph (development version)

*
* remove all use of deprecated packages (`sp` and `maptools`), and move to
`sf` objects

# geoGraph v1.1

Expand Down
2 changes: 1 addition & 1 deletion R/buffer.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @aliases buffer buffer-methods buffer,gGraph-method buffer,gData-method
#' @param x a valid \linkS4class{gGraph} or \linkS4class{gData} object.
#' @param \dots further arguments passed to specific methods.
#' @param nodes a character vector identifying the nodes aournd which buffers
#' @param nodes a character vector identifying the nodes around which buffers
#' should be computed.
#' @param d the radius of the buffer, in km.
#' @param res.type the type of result that should be returned (see section
Expand Down
4 changes: 2 additions & 2 deletions R/combineCosts.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Combine the costs of two gGraph objects
#'
#' The function \code{combineCosts} combines the edge costs of two
#' \linkS4class{gGraph} objects. The first object is used as a temlate to generate
#' \linkS4class{gGraph} objects. The first object is used as a template to generate
#' the objects with the combined costs. Two two \linkS4class{gGraph} objects must
#' have the same edges.
#'
Expand All @@ -20,7 +20,7 @@
#' the product or a custom function (defined in \code{FUN}) of the costs of its nodes.
#' @param FUN a function used to compute the cost between two nodes (needed if \code{method="function"}).
#' @param \dots additional parameters to be passed to \code{FUN}.
#' @return A \linkS4class{gGraph} object with the newly defined costs, basedd on the combination of the
#' @return A \linkS4class{gGraph} object with the newly defined costs, based on the combination of the
#' two gGraph objects, used as weightings of edges.
#' @export
#' @examples
Expand Down
2 changes: 1 addition & 1 deletion R/connectivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#'
#' - \code{isConnected}: a single logical value, being TRUE if nodes of the
#' object form a connected set.\cr

#' @include classes.R
#' @keywords utilities methods
#' @name connectivity
#' @examples
Expand Down
12 changes: 4 additions & 8 deletions R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @docType data
#' @format \code{hgdp} is a \linkS4class{gGraph} object with the following
#' data: % \describe{ % \item{@nodes.attr$habitat}{habitat corresponding to
#' each % vertice; currently 'land' or 'sea'.} % \item{@meta$color}{a matrix
#' each % vertex; currently 'land' or 'sea'.} % \item{@meta$color}{a matrix
#' assigning a color for plotting % vertices (second column) to different
#' values of habitat (first % column).} % }
#' @references Authors \emph{Journal}, YEAR, \bold{nb}: pp-pp.
Expand Down Expand Up @@ -70,24 +70,20 @@ NULL
#' connectivity between edges at some places. The most noticeable change is that
#' all edges involving sea vertices have been removed.\cr
#'
#' 'worldshape' is a shapefile of countries of the world (snapshot from 1994).
#'
#'
#' @name worldgraph
#' @aliases worldgraph rawgraph.10k rawgraph.40k worldgraph.10k worldgraph.40k
#' worldshape
#' @docType data
#' @format \code{worldgraph.10k} and \code{worldgraph.40k} are
#' \linkS4class{gGraph} objects with the following specificities: \describe{
#' \item{@nodes.attr\$habitat}{habitat corresponding to each vertice; currently
#' 'land' or 'sea'.} \item{@meta\$color}{a matrix assigning a color for
#' \item{@nodes.attr$habitat}{habitat corresponding to each vertex; currently
#' 'land' or 'sea'.} \item{@meta$color}{a matrix assigning a color for
#' plotting vertices (second column) to different values of habitat (first
#' column).} }
#' @references === On the construction of the graph ===\cr Randall, D. A.;
#' Ringler, T. D.; Heikes, R. P.; Jones, P. & Baumgardner, J. Climate Modeling
#' with Spherical Geodesic Grids \emph{Computing in science & engineering},
#' 2002, \bold{4}: 32-41.
#' @source Graph reconstructed by Andrea Manica.
#' @source Graph constructed by Andrea Manica.
#' @keywords datasets
#' @examples
#'
Expand Down
2 changes: 1 addition & 1 deletion R/dijkstra.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ plot.gPath <- function(x, col = "rainbow", lwd = 3, ...) {
N <- length(vecNodes)
if (N < 2) {
return()
} # escape if a path is a single vertice
} # escape if a path is a single vertex
from <- vecNodes[1:(N - 1)]
to <- vecNodes[2:N]
## segments(xy[from,1], xy[from,2], xy[to,1], xy[to,2], col=col, lwd=lwd, ...)
Expand Down
26 changes: 10 additions & 16 deletions R/extractFromLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
#' two columns giving longitudes and latitudes of locations being considered.
#' For list, input must have two components being vectors giving longitudes and
#' latitudes of locations.
#' @param layer a shapefile of the class \code{SpatialPolygonsDataFrame} (see
#' \code{readShapePoly} in maptools package to import such data from a GIS
#' @param layer a shapefile of the class [`sf`] (see
#' [sf::st_read()] to import a GIS
#' shapefile). Alternatively, a character string indicating one shapefile
#' released with geoGraph; currently, only 'world' is available (see
#' \code{?data(worldshape)}).
#' released with geoGraph; currently, only 'world' is available.
#' @param attr a character vector giving names of the variables to be extracted
#' from the layer. If 'all', all available variables are extracted. In case of
#' problem, available names are displayed with the error message. Available
#' data are also stored in \code{layer@data}.
#' problem, available names are displayed with the error message.
#' @param \dots further arguments to be passed to other methds. Currently not
#' used.
#' @return The output depends on the nature of the input:\cr - \code{matrix,
Expand All @@ -51,13 +49,13 @@
#'
#' ## retrieve continent info for all nodes
#' ## (might take a few seconds)
#' x <- extractFromLayer(worldgraph.10k, layer = "world", attr = "CONTINENT")
#' x <- extractFromLayer(worldgraph.10k, layer = "world", attr = "continent")
#' x
#' table(getNodesAttr(x, attr.name = "CONTINENT"))
#' table(getNodesAttr(x, attr.name = "continent"))
#'
#'
#' ## subset Africa
#' temp <- getNodesAttr(x, attr.name = "CONTINENT") == "Africa"
#' temp <- getNodesAttr(x, attr.name = "continent") == "Africa"
#' temp[is.na(temp)] <- FALSE
#' x <- x[temp]
#' plot(x, reset = TRUE)
Expand All @@ -84,17 +82,13 @@ setGeneric("extractFromLayer", function(x, ...) {
#' @rdname extractFromLayer
#' @export
setMethod("extractFromLayer", "matrix", function(x, layer = "world", attr = "all", ...) {


## This functions automatically assigns to land all points overlapping the country polygons
# if(!require(maptools)) stop("maptools package is required.")

## Load default shapefile ##
if (is.character(layer) && layer[1] == "world") {
# use rnaturalearth instead of the inbuilt dataset
# layer <- rnaturalearth::ne_countries(scale="medium", returnclass = "sf")
# sf::sf_use_s2(FALSE)
layer <- sf::st_read(system.file("files/shapefiles/world-countries.shp", package = "geoGraph"))
layer <- rnaturalearth::ne_countries(scale="medium", returnclass = "sf")
sf::sf_use_s2(FALSE)
#layer <- sf::st_read(system.file("files/shapefiles/world-countries.shp", package = "geoGraph"))
}

## TODO if the layer is null, we should throw an error!!!
Expand Down
64 changes: 31 additions & 33 deletions R/findLand.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
#' @param x a matrix, a data.frame, or a valid \linkS4class{gGraph} object. For
#' matrix and data.frame, input must have two columns giving longitudes and
#' latitudes of locations being considered.
#' @param shape a shapefile of the class \code{SpatialPolygonsDataFrame} (see
#' \code{readShapePoly} in maptools package to import such data from a GIS
#' @param shape a shapefile of the class [`sf`] (see
#' [sf::st_read()] to import a GIS
#' shapefile). Alternatively, a character string indicating one shapefile
#' released with geoGraph; currently, only 'world' is available (see
#' \code{?data(worldshape)}).
#' released with geoGraph; currently, only 'world' is available.
#' @param \dots further arguments to be passed to other methods. Currently not
#' used.
#' @param attr.name a character string giving the name of the node attribute in
Expand Down Expand Up @@ -49,7 +48,7 @@
#' ## define rules for colors
#' temp <- data.frame(habitat = c("land", "sea"), color = c("green", "blue"))
#' temp
#' obj@meta$color <- temp
#' obj@meta$colors <- temp
#'
#' ## plot object with new colors
#' plot(obj)
Expand Down Expand Up @@ -77,43 +76,42 @@ setGeneric("findLand", function(x, ...) {
#' @rdname findLand
#' @export
setMethod("findLand", "matrix", function(x, shape = "world", ...) {
## This functions automatically assigns to land all points overlapping the country polygons
# if(!require(maptools)) stop("maptools package is required.")

## Load country shapefile
## Load default shapefile ##
if (is.character(shape) && shape[1] == "world") {
shape <- worldshape
# use rnaturalearth
shape <- rnaturalearth::ne_countries(scale="medium", returnclass = "sf")
sf::sf_use_s2(FALSE)
}

if (!is.null(shape)) { # with background
if (!inherits(shape, "SpatialPolygonsDataFrame")) {
stop("Layer must be a SpatialPolygonsDataFrame object \n(see st_read and as_Spatial in sf to import such data from a GIS shapefile).")

## TODO if the shape is null, we should throw an error!!!
if (!is.null(shape)) {
if (!inherits(shape, "sf")) {
if (inherits(shape, "SpatialPolygonsDataFrame")){
shape <- sf::st_as_sf(shape)
} else {
stop("shape must be a sf object \n(see st_read in sf to import such data from a GIS shapefile).")
}
}
}



if (any(is.na(x))) {
stop("Matrix contains NA values.")
}

long <- x[, 1]
lat <- x[, 2]
n.country <- length(shape@polygons)

## create land vector to score land
land <- rep(0, length(lat))

for (i in 1:n.country) {
this.country <- shape@polygons[i][[1]]
n.polys <- length(this.country@Polygons)

for (p in 1:n.polys) {
this.poly <- this.country@Polygons[p][[1]]
land <- land + point.in.polygon(long, lat, this.poly@coords[, 1], this.poly@coords[, 2])
}
}
land[land > 1] <- 1
land[land == 0] <- "sea"
land[land == 1] <- "land"

# create an sf point object from the coordinates
locations_st <- x %>% as.data.frame %>%
sf::st_as_sf(coords=c(1,2)) %>%
sf::st_set_crs(sf::st_crs(shape))
# now find points in polygons
points_within <- sf::st_intersects(shape, locations_st)
points_within <- data.frame(x = unlist(points_within),
polygon = rep(seq_along(lengths(points_within)), lengths(points_within)))

land<-rep("sea",nrow(x))
land[points_within$x]<-"land"

return(factor(land))
})
Expand Down
7 changes: 2 additions & 5 deletions R/geograph.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#'
#' - \code{\link{findLand}}: checks which nodes are on land.\cr
#'
#' - \code{\link{setCosts}}: define edges weights accoring to rules specified
#' - \code{\link{setCosts}}: define edges weights according to rules specified
#' in the @meta slot.\cr
#'
#' - \code{\link{geo.add.edges}}, \code{\link{geo.remove.edges}}: graphical
Expand Down Expand Up @@ -83,14 +83,11 @@
#' since they provide the spatial models used in later operations.
#'
#' Two main datasets are proposed, each being a \linkS4class{gGraph} resulting
#' from the spliting of the earth into cells of (allmost perfectly) equal
#' from the spliting of the earth into cells of (almost perfectly) equal
#' sizes. Two different resolutions are provided:\cr -
#' \code{\link{worldgraph.10k}}: coverage using about 10,000 nodes\cr -
#' \code{\link{worldgraph.40k}}: coverage using about 40,000 nodes\cr
#'
#' Other datasets are:\cr - \code{\link{worldshape}}: shapefile containing
#' world countries.\cr
#'
#' To cite geoGraph, please use the reference given by
#' \code{citation("geoGraph")}.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
@@ -1 +1 @@
utils::globalVariables(c("rawgraph.10k", "rawgraph.40k", "worldshape"))
utils::globalVariables(c("rawgraph.10k", "rawgraph.40k"))
2 changes: 1 addition & 1 deletion R/isInArea.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' so \code{locator(1)} is a valid value for \code{reg}.
#' @param res.type a character string indicating what kind of output should be
#' produced. See value.
#' @param buffer a numeric value giving a buffer adding extra space aroung the
#' @param buffer a numeric value giving a buffer adding extra space around the
#' area, as a proportion of current area's dimensions.
#' @return The output depends on the value of the argument \code{res.type}:\cr
#' - \code{logical}: a vector of logicals having one value for each node of the
Expand Down
32 changes: 20 additions & 12 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#' points,gGraph-method points.gGraph plotEdges
#' @docType methods
#' @param x a \linkS4class{gGraph} object.
#' @param shape a shapefile used as background to the object. Must be of the
#' class \code{SpatialPolygonsDataFrame} (see \code{readShapePoly} in maptools
#' package to import such data from a GIS shapefile). Alternatively, a
#' character string indicating one shapefile released with geoGraph.
#' @param shape a shapefile of the class [`sf`] (see
#' [sf::st_read()] to import a GIS
#' shapefile). Alternatively, a character string indicating one shapefile
#' released with geoGraph; currently, only 'world' is available.
#' @param psize a numeric giving the size of points.
#' @param pch a numeric or a character indicating the type of point.
#' @param col a character string indicating the color to be used.
Expand Down Expand Up @@ -64,6 +64,7 @@
#'
#' - \code{\link{isInArea}}, to retain a set of visible data.\cr
#' @keywords methods hplot spatial
#' @importFrom rnaturalearth ne_countries
#' @examples
#'
#'
Expand Down Expand Up @@ -103,7 +104,7 @@ NULL
## plot for gGraph
###################
#' @export
#' @import sp
#' @import sf
setMethod("plot", signature(x = "gGraph", y = "missing"), function(x, y, shape = "world", psize = NULL, pch = 19, col = NULL,
edges = FALSE, reset = FALSE, bg.col = "gray", border.col = "dark gray",
lwd = 1, useCosts = NULL, maxLwd = 3, col.rules = NULL, ...) {
Expand Down Expand Up @@ -191,16 +192,23 @@ setMethod("plot", signature(x = "gGraph", y = "missing"), function(x, y, shape =

## handle shape
if (!is.null(shape) && is.character(shape) && shape == "world") {
shape <- worldshape
}

if (!is.null(shape)) { ## plot with background ##
if (!inherits(shape, "SpatialPolygonsDataFrame")) {
stop("Layer must be a SpatialPolygonsDataFrame object \n(see st_read and as_Spatial in sf to import such data from a GIS shapefile).")
#shape <- sf::st_read(system.file("files/shapefiles/world-countries.shp", package = "geoGraph"))
shape <- rnaturalearth::ne_countries(scale="medium", returnclass = "sf")
sf::sf_use_s2(FALSE)
}

## TODO if the shape is null, we should throw an error!!!
if (!is.null(shape)) {
if (!inherits(shape, "sf")) {
if (inherits(shape, "SpatialPolygonsDataFrame")){
shape <- sf::st_as_sf(shape)
} else {
stop("shape must be a sf object \n(see st_read in sf to import such data from a GIS shapefile).")
}
}

## plot background
plot(shape, col = bg.col, border = border.col, xlim = xlim, ylim = ylim)
plot(sf::st_geometry(shape), col = bg.col, border = border.col, xlim = xlim, ylim = ylim)

## subset of points in area
toKeep <- isInArea(x, reg = "current", res.type = "character")
Expand Down
Loading
Loading