[R-sig-Geo] Overlay between polygons and their intersecting lines

Gwennaël Bataille gwennael.bataille at uclouvain.be
Wed May 25 15:53:47 CEST 2016


Dear all,

I can't find a solution for the following problem:

When I first intersect a line with 2 polygons (splitting it into 2 
segments) and then use an overlay to get for each segment the attribute 
of the overlapping polygon, I sometimes get too answers (i.e. a small 
point overlapping one polygon, the rest of the segment overlapping another).

The functions I use for this are:

rgeos::gIntersection

and sp::over

Do you have any idea how I could get the attribute of the polygon the 
segments are "mostly overlapping"?

Below is a reproductible example.

Thank you very much in advance for any hint on this.

Best regards,

Gwennaël



Reproductible example:

matrix1 <- cbind(x= c(250300, 250451, 250494, 250300),

y= c(104030, 104030, 103733, 103733))

matrix2 <- cbind(x= c(250451, 250666, 250666, 250494),

y= c(104030, 104030, 103733, 103733))

P1 <- Polygon (matrix1)

P2 <- Polygon (matrix2)

Ps1 <- Polygons (list(P1), 1 )

Ps2 <- Polygons (list(P2), 2 )

Poly <- SpatialPolygons (list (Ps1, Ps2), proj4string=CRS("+proj=longlat 
+datum=WGS84"))

data <- data.frame( Attribute = c("1black", "2red") )

PolyD <- SpatialPolygonsDataFrame(Poly , data = data )

plot(PolyD, col = PolyD$Attribute)

matrix3 <- cbind(x= c(250300, 250666),

y= c(103900, 103900))

L <- Line(matrix3)

Ls <- Lines(list(L), "1")

Line <- SpatialLines( list(Ls), proj4string = CRS("+proj=longlat 
+datum=WGS84") )

Inter <- rgeos::gIntersection(Line,PolyD, byid=T)

lines( Inter[1], col = "green", lwd = 3 )

lines( Inter[2], col = "blue", lwd = 3 )

over(Inter[1], PolyD, byid=T)

'Attribute

11black'

# The green (1st) segment intersects with the black (1st) polygon => good

over(Inter[2], PolyD, byid=T)

'Attribute

11black'

# The blue(2nd) segment intersects with the red(2nd) polygon => NOT GOOD !

over(Inter[1], PolyD, byid=T, returnList = TRUE) # OK

over(Inter[2], PolyD, byid=T, returnList = TRUE)

'[[1]]

Attribute

11black

22red'

# The blue(2nd) segment appears to intersect the two polygons (I guess 
one point in common with the first polygon; the rest of the segment 
overlapping the other polygon)



-- 
Gwennaël BATAILLE, PhD student - Teaching assistant

Earth and Life Institute
Université Catholique de Louvain
SST/ELI/ELIB
Bâtiment Carnoy, c.145
Croix du sud 4-5, bte L7.07.04
1348 Louvain-la-Neuve
BELGIUM


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list