3

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.

Community
  • 1
  • 1
ikashnitsky
  • 2,941
  • 1
  • 25
  • 43
  • Post this question on gis.stackexchange.com and you will get your answer faster – CuriousBeing Mar 04 '16 at 11:41
  • @MaxPD I considered that. But I really need an `R`-specific solution. So, I thought, `R` community is bigger at SO – ikashnitsky Mar 04 '16 at 11:53
  • Your "desired output" has missed the border between Bulgaria and Romania! Its possible the border lines are opposite sides of the Danube here, and so they don't overlap. You might see this when you zoom into data. Something to watch out for... – Spacedman Mar 04 '16 at 12:25
  • @Spacedman Sorry, I didn't mention that in the post. This is the precise reason why I need to produce the borders shapefile again. In my research, I first decided to exclude Romania because of the non-harmonized population data Eurostat published (there was a Census in Romania in 2011 that registered an underestimated population decline). Later on, I did the data harmonization myself, and now I need to include Romania with the borders. Sorry, I thought, that wouldn't be very relevant for the post. But thanks for pointing that out. Should I update the question? – ikashnitsky Mar 04 '16 at 12:33
  • You've still got a few non-EU countries plotted grey (Norway, Switzerland,...) but without red borders! I suspect it used EU bounds but the map is Europe, or something, but if my answer gives you what you want then I'd leave it. I think we understand what's wanted from the text of the question even if the map isn't precisely right! – Spacedman Mar 04 '16 at 12:47
  • @Spacedman Yes. Again, a stupid mistake when I reproduced the maps with the new borders. I fixed this. Thanks a lot for you help! – ikashnitsky Mar 04 '16 at 13:26

1 Answers1

7

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

Spacedman
  • 92,590
  • 12
  • 140
  • 224