title: Intersect Spatial Lines in R
body: <p>I have the next spatial object in R.</p>
  
  <pre><code>library(sp)
  library(rgeos)
  poly1 &lt;- structure(c(-3.25753225, -3.33532866, -3.33503723, -3.35083008, 
                        -3.35420388, -3.407372, -3.391667, -3.254167, -3.248129, -3.25753225, 
                        47.78513433, 47.73738617, 47.73793803, 47.74440261, 47.74004583, 
                        47.803846, 47.866667, 47.866667, 47.806292, 47.78513433),
                      .Dim = c(10L, 2L), .Dimnames = list(NULL, c("x", "y")))
  poly2 &lt;- structure(c(-3.101871, -3.097764, -3.20532, -3.260711, -3.248129, 
                        -3.101871, 47.777041, 47.735975, 47.709087, 47.777982, 47.806292, 47.777041),
                      .Dim = c(6L, 2L), .Dimnames = list(NULL, c("x", "y")))
  sobj &lt;- SpatialPolygons(
      list(
          Polygons(list(Polygon(poly1)), ID = '1'),
          Polygons(list(Polygon(poly2)), ID = '2')),
      proj4string = CRS('+proj=merc'))
  plot(sobj)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/CgaDx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CgaDx.png" alt="The plot"></a></p>
  
  <p>I would like to obtain a Spatial Object containing the border line that the two polygons have in common, that is, the line that is in green in the next image.</p>
  
  <pre><code>lines &lt;- matrix(c(-3.248129, -3.25753225, 47.806292, 47.78513433), 2, 2)
  lobj &lt;- SpatialLines(
      list(
          Lines(list(Line(lines)), ID = '1')),
      proj4string = CRS('+proj=merc'))
  
  plot(lobj, col = 'green', add = TRUE)
  
  lines &lt;- matrix(c(-3.248129, -3.25753225, 47.806292, 47.78513433), 2, 2)
  lobj &lt;- SpatialLines(
      list(
          Lines(list(Line(lines)), ID = '1')),
      proj4string = CRS('+proj=merc'))
  
  plot(lobj, col = 'green', add = TRUE)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DOZZN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DOZZN.png" alt="enter image description here"></a></p>
  
  <p>So far I have tried with the <code>gIntersection</code> function in <code>rgeos</code> package but it does not do what I require. How would I get this?</p>