<commit_before>---
title: "Spatial networks - First attempt"
author: "<NAME>"
date: "March 12, 2017"
output: html_document
tags:
 - R
 - Spatial
 - Network Analysis
---



This document describes a preliminary workflow for familiarizing with using `igraph` to represent a spatial network. `igraph` is a package for network analysis, with the main data structure (`igraph`) used to represent a network. The term *spatial network* hereby refers to any network where both the **vertices** and the **edges** are associated with spatial features. For example, in a transportation network the **vertices** may correspond to junctions, bus stops, etc., while **edges** may correspond to the road segments connecting them. 

In the following simple network, vertices correspond to US state centroids, while edges are straight line connections between the centroids. Not all possible vertex combinations are connected, however. We are going to connect only those centroids that correspond to states sharing a common boundary.

As a first step, let us obtain a `SpatialPolygonsDataFrame` object of US states -


```r
library(rgdal)
library(rgeos)
library(igraph)

# Read states layer from a GeoJSON file
url = "https://raw.githubusercontent.com/alignedleft/d3-book/master/chapter_12/us-states.json"
state = readOGR(
  dsn = url, 
  layer = "OGRGeoJSON", 
  stringsAsFactors = FALSE, 
  verbose = FALSE
  )

# Transform to US-Atlas projection
state = spTransform(
  state, 
  "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
  )<commit_msg>add tags 3<commit_after>---
title: "Spatial networks - First attempt"
author: "<NAME>"
date: "March 12, 2017"
output: html_document
tags:
- R
- Spatial
- Network Analysis
---



This document describes a preliminary workflow for familiarizing with using `igraph` to represent a spatial network. `igraph` is a package for network analysis, with the main data structure (`igraph`) used to represent a network. The term *spatial network* hereby refers to any network where both the **vertices** and the **edges** are associated with spatial features. For example, in a transportation network the **vertices** may correspond to junctions, bus stops, etc., while **edges** may correspond to the road segments connecting them. 

In the following simple network, vertices correspond to US state centroids, while edges are straight line connections between the centroids. Not all possible vertex combinations are connected, however. We are going to connect only those centroids that correspond to states sharing a common boundary.

As a first step, let us obtain a `SpatialPolygonsDataFrame` object of US states -


```r
library(rgdal)
library(rgeos)
library(igraph)

# Read states layer from a GeoJSON file
url = "https://raw.githubusercontent.com/alignedleft/d3-book/master/chapter_12/us-states.json"
state = readOGR(
  dsn = url, 
  layer = "OGRGeoJSON", 
  stringsAsFactors = FALSE, 
  verbose = FALSE
  )

# Transform to US-Atlas projection
state = spTransform(
  state, 
  "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
  )
{'fraction_non_alphanumeric': 0.08748403575989783, 'fraction_numerical': 0.022030651340996167, 'mean_word_length': 4.655234657039712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022305', 'n_tokens_mistral': 962, 'n_tokens_neox': 877, 'n_words': 392}