Skip to content
Prev 22535 / 29559 Next

Calculating the length of a line created from a spatial intersect.

Walter,

Next time, please try to give as a simple self contained example in R
showing what you have done, and where you are stuck.

Here is how you can intersect lines and polygons, keeping their attributes:

library(raster)
library(rgeos)

# example data
p <- shapefile(system.file("external/lux.shp", package="raster"))

k1 <- (rbind(c(5.5, 49.6), c(6.5, 49.6)))
k2 <- (rbind(c(5.5, 50), c(6.5, 50)))
k <- SpatialLines(list(Lines(list(Line(k1)), "1"),  Lines(list(Line(k2)), "2")))
k <- SpatialLinesDataFrame(k, data.frame(LID=1:2))
crs(k) <- crs(p)

# intersection
x <- intersect(k, p)

## note that   x <- intersect(p, k)
## currently does not work; I'll fix that.

x
data.frame(x)

Robert
On Wed, Apr 1, 2015 at 8:18 AM, Walter Anderson <wandrson01 at gmail.com> wrote: