Skip to content
Snippets Groups Projects
README.Rmd 3.05 KiB
Newer Older
---
output: github_document
---

[![CRAN status](https://www.r-pkg.org/badges/version/geonetwork)](https://cran.r-project.org/package=geonetwork)
[![r-universe](https://cirad-astre.r-universe.dev/badges/geonetwork)](https://cirad-astre.r-universe.dev/)
<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(geonetwork)
```
# geonetwork

Classes and methods for handling networks or graphs whose nodes are
geographical (i.e. locations in the globe). Create, transform, plot.


## Installation

You can install the released version of `geonetwork` from
[CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("geonetwork")
```
Alternatively, install the latest development version with:
install.packages("geonetwork", repos = 'https://cirad-astre.r-universe.dev')
```


## Example

### Creation

A `geonetwork` is an object of class `igraph` whose nodes have
_geospatial_ attributes (i.e. coordinates and CRS).

Consider the distances (in km) between 21 cities in Europe from the
`datasets` package. A simple way of constructing a `geonetwork` is
by combining a data.frame of `nodes` with one of `edges`:

```{r creation, message = FALSE}
## Use OpenStreetMap's Nominatim service through the package {tmaptools}
## to retrieve coordinates of the cities.
## Restrict search to Europe (to prevent homonym cities to show up)
cities <- tmaptools::geocode_OSM(
  paste(
    labels(datasets::eurodist),
    "viewbox=-31.64063%2C60.93043%2C93.16406%2C31.65338",
    sep = "&"
  )
cities$city <- labels(datasets::eurodist)
Facundo Muñoz's avatar
Facundo Muñoz committed

distances <- 
  expand.grid(
    origin = labels(datasets::eurodist),
    destin = labels(datasets::eurodist),
    stringsAsFactors = FALSE,
    KEEP.OUT.ATTRS = FALSE
  )
distances <- 
  cbind(
    distances[distances$destin < distances$origin,],
    distance = as.numeric(datasets::eurodist)
  )

str(cities)
str(distances)

eurodist <- geonetwork(
  distances,
  nodes = cities[, c("city", "lon", "lat")], 
  directed = FALSE
)
```

Several assumptions were made here unless otherwise specified:

- The first column in `cities` was matched with the first two columns
in `distances`.

- The second and third columns in `cities` were assumed to be 
longitude and latitude in decimal degrees in a WGS84 CRS.

- The remaining column in `distances` was treated as an edge _weight_.

Now we can readily plot the network, optionally with some additional 
geographical layer for context:


```{r plotting}
## Base system
plot(eurodist, axes = TRUE, type = "n")
plot(sf::st_geometry(spData::world), col = "lightgray", add = TRUE)
plot(eurodist, axes = TRUE, add = TRUE)
```

```{r include = FALSE, eval = FALSE}

# bgm <- ggmap::get_stamenmap(
#   bbox = unname(sf::st_bbox(eurodist)),
#   zoom = 5, maptype = "watercolor")
# plot(st_transform(eurodist, 3857), bgMap = bgm)

# library(ggplot2)
# ggplot() +
#   geom_sf(eurod_net_dummy) +
#   geom_sf(spData::world)