[R-sig-Geo] Calculating the length of a line created from a spatial intersect.

Robert J. Hijmans r.hijmans at gmail.com
Wed Apr 1 19:36:12 CEST 2015


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:
> Hello all,
>
> I am continuing on the process of automating an analysis that I
> developed with ArcInfo using R with the sp, rgdal, rgeos, and raster
> libraries.
>
> This step is similiar to my previous question concerning calculating the
> area of a polygon created from a spatial intersect; however, the command
> I was referred doesn't perform the merge of the two data frames as it
> does when both spatial data sets are polygons.
>
> Here is the process I am using in ArcInfo that I need to replicate:
>
> I have a shape file (lines) that defines the limits of all of the
> projects with each project having a unique identifier.
>
> I have another shape file (lines) that represent the road centerlines
> and have a traffic volumen associated with each link.
>
> Process
> Step 1.
> I then buffer these project lines to create a second shape file that
> represents the 'footprint' of the project. (Creates polys).  This is a
> small footprint (250 ft) that is solely used to associate the
> appropriate portion of any adjacent roadways.  Mostly to accomodate
> spatial errors...
>
> Step 2.
> In ArcInfo, I perform an intersection of the two shape files
> (footprint and roadway centerlines) and this creates a third shape file
> which has a unique line for every project buffer/roadway intersection.
> This attribute table contains the attributes from both original shape files
>
> Step 3.
> I then perform a calculation that provides a weighted volume to
> associate with each project.  The weighting is based upon the lengths of
> the road segments that are 'cut' by the project in relation to their
> original lengths.
>
> My issue is that I have not found a method to 'intersect' the two
> spatial data sets and retain both of their attributes (and calculate new
> lengths).
>
> Walter Anderson
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list