Error building network from vertices in spatstat

64 views Asked by At

I have a very large (shapefile) road network to read as a linear network in spatstat. So I am trying to build a basic network from reading vertices and edges as discussed in chapter 17 of book - spatial point patterns by Baddeley et al

I attach my data here

Using this code below I get an error Error: length(x0) == length(x1) is not TRUE. It is not clear to me what is x0 and x1 in order to be able to find the problem.

library(maptools)
library(spatstat)
setwd("~/documents/rwork/traced/a")

pt <- readShapePoints("collected.shp") #read vertices from a shapefile.

edgeRecords<-read.delim("edgelist.txt") #read edge connectivity list

ed<-data.frame(from=edgeRecords$from,to=edgeRecords$to)

xx<-pt@bbox[1,]#read x bounds of owin
yy<-pt@bbox[2,]#read y bounds of owin

v<-ppp(x=pt@coords[,1], y=pt@coords[,2], xx,yy) #read list of vertices


edg<-as.matrix(ed) # read node pairs as matrix

built_network<-linnet(v,edges = edg)

This results in error

Error: length(x0) == length(x1) is not TRUE
1

There are 1 answers

0
BKS On BEST ANSWER

As in one of the comments above. I noticed that GIS indexing starts from 0 while R indexing starts from 1.

So to solve this problem, I just added +1 to the edge matrix. Because if you have collected your edge matrix from a GIS software it will have references to node zero in either from_node or to_node. If your edge matrix in R is em then add +1 , like so: em+1 . A sample code could be like this

edgelist <- read.delim("edgelist.txt")
em <- matrix(c(edgelist$from, edgelist$to), ncol=2) +1
net <- linnet(n,edges = em)
plot(net)

This solved the problem for me. Hope it helps someone. Or if someone has another solution, please feel free to share.