0

I have a SpatialPolygonsDataFrame and I need to convert polygon boundaries into a SpatialLinesDataFrame retaining the attributes of the 2 polygons

The structure of the SPDF looks like this

'data.frame':   11 obs. of  2 variables:
$ ID_poly  : int  1 2 3 4 5 6 7 8 9 10 ...
$ Code: chr  "33200" "32110" "33300" "32410" ...

The structure of the SLDF should be like this

'data.frame':   n obs. of  3 variables:
$ ID_line  : int  1 2 3 4 5 6 7 8 9 10 ...
$ Code1: chr  "33200" "32110" "33300" "32410" ...
$ Code2: chr  "32110" "33300" "32410" "32420" ...

where Code1 and Code2 are the values in SPDF@data$Code of the polygons that share the same boundary.

I think that

boundr <- as(SPDF, 'SpatialLinesDataFrame')

followed by an overlay (raster::intersect?) might be a solution, but I'm not sure how to properly use intersect within the same object to get the desired result (a for looping over all lines?).

This procedure has to be applied to a SPDF counting more than 10000 polygons...

This is what I'm looking for

enter image description here

SLDF@data
  ID_line Code1 Code2
0       1 11100 32100 # let's say, the green line
1       2 11100 41200 # same, the red line
2       3 33300 32410
3       4 32410 32420
  • What are the results of using "as"? When I convert a SpatialPolygonsDataFrame to a SpatialLinesDataFrame the attributes are retained in the @data slot. – Jeffrey Evans Jul 1 '16 at 14:37
  • That's right, and that's why I'd use it, but it's not the final result I'm looking for, as as (sorry for the pun!) returns a SLDF containing the full perimeter of polygons. These perimeter lines need to be split into different lines, according to where they overlap, and retaining the information of the two polygons. I'm editing the post adding an explanatory image, sorry if I didn't make myself clear. – Quechua Jul 1 '16 at 14:57
  • So the dark blue piece on the rhs would be in three separate lines, yeah? – mdsumner Jul 2 '16 at 22:38
  • Probably easiest to explode to all line segments, copying down line- (and hence poly-) id, then group by to resolve duplicates and split parent id. I think this can be done easily with spbabel (or even ggplot2::fortify) but I cannot try right now. Got example data? – mdsumner Jul 2 '16 at 22:42
  • Thank you @mdsumner, here's the example data link. I'm not sure I got all that you mean – Quechua Jul 4 '16 at 8:05
0

There is certainly a neater approch, but this worked

# convert SPDF to SLDF
bound <- as(poly, 'SpatialLinesDataFrame')

# overlay SLDF and SPDF
bound <- raster::intersect(bound, poly)

    str(bound@data)
    'data.frame':   43 obs. of  4 variables:
    $ ID_poly.1: num  6249 6249 6249 6249 6249 ...
    $ Code.1   : chr  "33200" "33200" "33200" "33200" ...
    $ ID_poly.2: num  6249 15683 15856 15986 16320 ...
    $ Code.2   : chr  "33200" "32210" "33300" "32110" ...  

# remove 'self' overlays (polygon boundary over the same polygon)
bound <- bound[bound@data$ID_poly.1!=bound@data$ID_poly.2,]

# remove 'duplicated' results (as each boundary occurs twice after the overlay)
bound@data$key <- apply(bound@data, 1, function(s) paste0(sort(s), collapse=''))
bound <- bound[!duplicated(bound@data$key),]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.