How to cast a multilinestring into a linestring with GEOS/geos?

26 Views Asked by At
multilinestring = geos::geos_unary_union(
    geos::geos_make_collection(c(
      geos::geos_make_linestring(1:3, 1:3),
      geos::geos_make_linestring(11:13, 11:13)
    ))
)
# This does not generate a linestring:
geos::geos_unnest(multilinestring)
# Convert back to linestring with ???:

You can do this with the {sf} package as follows:

multilinestring_sf = sf::st_as_sf(multilinestring)
sf::st_cast(multilinestring_sf, "LINESTRING")

Resulting in:

                        geometry
1     LINESTRING (1 1, 2 2, 3 3)
2 LINESTRING (11 11, 12 12, 1...
1

There are 1 best solutions below

0
RobinLovelace On

Solution:

geos::geos_unnest(multilinestring, keep_multi = FALSE)