Length of the boundary intersection between two polygons in R

by John Asplund   Last Updated May 15, 2018 19:22 PM

As the title suggests, I am trying to find the length of the border shared by two polygons. I have run cleangeo on these shapefiles. There were no flags that came up. I have looked through several other posts on the topic such as https://stackoverflow.com/questions/33924895/intersect-spatial-lines-in-r and https://stackoverflow.com/questions/45338384/calculate-the-length-of-shared-boundaries-between-multiple-polygons. But I was unable to resolve the errors I was getting.

I have provided three separate pairs of polygons that represent some of the errors that I am encountering. They can be found at: https://www.dropbox.com/sh/kgqrbiojye2tu7o/AACe0AAqqpGmMyzVfKTiwVula?dl=0

When I run the following code, I get an error saying there is no slot of name "lines" for this object of class "SpatialCollections":

    library(sf)
    library(sp)
    library(raster)
    library(rgeos)
    library(rgdal)
    library(maptools)

    setwd("C:/Users/jasplund/Downloads/shapefiles/Georgia/")

    Shapefile <- readOGR(dsn=".",layer="test_polygons1")

    xx <- as(Shapefile, "SpatialLines")
    line <- rgeos::gIntersection(xx[1,], xx[2,],byid = TRUE)
    l_line <- sp::SpatialLinesLengths(line)
    l_line

By using the same code and replacing test_polygons1 with test_polygons2, you will see the second example. When I run

    plot(Shapefile)

I see the following image for test_polygons1.

enter image description here

When I view the intersection between these two polygons, I get the following figure.

enter image description here

This "line" has a marker on it, but some of these intersections have no markers and simply look like a line. Or, in the case of test_polygons2, the intersection forms disconnected lines.

I'm fairly confident I know what is wrong, but I am unable to resolve it. I think when the intersection of the two polygons is found, there is a very slight difference between these two polygons causing the intersection to be not exactly a line or missing some lines or simply points. I even tried using gBuffer as suggested in https://stackoverflow.com/questions/33924895/intersect-spatial-lines-in-r, but that gave me a different error, though I cannot remember what exactly this error said.

Any advice on how to exactly calculate the length of shared boundary between these polygons would be greatly appreciated.

Tags : r intersection sp


Related Questions



Intersection with 2 KML files - newbie

Updated June 09, 2017 23:22 PM

Intersect two polygons with same FID

Updated August 19, 2015 17:09 PM


Error message using intersect in r

Updated April 12, 2018 23:22 PM