0

how to transform the following multilinestring in a linestring featuring the same aspect of the multilinestring (check against plot)?

ml<-structure(list(geometry = structure(list(structure(list(structure(c(1745287.23933068, 
1745287.67630239, 1745288.12199725, 1745289.03845948, 1745289.98654929, 
1745290.47177848, 1745290.96409975, 1745291.96894511, 1745292.48092803, 
1745292.99892079, 1745294.05186332, 1745295.12561036, 1745296.21800057, 
1745297.32687358, 1745297.88681457, 1745298.45006988, 1745299.58543072, 
1745300.73079798, 1745301.8840141, 1745303.04292191, 1745303.62383925, 
1745303.65892312, 1745304.20536448, 1745304.93905298, 1745305.38446676, 
1745306.10922967, 5047316.86327092, 5047311.88481158, 5047306.90703783, 
5047296.95342224, 5047287.00235098, 5047282.0277297, 5047277.05373024, 
5047267.10744701, 5047262.13513129, 5047257.16337052, 5047247.22135357, 
5047237.28123403, 5047227.34283621, 5047217.40597225, 5047212.43804541, 
5047207.47044357, 5047197.53604225, 5047187.60255244, 5047177.66975179, 
5047167.73741281, 5047162.77133866, 5047162.47158816, 5047157.80530432, 
5047151.54377516, 5047147.87505837, 5047141.905441), .Dim = c(26L, 
2L)), structure(c(1745290.43145024, 1745289.36560597, 1745288.85127806, 
1745288.34935059, 1745287.38270942, 1745286.46570651, 1745285.5983647, 
1745284.78070559, 1745284.39051346, 1745284.01274954, 1745283.29451568, 
1745282.62602188, 1745282.00728479, 1745281.43831982, 1745281.17250635, 
1745280.91914113, 1745280.44976166, 1745280.03019309, 1745279.66044586, 
1745279.57012111, 1745279.34052919, 1745279.19925985, 1745279.07045105, 
1745278.85021814, 1745278.67983597, 1745278.55930877, 1745278.48863955, 
1745278.47200227, 1745278.46783005, 1745278.49688081, 1745278.5420099, 
1745278.5757911, 1745278.70455895, 1745278.88318116, 1745278.9911863, 
1745279.11165327, 1745279.38996961, 1745279.71812324, 1745280.09610598, 
1745280.52390843, 1745280.75648877, 1745280.78135548, 1745281.00151994, 
1745281.52892861, 1745282.1061213, 1745282.49394758, 1745282.73306663, 
1745283.33821922, 1745283.40889108, 1745283.74373711, 1745284.09002103, 
1745284.81580247, 1745285.28296443, 1745285.58406251, 5047725.84549261, 
5047715.91049629, 5047710.94103458, 5047705.9703051, 5047696.02516655, 
5047686.07532827, 5047676.12103804, 5047666.1625437, 5047661.18179748, 
5047656.20009322, 5047646.23393468, 5047636.26431624, 5047626.29148613, 
5047616.31569269, 5047611.32676234, 5047606.33718431, 5047596.35620947, 
5047586.37301668, 5047576.38785455, 5047573.72059734, 5047566.40097169, 
5047561.4069627, 5047556.41261678, 5047546.42303854, 5047536.43248571, 
5047526.44120706, 5047516.44945136, 5047511.45347238, 5047506.45746743, 
5047496.46550406, 5047490.75121657, 5047486.47381006, 5047476.48263421, 
5047466.49222531, 5047461.4973862, 5047456.50283211, 5047446.51470336, 
5047436.52808776, 5047426.54323398, 5047416.56039065, 5047411.56980059, 
5047411.05157031, 5047406.57980634, 5047396.60172957, 5047386.6264088, 
5047380.36490035, 5047376.65406725, 5047367.69504537, 5047366.68463887, 
5047361.69950263, 5047356.71504116, 5047346.74806808, 5047340.68899902, 
5047336.78377077), .Dim = c(54L, 2L)), structure(c(1745306.58970267, 
1745307.79493858, 1745308.44091665, 1745309.00011323, 1745309.60222312, 
1745310.20341007, 1745311.40104073, 1745312.25725564, 1745312.58911105, 
1745313.16953118, 1745313.41561993, 1745315.1770996, 1745315.27543957, 
1745316.42722587, 1745316.89316172, 1745317.54007008, 1745317.8399176, 
5047137.94795374, 5047128.02084911, 5047122.70015468, 5047118.09370727, 
5047113.12998375, 5047108.16610094, 5047098.2375332, 5047091.09132923, 
5047088.30754081, 5047083.39983841, 5047081.31905191, 5047066.21272435, 
5047065.35694747, 5047055.19165519, 5047051.02238832, 5047045.14306009, 
5047042.38692253), .Dim = c(17L, 2L)), structure(c(1745308.44100173, 
1745313.16953118, 1745313.28153259, 1745313.41561993, 1745313.76372521, 
1745314.92098523, 1745315.1770996, 1745315.22570862, 1745315.27543957, 
1745315.49188505, 1745316.05699045, 1745316.42722587, 1745316.67590187, 
1745316.89316172, 1745317.16783712, 1745317.54007008, 1745317.8636033, 
1745318.87931992, 1745319.76988423, 1745320.56335911, 1745321.27818555, 
1745321.92728077, 1745322.5201282, 1745323.06394616, 1745323.56438844, 
1745324.02598764, 1745324.45244833, 1745324.84684797, 1745325.21177883, 
1745325.54945088, 1745325.86176804, 1745326.15031681, 5047122.69944846, 
5047083.39983841, 5047082.46897571, 5047081.31905191, 5047078.37567178, 
5047068.44148994, 5047066.21272435, 5047065.79585678, 5047065.35694747, 
5047063.47338966, 5047058.50457956, 5047055.19165519, 5047052.99692139, 
5047051.02238832, 5047048.56454993, 5047045.14306009, 5047042.20267757, 
5047032.68916629, 5047024.08516485, 5047016.17019134, 5047008.80061779, 
5047001.8767004, 5046995.32583198, 5046989.09321235, 5046983.13627728, 
5046977.42118602, 5046971.92050768, 5046966.61164131, 5046961.47570396, 
5046956.49672757, 5046951.66106613, 5046946.95807376), .Dim = c(32L, 
2L))), class = c("XY", "MULTILINESTRING", "sfg"))), n_empty = 0L, crs = structure(list(
    epsg = NA_integer_, proj4string = "+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +units=m +no_defs"), class = "crs"), class = c("sfc_MULTILINESTRING", 
"sfc"), precision = 0, bbox = structure(c(xmin = 1745278.46783005, 
ymin = 5046946.95807376, xmax = 1745326.15031681, ymax = 5047725.84549261
), class = "bbox"))), row.names = 1L, class = c("sf", "data.frame"
), sf_column = "geometry", agr = structure(integer(0), class = "factor", .Label = c("constant", 
"aggregate", "identity"), .Names = character(0)))

my idea is to first extract points by

library(sf)
st_cast(ml, "POINT")

and then somehow join the resulting points to get a single linestring

the problem is that the multilinestring is apparently composed by not contiguous segments

any idea to point me in the right direction?

sorry for such a loose question but I'm searching my way through this: how to linear connect points extracted from many linestrings that are apparently not in order (I've searched and also found similar question somewhere here but not useful for sorting out my problem)

  • Not only are your line segments not contiguous but they overlap... – Spacedman Jan 27 at 18:19
  • yes, also. So what do you reccomend me to do? Completely redraw geometry? – maxbre Jan 28 at 8:32
  • not sure, its just that having overlapping parts is a different problem from trying to splice non-contiguous parts into the right order to make a single line... – Spacedman Jan 28 at 14:48
  • 1
    I cast it into LINESTRING with mls = st_cast(ml, "LINESTRING"), and there were four, but when plotted it only looks like three parts - plotting individually in different colours and widths shows the overlap. – Spacedman Jan 28 at 16:44
  • 1
    If you have well-behaved lines of points then one approach is to join each point to its two nearest neighbours and then [waves hands] fix up the ends where you'll have one point with three neighbours... – Spacedman Jan 28 at 16:48

Your Answer

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

Browse other questions tagged or ask your own question.