as.linnet function causes to hang the machine

83 views Asked by At

I am using the following code to read a shapefile as a linnet object, and in the last line where I use as.linnet, the process just hangs and I have to force quit Rstudio, I do not know what is wrong? I tried both packages on CRAN and on github, but both have the same outcome: Rstudio hangs.

library(spatstat)
library(maptools)
library(sp)
setwd("~/documents/rwork/traced")
roads<-readShapeSpatial('NLroads')
spatstat.roads<-as.psp(roads)
#when I do head(spatstat.roads), it gives me only 5 line segments
#while the shapefile has 174 line segments
plot(spatstat.roads)
final_roads<-as.linnet(spatstat.roads)

I do not know then if the problem is with my shapefile? Also I do not know what it means by:

In as.psp.SpatialLinesDataFrame(roads) : 1 columns of data frame discarded

here is the line data i am reading in. Any help would be great. Thanks.

1

There are 1 answers

3
Adrian Baddeley On BEST ANSWER

The short answer: your dataset is large; set the argument sparse=TRUE in the last line, and give the computer a few minutes.

The long answer: a SpatialLines object is basically a list of curves, each curve consisting of a sequence of straight line segments. Your dataset roads has 174 curves, consisting of a total of 38635 straight line segments (so there is an average of over 200 line segments in each curve). When you do as.psp(roads), you extract just the straight line segments, so there are 38635 of them (this would be printed out if you typed the name of the object, or you could use nsegments to count them). When you type head(spatstat.roads) you are just getting the first 5 entries.

Your dataset is a SpatialLinesDataFrame in which each curve carries additional data. These additional columns of data are ignored by as.psp currently, so it issues a warning that it has ignored them. You can extract them from the original object if you need them.

The command as.linnet(spatstat.roads) calls the function as.linnet.psp. This tries to guess which of the line segments you intended to be joined in the linear network. It does this by finding cases where two different segments have identical endpoints or very close endpoints. The argument eps controls the closeness threshold. More importantly the argument sparse determines whether to use a sparse matrix representation of the network topology. For this size of dataset, you definitely need the sparse matrices, so set sparse=TRUE.