Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r gis: find borders between polygons

Tags:

r

gis

rgdal

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.

like image 943
ikashnitsky Avatar asked Sep 13 '25 20:09

ikashnitsky


1 Answers

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

like image 115
Spacedman Avatar answered Sep 16 '25 12:09

Spacedman