Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate a multipolygon geometry into several polygons objects after performing a st_union() operation?

Tags:

join

r

spatial

I have a set of polygons of which some intersect and/or touch (common borders). I am using R's sfpackage to perform operations on polygons. My approach so far was to use sf::st_union() which joins neighboring and intersecting polygons as i want, but it also combines all polygons into a MULTIPOLYGONgeometry. I would like to have each polygon separated as a sf(data.frame) class where each polygon object is show as a row in the data.frame

I show below an example. I start by creating an example dataset:

    # Creating four example polygons, of which two (two squares) are neighbors:

    p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
    pol1 <-st_polygon(list(p1))
    p2 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
    pol2 <-st_polygon(list(p2))
    p3 <- rbind(c(4,0), c(4,1), c(5,1), c(5,0),c(4,0))
    pol3 <-st_polygon(list(p3))
    p4 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
    pol4 <-st_polygon(list(p4))

    d = data.frame(some_attribute = 1:4)
    d$geometry = st_sfc(pol1,pol2,pol3,pol4)
    df = st_as_sf(d)

    class(df)
    #[1] "sf"         "data.frame"

    df
    # Simple feature collection with 4 features and 1 field
    # geometry type:  POLYGON
    # dimension:      XY
    # bbox:           xmin: 0 ymin: 0 xmax: 5 ymax: 4
    # epsg (SRID):    NA
    # proj4string:    NA
    # some_attribute                       geometry
    # 1              1 POLYGON((0 0, 1 0, 3 2, 2 4...
    # 2              2 POLYGON((3 0, 4 0, 4 1, 3 1...
    # 3              3 POLYGON((4 0, 4 1, 5 1, 5 0...
    # 4              4 POLYGON((3 3, 4 2, 4 3, 3 3))

plot(df) gives:

plot(df)

I then perform a st_union() operation to combine all polygon geometries that intersect or touch (the two squares above) into one:

    df_union <- df %>% st_union() 
    df_union
    # Geometry set for 1 feature 
    # geometry type:  MULTIPOLYGON
    # dimension:      XY
    # bbox:           xmin: 0 ymin: 0 xmax: 5 ymax: 4
    # epsg (SRID):    NA
    # proj4string:    NA
    # MULTIPOLYGON(((3 3, 4 3, 4 2, 3 3)), ((4 0, 3 0...

plot(df_union) results in:

plot(df_union)

As shown above the result of df_union is a MULTIPOLYGON geometry with one row only. I would like to perform an operation that separates each polygon into a geometry as shown in the figure above, but resulting in several polygon objects, something equivalent to this:

    # Simple feature collection with 4 features and 1 field
    # geometry type:  MULTIPOLYGON
    # dimension:      XY
    # bbox:           xmin: 0 ymin: 0 xmax: 5 ymax: 4
    # epsg (SRID):    NA
    # proj4string:    NA
    # some_attribute                       geometry
    # 1              1 POLYGON((0 0, 1 0, 3 2, 2 4...
    # 2              2 POLYGON((3 0, 4 0, 5 1, 5 0...
    # 3              3  POLYGON((3 3, 4 2, 4 3, 3 3))

How can i do this using the sf package?

like image 571
trevi Avatar asked Feb 28 '17 15:02

trevi


People also ask

How do you convert Linestring to polygons?

geometry. Polygon to simply convert to line string to a polygon. It will connect the first and last coordinates. Try Polygon([(0, 0), (1, 1), (1, 2), (0, 1)]) or Polygon(s1) to produce POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0)).

What is the difference between St_union () and St_combine ()?

st_combine returns a single, combined geometry, with no resolved boundaries; returned geometries may well be invalid. If y is missing, st_union(x) returns a single geometry with resolved boundaries, else the geometries for all unioned pairs of x[i] and y[j].

What is Multipolygon?

A MULTIPOLYGON is a collection of Polygons. MultiPolygons are useful for gathering a group of Polygons into one geometry. For example, you may want to gather the Polygons denoting a group of properties in a particular municipality.


1 Answers

Thanks to Edzer in providing the answer and for giving us the great sf package!

As mentioned by Edzer performing an st_cast converts the MULTIPOLYGON object into several POLYGON objects:

    df_union_cast <- st_cast(df_union, "POLYGON")
    df_union_cast
    # Geometry set for 3 features 
    # geometry type:  POLYGON
    # dimension:      XY
    # bbox:           xmin: 0 ymin: 0 xmax: 5 ymax: 4
    # epsg (SRID):    NA
    # proj4string:    NA
    # POLYGON((3 3, 4 3, 4 2, 3 3))
    # POLYGON((4 0, 3 0, 3 1, 4 1, 5 1, 5 0, 4 0))
    # POLYGON((0 0, 1 4, 2 4, 3 2, 1 0, 0 0))
like image 179
trevi Avatar answered Oct 10 '22 00:10

trevi