Shp2graph: Tools to Convert a Spatial Network into an Igraph Graph in R
<p>Network to graph conversion by taking all the endpoints or junctions as vertices (Ontario road data). (<b>a</b>) The ORN spatial network, (<b>b</b>) The resultant ‘<b>igraph</b>’ graph.</p> "> Figure 2
<p>The converted ‘<b>igraph</b>’ graph with all geometric properties retained (Ontario road data).</p> "> Figure 3
<p>Case study data sets: London house price and road network data.</p> "> Figure 4
<p>Connectivity of the London road network data via the function <span class="html-italic">nt.connect.</span></p> "> Figure 5
<p>Connectivity of the Estevan road network via the function <span class="html-italic">nt.connect</span>.</p> "> Figure 6
<p>Three common types of topology errors. (<b>a</b>) Dangling arc, (<b>b</b>) Undershoot, (<b>c</b>) Overshoot.</p> "> Figure 7
<p>The main self-connected part of the Estevan road network returned by the function <span class="html-italic">nt.connect.</span></p> "> Figure 8
<p>Connectivity of the Estevan road network with topology errors fixed via the function <span class="html-italic">nt.connect.</span></p> "> Figure 9
<p>Four strategies to integrate points/locations within the spatial network. (<b>a</b>) Strategy 1: represent each point with its nearest vertex; (<b>b</b>) Strategy 2: represent each point with its nearest geometric point; (<b>c</b>) Strategy 3: add a pseudo edge (colored green) between each point and its nearest vertex; (<b>d</b>) Strategy 4: Add a pseudo edge (colored green) between each point and its nearest geometric point.</p> "> Figure 10
<p>Examples of integrating points/locations with a spatial network (Ontario road data), where green dots represent points given, a blue dash in (<b>a</b>,<b>b</b>) indicate the closest relationship between a point and its nearest vertex/position on the network, green lines in (<b>c</b>,<b>d</b>) mean the edges newly added and red circles mark the representative vertices. (<b>a</b>) Solution with strategy 1; (<b>b</b>) Solution with strategy 2; (<b>c</b>) Solution with strategy 3; (<b>d</b>) Solution with strategy 4.</p> "> Figure 11
<p>Integration of the London property locations onto the London road network using strategies 1 and 2. (<b>a</b>) Solution with strategy 1; (<b>b</b>) Solution with strategy 2.</p> "> Figure 12
<p>Network distances calculated with two different integrating strategies (London case study).</p> "> Figure 13
<p>Flow chart of calculating network distances with <b>shp2graph</b> and <b>igraph.</b></p> "> Figure 14
<p>An interactive example for finding the shortest path with <b>shp2graph</b> and <b>igraph</b> (Ontario road data).</p> "> Figure 15
<p>Finding the network diameter with <b>shp2graph</b> and <b>igraph</b> (Ontario road data).</p> ">
Abstract
:1. Introduction
2. Converting a Spatial Network into an ‘igraph’ Graph
2.1. The Graph Data Model
- (i)
- vertex-vertex relations, where the adjacency relationship is defined between two vertices if they are connected by an undirected or directed edge;
- (ii)
- vertex-edge relations, where the member-owner relationship is defined if it is a starting or ending vertex of an edge;
- (iii)
- edge-edge relations, where the contiguity relationship between two edges is defined if they share one or more vertex.
- Step 1.
- Extract objects from the spatial network as vertices and include an identifier (ID) and 2-D coordinate (x, y) for each vertex;
- Step 2.
- Construct all the edges according to the vertex-vertex adjacency relationships and deliver them as an adjacency matrix or 2-D array;
- Step 3.
- Use existing functions in igraph to create an ‘igraph’ graph.
2.2. Data Conversion
Code snippet 1: data(ORN) plot(ORN.nt) title(“The ORN spatial network”) rtNEL1 <-readshpnw(ORN.nt) igr1 <-nel2igraph(rtNEL1[[2]], rtNEL1[[3]]) plot(igr1, vertex.label = NA, vertex.size = 2, vertex.size2 = 2, mark.col = “green”, main = “The converted igraph graph”) summary(igr1) IGRAPH U--- 298 343 -- + attr: x (v/n), y (v/n)
Code snippet 2: rtNEL2 <-readshpnw(ORN.nt, Detailed = TRUE, ea.prop = rep(0, 37)) igr2 <-nel2igraph(rtNEL2[[2]], rtNEL2[[3]]) plot(igr2, vertex.label = NA, vertex.size = 1, vertex.size2 = 1, main = “The converted igraph graph with all the geometric properties”) summary(igr2) IGRAPH U--- 597 642 -- + attr: x (v/n), y (v/n)
3. Calculating Network Distances with Shp2graph and Igraph
Code snippet 3: require(RColorBrewer) data(LNNT) data(LNHP) road.type <- unique(LN.nt$nt_RoadTyp) idx <- match(LN.nt$nt_RoadTyp, road.type) ltypes <- c(1, 1, 3, 1) lwidths <- c(1, 1.5, 0.2, 2) shades <- brewer.pal(4, “Dark2”) plot(LN.nt, col = shades[idx], lty = ltypes[idx], lwd = lwidths[idx], cex = 0.3) points(coordinates(LN.prop), pch = 15) legend(551000, 172000, legend = road.type, lty = ltypes, lwd = lwidths, col = shades, title = “Road type”)
3.1. Confirming Connectivity in a Spatial Network
Code snippet 4: LN.nt.con <- nt.connect(LN.nt) plot(LN.nt.con)
Code snippet 5: data(ERN_OSM) ERN.con <- nt.connect(ERN_OSM.nt) plot(ERN.con)
- ▗
- Dangling arc (Figure 6a): an arc featured by having the same polygon on both its sides and at least one vertex that does not connect to any other arc, which is especially harmful for the connectivity of the converted graph when both nodes are not connected to any other arc;
- ▗
- Overshoot (Figure 6b): an arc passes another arc which should intersect and stop there;
- ▗
- Undershoot (Figure 6c): an arc does not extend long enough to another arc which it should intersect.
Code snippet 6: data(ERN_OSM_correct) ERN_cor.con <- nt.connect(ERN_OSM_cor.nt)
3.2. Integrating Points with a Spatial Network
Code snippet 7: data(ORN) pts <- spsample(ORN.nt, 100, type = “random”) ptsxy <- coordinates(pts)[, 1:2] ptsxy <- cbind(ptsxy[, 1] + 0.008, ptsxy[, 2] + 0.008) res.nv <- points2network(ntdata = ORN.nt, pointsxy = ptsxy, ELComputed = TRUE, approach = 1) ptsinnt.view(ntdata = ORN.nt, nodelist = res.nv[[1]], pointsxy = ptsxy, CoorespondIDs = res.nv[[3]]) res.np <- points2network(ntdata = ORN.nt, pointsxy = ptsxy, approach = 2, ea.prop = rep(0, 2)) ptsinnt.view(ntdata = ORN.nt, nodelist = res.np[[1]], pointsxy = ptsxy, ELComputed = TRUE, CoorespondIDs = res.np[[3]]) res.vnv <- points2network(ntdata = ORN.nt, pointsxy = ptsxy, ELComputed = TRUE, approach = 3, ea.prop = rep(0, 2)) ptsinnt.view(ntdata = ORN.nt, nodelist = res.vnv[[1]], pointsxy = ptsxy, CoorespondIDs = res.vnv[[3]], VElist = res.vnn[[7]]) res.vnp <- points2network(ntdata = ORN.nt, pointsxy = ptsxy, ELComputed = TRUE, approach = 4, ea.prop = rep(0, 2)) ptsinnt.view(ntdata = ORN.nt, nodelist = res.vnp[[1]], pointsxy = ptsxy, CoorespondIDs = res.vnp[[3]], VElist = res.vnp[[7]])
3.3. Converting a Spatial Network into an Igraph Graph
Code snippet 8: # Strategy 1 Edge.len1 <- LN.pt2nt1[[8]] LN.ig1 <-nel2igraph(LN.pt2nt1[[1]], LN.pt2nt1[[2]], weight = Edge.len1) summary(LN.ig1) IGRAPH U-W- 75236 97800 -- + attr: x (v/n), y (v/n), weight (e/n) # Strategy 2 Edge.len2 <- LN.pt2nt2[[8]] LN.ig2 <-nel2igraph(LN.pt2nt2[[1]], LN.pt2nt2[[2]], weight = Edge.len2) summary(LN.ig2) IGRAPH U-W- 76762 99326 -- + attr: x (v/n), y (v/n), weight (e/n)
Code snippet 9: # Strategy 1 names(LN.pt2nt1[[6]]) Speed1 <- LN.pt2nt1[[6]][, 4] * 0.44704 LN.ig3 <- nel2igraph(LN.pt2nt1[[1]], LN.pt2nt1[[2]], weight = Edge.len1/Speed1) summary(LN.ig3) IGRAPH U-W- 75236 97800 -- + attr: x (v/n), y (v/n), weight (e/n) # Strategy 2 Speed2 <- LN.pt2nt2[[6]][, 4] * 0.44704 LN.ig4 <- nel2igraph(LN.pt2nt2[[1]], LN.pt2nt2[[2]], weight = Edge.len2/Speed2) summary(LN.ig4) IGRAPH U-W- 76762 99326 -- + attr: x (v/n), y (v/n), weight (e/n)
3.4. Calculating Network Distances and Travel Times
Code snippet 10: # Strategy 1 LN.pt.VIDs1 <- as.numeric(LN.pt2nt1[[3]]) V.idx1 <- as.numeric(levels(as.factor(LN.pt.VIDs1))) ND.mat1 <- shortest.paths(LN.ig1, v = LN.pt.VIDs1, to = V.idx1) ND.mat1 <- ND.mat1[, match(LN.pt.VIDs1, V.idx1)] # Strategy 2 LN.pt.VIDs2 <- as.numeric(LN.pt2nt2[[3]]) V.idx2 <- as.numeric(levels(as.factor(LN.pt.VIDs2))) ND.mat2 <- shortest.paths(LN.ig2, v = LN.pt.VIDs2, to = V.idx2) ND.mat2 <- ND.mat2[, match(LN.pt.VIDs2, V.idx2)] # Scatterplot plot(ND.mat1, ND.mat2, pch = 16, cex = 0.2, col = “grey”, xlab = “Network distances calculated with integrating strategy 1”, ylab = “Network distances calculated with integrating strategy 2”) abline(a = 0, b = 1, col = “blue”, lwd = 2)
Code snippet 11: # Strategy 1 TT.mat1 <- shortest.paths(LN.ig3, v = LN.pt.VIDs1, to = V.idx1) TT.mat1 <- ND.mat1[, match(LN.pt.VIDs1, V.idx1)] # Strategy 2 TT.mat2 <- shortest.paths(LN.ig2, v = LN.pt.VIDs2, to = V.idx2) TT.mat2 <- TT.mat2[, match(LN.pt.VIDs2, V.idx2)]
3.5. Example Extensions
Code snippet 12: plot(ORN.nt) coords <- locator(2) coords <- cbind(coords$x, coords$y) points(coords, pch = 3, col = “red”) pt2nt1 <- points2network(ntdata = ORN.nt, pointsxy = coords, Detailed = T, ELComputed = T, approach = 2, ea.prop = rep(0, 2)) V.idx3 <- as.numeric(pt2nt1[[3]]) V.coords <- cbind(pt2nt1[[4]][V.idx3], pt2nt1[[5]][V.idx3]) points(V.coords, pch = 16, col = “green”) lines(rbind(coords[1,], V.coords[1,]), lty = 2, col = “blue”) lines(rbind(coords[2,], V.coords[2,]), lty = 2, col = “blue”) igr3 <- nel2igraph(pt2nt1[[1]], pt2nt1[[2]], weight = pt2nt1[[8]]) path <- get.shortest.paths(igr3, from = V.idx3[1], to = V.idx3[2])$vpath[[1]] for(i in 1:(length(path)-1)) lines(cbind(pt2nt1[[4]][path[i,i+1]], pt2nt1[[5]][path[i,i+1]]), lwd =2, col = “blue”)
Code snippet 13: V.bn <- betweenness(igr2) E.bn <- edge_betweenness(igr2) G.dm <- diameter(igr2, directed = F) V.gdm <- get_diameter(igr2, directed = F) plot(ORN.nt) for(i in 1:(length(V.gdm)-1)) lines(cbind(rtNEL2[[6]][V.gdm [i,i+1]], rtNEL2[[7]][V.gdm[i,i+1]]), lwd = 2, col = “red”)
4. Summary
Author Contributions
Funding
Conflicts of Interest
References
- Barthélemy, M. Spatial networks. Phys. Rep. 2011, 499, 1–101. [Google Scholar] [CrossRef] [Green Version]
- Lu, B.; Charlton, M. Convert a Spatial Network to a Graph in R. In Proceedings of the R User Conference, Coventry, UK, 16–18 August 2011. [Google Scholar]
- George, B.; Shekhar, S. Road maps, digital. In Encyclopedia of GIS; Shashi, S., Hui, X., Eds.; Springer: New York, NY, USA, 2008. [Google Scholar]
- Goodchild, M.F. Geographical data modeling. Comput. Geosci. 1992, 18, 401–408. [Google Scholar] [CrossRef]
- Gastner, M.T.; Newman, M.E.J. The spatial structure of networks. Eur. Phys. J. B Condens. Matter Complex Syst. 2006, 49, 247–252. [Google Scholar] [CrossRef] [Green Version]
- Fowler, P.A. The königsberg bridges—250 years later. Am. Math. Mon. 1988, 95, 42–43. [Google Scholar]
- Appert, M.; Chapelon, L. The space-time variability of road base accessibility: Application to london. In Graphs and Networks; Mathis, P., Ed.; ISTE: London, UK, 2007; pp. 3–28. [Google Scholar]
- R Core Team. R: A Language and Environment for Statistical Computing; R Foundation for Statistical Computing: Vienna, Austria, 2017. [Google Scholar]
- Lu, B.; Charlton, M.; Harris, P.; Fotheringham, A.S. Geographically weighted regression with a non-euclidean distance metric: A case study using hedonic house price data. Int. J. Geogr. Inf. Sci. 2014, 28, 660–681. [Google Scholar] [CrossRef]
- Van Etten, J. R package gdistance: Distances and routes on geographical grids. J. Stat. Softw. 2017, 1. [Google Scholar] [CrossRef]
- Pebesma, E.J.; Bivand, R.S. Classes and methods for spatial data in R. R News 2005, 5, 9–13. [Google Scholar]
- ESRI Corp. Arcgis Desktop: Release 10; Environmental Systems Research Institute: Redlands, CA, USA, 2011. [Google Scholar]
- GRASS Development Team. Geographic Resources Analysis Support System (Grass GIS) Software; Open Source Geospatial Foundation: Chicago, IL, USA, 2018. [Google Scholar]
- pgRouting Community. Pgrouting 2.3. Available online: https://pgrouting.org/index.html (accessed on 5 May 2018).
- Boeing, G. Osmnx: New methods for acquiring, constructing, analyzing, and visualizing complex street networks. Comput. Environ. Urban Syst. 2017, 65, 126–139. [Google Scholar] [CrossRef]
- Boeing, G. A multi-scale analysis of 27,000 urban street networks. Environ. Plan. B Urban Anal. City Sci. 2018, in press. [Google Scholar]
- Hagberg, A.; Swart, P.; S Chult, D. Exploring Network Structure, Dynamics, and Function Using NetworkX. In Proceedings of the 7th Python in Science Conference, Pasadena, CA, USA, 19–24 August 2008. [Google Scholar]
- Karduni, A.; Kermanshah, A.; Derrible, S. A protocol to convert spatial polyline data to network formats and applications to world urban road networks. Sci. Data 2016, 3, 160046. [Google Scholar] [CrossRef] [PubMed]
- Furieri, A. Spatialite-Tools, 2017-03-03 ed.; SpatiaLite: Genova, Italy, 2018. [Google Scholar]
- Jiang, B. Axwoman 6.3: An Arcgis Extension for Urban Morphological Analysis; University of Gävle: Gävle, Sweden, 2015. [Google Scholar]
- Gollini, I.; Lu, B.; Charlton, M.; Brunsdon, C.; Harris, P. Gwmodel: An R package for exploring spatial heterogeneity using geographically weighted models. J. Stat. Softw. 2015, 63, 1–50. [Google Scholar] [CrossRef]
- Lu, B.; Harris, P.; Charlton, M.; Brunsdon, C. The gwmodel r package: Further topics for exploring spatial heterogeneity using geographically weighted models. Geo-Spat. Inf. Sci. 2014, 17, 85–101. [Google Scholar] [CrossRef]
- Csardi, G.; Nepusz, T. The igraph software package for complex network research. InterJournal Complex Syst. 2006, 1695, 1–9. [Google Scholar]
- Dijkstra, E.W. A note on two problems in connexion with graphs. Numer. Math. 1959, 1, 269–271. [Google Scholar] [CrossRef] [Green Version]
- Bellman, R. On a routing problem. Q. Appl. Math. 1958, 16, 87–90. [Google Scholar] [CrossRef] [Green Version]
- Ford, L.R. Network Flow Theory; RAND Corporation: Santa Monica, CA, USA, 1956. [Google Scholar]
- Johnson, D.B. Efficient algorithms for shortest paths in sparse networks. J. ACM 1977, 24, 1–13. [Google Scholar] [CrossRef]
- Mathis, P. (Ed.) Strengths and deficiencies of graphs for network description and modeling. In Graphs and Networks; ISTE: London, UK, 2007; p. xvi. [Google Scholar]
- Peterborough, O. Ontario Road Network; Ontario Ministry of Natural Resources: Peterborough, ON, Canada, 2006. [Google Scholar]
- Mainguenaud, M. Modelling the network component of geographical information systems. Int. J. Geogr. Inf. Syst. 1995, 9, 575–593. [Google Scholar] [CrossRef]
- Jiang, B.; Claramunt, C. A structural approach to the model generalization of an urban street network. Geoinformatica 2004, 8, 157–171. [Google Scholar] [CrossRef]
- Viana, M.P.; Strano, E.; Bordin, P.; Barthelemy, M. The simplicity of planar networks. Sci. Rep. 2013, 3, 3495. [Google Scholar] [CrossRef] [PubMed]
- Boeing, G. Planarity and street network representation in urban form analysis. arXiv, 2018; arXiv:1806.01805. [Google Scholar] [CrossRef]
- Abdulganiev, I.; Agafonov, A. Automatic Checking of Road Network Models. In Proceedings of the CEUR Workshop Proceedings, Moscow, Russia, 25–26 November 2016; pp. 249–255. [Google Scholar]
- OpenStreetMap. Estevan Road Network; OpenStreetMap Community: London, UK, 2014. [Google Scholar]
- Wade, T.; Sommer, S. A to Z GIS: An Illustrated Dictionary of Geographic Information Systems; ESRI Press: Redlands, CA, USA, 2006. [Google Scholar]
- Tchoukanski, I. Et Geotools; 11.5; ET SpatialTechniques: Faerie Glen, South Africa, 2018. [Google Scholar]
- Knight, P.L.; Marshall, W.E. The metrics of street network connectivity: Their inconsistencies. J. Urban. Int. Res. Placemarking Urban Sustain. 2015, 8, 241–259. [Google Scholar] [CrossRef]
© 2018 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).
Share and Cite
Lu, B.; Sun, H.; Harris, P.; Xu, M.; Charlton, M. Shp2graph: Tools to Convert a Spatial Network into an Igraph Graph in R. ISPRS Int. J. Geo-Inf. 2018, 7, 293. https://doi.org/10.3390/ijgi7080293
Lu B, Sun H, Harris P, Xu M, Charlton M. Shp2graph: Tools to Convert a Spatial Network into an Igraph Graph in R. ISPRS International Journal of Geo-Information. 2018; 7(8):293. https://doi.org/10.3390/ijgi7080293
Chicago/Turabian StyleLu, Binbin, Huabo Sun, Paul Harris, Miaozhong Xu, and Martin Charlton. 2018. "Shp2graph: Tools to Convert a Spatial Network into an Igraph Graph in R" ISPRS International Journal of Geo-Information 7, no. 8: 293. https://doi.org/10.3390/ijgi7080293