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@lines[[1]]@Lines),
function(x) Lines(list(l.break@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@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

	[[alternative HTML version deleted]]

