ikashnitsky
ikashnitsky

Reputation: 3111

r gis: find borders between polygons

Having a polygon shapefile, I need to produce a polyline shapefile containing only the common borders between polygons (see the picture).

My question is similar to 1 and 2, only I need to do this in R. The latter similar question refers to a solution with the use of Shapely package for python. The analogue of Shapely for R is rgeos. Though, I couldn't find the solution with rgeos on my own.


enter image description here

Note: the shapefile with borders used for illustration was produced in ArcGIS using the solution from similar question 1. Now I need to do the same in R.

Upvotes: 4

Views: 1928

Answers (1)

Spacedman
Spacedman

Reputation: 94182

What you want is the lines that are the difference between the set of lines from the dissolved regions and the lines of the regions themselves. IN the rgeos package, gUnaryUnion will dissolve the polygons, and gDifference will subtract.

For my small EU subset eusub, I can do this with:

library(rgeos); library(sp)
borders = gDifference(
   as(eusub,"SpatialLines"),
   as(gUnaryUnion(eusub),"SpatialLines"),
   byid=TRUE)

Note the need to convert polygons to lines because the output will be lines.

Then see this:

plot(eusub)
plot(borders, col="red",lwd=2,add=TRUE)

enter image description here

Upvotes: 8

Related Questions