Monday, 3 October 2016

Length of the boundary intersection between two polygons in R


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.


How exactly do I calculate the length of shared boundary between these polygons?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...