2

I have a LINESTRING called sel_river_sf object with 3 features and I would like to merge those features in one single geometry. sel_river_sf$NAME contains the same string for the three features. I tried to use the aggregate function:

aggregate(x = sel_river_sf, by = list(sel_river_sf$NAME), do_union=T ,FUN = unique)

The output is a MULTILINESTRING object with one feature, I would like to obtain a LINESTRING object with 1 feature as the subgeometries cause me problems in the sebsequent operations. Before I tried st_combine and st_union and they show the same behaviour, also in combination with st_cast. I think there is a really simple way to obtain the expected result but I can't figure it out. Many thanks for the help! Riccardo

At the link below you can find a reproducible example, I used my real data as I think some issue is related to the geometries involved.There is only code in the link but it was too long to be pasted here.

  • 1
    Hi! It may be useful if you add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with simulated data. – agila Aug 03 '21 at 15:08
  • 1
    Hi Riccardo, just wanted to add that an example would help us to understand your problem. For getting the discussion going: what do you expect, if you combine 3 "separate" linestrings (aka 3 feature) in one combined xxxxstring? If these strings are not to be connected (e.g. forming a linestring) the result must be "multiple" linestrings (aka MULTILINESTRING). Can you elaborate on what your features and geometries are and what you want to obtain in the end (or as an interim step before pressing on)? – Ray Aug 03 '21 at 15:14
  • Dear agila and Ray, many thanks for your answer, I tried to post a reproducible example for my case. The three strings shold be connected, they represent different sector of the same river. https://www.dropbox.com/s/vgj7m4nya2nejwg/esempio.r?dl=0 – Riccardo Fornaroli Aug 04 '21 at 08:06

1 Answers1

3

The solution was obviously simple, but it was really hard for me to find it! Just use the st_line_merge function to merge to three geometries.