[R-sig-Geo] SpatialLines to Graph object - computing the longest path - misconstruction

Mathieu Rajerison mathieu.rajerison at gmail.com
Tue May 21 11:23:38 CEST 2013


Sorry, I forgot the attached files.


2013/5/21 Mathieu Rajerison <mathieu.rajerison at gmail.com>

> Hello,
>
>
> I have a lines shapefile that I converted to a graph object, following the
> method in
> http://www.mail-archive.com/r-sig-geo@r-project.org/msg05836.html
>
> I have determined the farthest nodes of the graph.
>
> Then, I tried to determine the diameter's path  but the result is odd as
> the attached file shows.
>
> The problem may come from the SpatialLines to graph conversion..But I
> can't see where the problem is...
>
> Any help would be greatly appreciated...
>
> I've put the example data I used if you want to give it a try
>
> Here is the code:
>
> # READ
> l <- readOGR("IN/testline.shp", "testline")
> l.break <- gIntersection(l, l)
> l.split <- SpatialLines(lapply(1:length(l.break at lines[[1]]@Lines),
> function(x) Lines(list(l.break at lines[[1]]@Lines[[x]]),
> ID=as.character(x))))
>
> #GRAPH
> edges =
> do.call(rbind,lapply(1:length(l.split),function(x){as.vector(t(l.split at lines
> [[x]]@Lines[[1]]@coords))}))
> lengths = sqrt((edges[,1]-edges[,3])^2+(edges[,2]-edges[,4])^2)
>
> froms = paste(edges[,1],edges[,2])
> tos = paste(edges[,3],edges[,4])
> g = graph.edgelist(cbind(froms,tos),directed=FALSE)
> E(g)$weight=lengths
>
> # DIAMETER
> sel <- get.diameter(g)
> diam <- l.split[sel-1]
> plot(diam)
>
>
> Mathieu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130521/c1e0ea03/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplot02.png
Type: image/png
Size: 2169 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130521/c1e0ea03/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testline.zip
Type: application/zip
Size: 1832 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130521/c1e0ea03/attachment.zip>


More information about the R-sig-Geo mailing list