1

For a project I use OpenStreetMap road data. However, if multiple roads are parallel to each other (say within 30 meters), I only want to keep one line, not both.

See this example where a part of the road is consisting of two parallel lines: enter image description here

You can find the data as sf object here:

data <-sf::st_read('{
"type": "FeatureCollection",
                                 "name": "lines",
                                 "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::2056" } },
                                 "features": [
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640024.617221906781197, 1128406.329895808827132 ], [ 2639991.091514538042247, 1128410.510452873073518 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2636907.578166923951358, 1127305.013125614961609 ], [ 2636936.210024286992848, 1127309.156457853503525 ], [ 2636968.172334032133222, 1127314.287422464461997 ], [ 2637044.001726549118757, 1127333.393809959525242 ], [ 2637074.383794051595032, 1127342.36205457826145 ], [ 2637088.102748209610581, 1127346.892797288252041 ], [ 2637101.073931166436523, 1127351.474571102298796 ], [ 2637138.557396514806896, 1127363.432608185335994 ], [ 2637209.208556334488094, 1127387.389548795297742 ], [ 2637252.992265183012933, 1127402.244034755276516 ], [ 2637346.153418523725122, 1127433.166850040666759 ], [ 2637356.738831535447389, 1127435.966893509961665 ], [ 2637405.380862294696271, 1127448.829231970012188 ], [ 2637444.721707837190479, 1127456.798631539568305 ], [ 2637486.308976805768907, 1127464.081847212510183 ], [ 2637545.805482633411884, 1127472.287947679869831 ], [ 2637588.81973597034812, 1127468.130486275302246 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2637588.81973597034812, 1127468.130486275302246 ], [ 2637617.827702940441668, 1127459.917582942172885 ], [ 2637645.132548771798611, 1127448.036818621214479 ], [ 2637689.381374039687216, 1127426.323308076476678 ], [ 2637711.088814091403037, 1127415.008289225865155 ], [ 2637731.801571876741946, 1127407.522420661756769 ], [ 2637754.358512750826776, 1127403.338680840330198 ], [ 2637783.080900636035949, 1127403.929242005106062 ], [ 2637805.042558237910271, 1127409.936023102840409 ], [ 2637829.342208391055465, 1127421.582566830562428 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2637956.705710261594504, 1127522.363951807375997 ], [ 2637930.925739976111799, 1127504.815742473816499 ], [ 2637892.532412434462458, 1127473.648470476502553 ], [ 2637853.203425844199955, 1127438.017780690686777 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2638514.182559803128242, 1127933.581524671753868 ], [ 2638499.359279229305685, 1127914.077365342294797 ], [ 2638473.478856971021742, 1127889.811669147573411 ], [ 2638360.730201236438006, 1127819.805245234631002 ], [ 2638334.2469532918185, 1127799.327139176893979 ], [ 2638251.208152179606259, 1127727.732368913013488 ], [ 2638184.802185411565006, 1127673.341223593102768 ], [ 2638120.797360077034682, 1127626.55867326259613 ], [ 2638009.433593571651727, 1127559.135549189057201 ], [ 2637956.705710261594504, 1127522.363951807375997 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2638518.230566815938801, 1127940.37738674110733 ], [ 2638514.182559803128242, 1127933.581524671753868 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2638775.311564370989799, 1128095.185755105223507 ], [ 2638711.175722673535347, 1128096.787253802642226 ], [ 2638674.782318400219083, 1128095.076019098516554 ], [ 2638636.515371216461062, 1128083.914924237877131 ], [ 2638606.443746893666685, 1128068.281836913898587 ], [ 2638583.044151852838695, 1128049.03402814664878 ], [ 2638566.286373481620103, 1128029.706352515378967 ], [ 2638554.451437803450972, 1128012.855770225636661 ], [ 2638529.85405474063009, 1127965.063855364453048 ], [ 2638518.230566815938801, 1127940.37738674110733 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2638955.928897099103779, 1128196.063064313028008 ], [ 2638909.089371021836996, 1128159.521099467994645 ], [ 2638873.880816109012812, 1128131.747506777755916 ], [ 2638828.283976610284299, 1128106.708874937845394 ], [ 2638795.451784070581198, 1128097.538193684071302 ], [ 2638775.311564370989799, 1128095.185755105223507 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2639083.774053867906332, 1128275.305024244822562 ], [ 2639040.259293906390667, 1128253.85782260983251 ], [ 2638999.005936536472291, 1128228.15667894622311 ], [ 2638955.928897099103779, 1128196.063064313028008 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2639157.12251483136788, 1128302.292687038891017 ], [ 2639134.361677292734385, 1128295.47538756695576 ], [ 2639083.774053867906332, 1128275.305024244822562 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2639991.091514538042247, 1128410.510452873073518 ], [ 2639882.457077416591346, 1128424.067332472885028 ], [ 2639866.164695278275758, 1128426.094380943104625 ], [ 2639800.633557313121855, 1128430.632155400468037 ], [ 2639800.4255525120534, 1128430.630787155358121 ], [ 2639738.981251694262028, 1128428.770628163125366 ], [ 2639676.458924663253129, 1128420.945443539181724 ], [ 2639660.485897106584162, 1128417.750264805043116 ], [ 2639636.760136045049876, 1128412.54778755758889 ], [ 2639634.467430759686977, 1128412.065865133656189 ], [ 2639510.223475671373308, 1128373.967694660648704 ], [ 2639441.502333330456167, 1128359.623405892169103 ], [ 2639176.607571490108967, 1128308.132822000654414 ], [ 2639157.12251483136788, 1128302.292687038891017 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640220.733519095927477, 1128401.004802472190931 ], [ 2640211.346283114515245, 1128400.364336533704773 ], [ 2640175.129508480429649, 1128397.900398271623999 ], [ 2640126.635816971305758, 1128397.200628163758665 ], [ 2640081.374219815246761, 1128400.54692515777424 ], [ 2640024.617221906781197, 1128406.329895808827132 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640459.141703600995243, 1128479.377829450881109 ], [ 2640447.749712003394961, 1128474.399167499737814 ], [ 2640380.011750067118555, 1128444.8207830816973 ], [ 2640330.356532402802259, 1128426.56936786393635 ], [ 2640292.247431453783065, 1128415.598904620623216 ], [ 2640260.083885305561125, 1128409.559617761988193 ], [ 2640243.678967818152159, 1128406.47118879808113 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640459.141703600995243, 1128479.377829450881109 ], [ 2640499.729281128384173, 1128491.733302428387105 ], [ 2640515.780059492215514, 1128497.032294345553964 ] ] } },
                                 { "type": "Feature", "properties": { "name": "Kantonsstrasse", "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640508.752271783072501, 1128515.683639300055802 ], [ 2640494.69864966487512, 1128500.915430275956169 ], [ 2640459.141703600995243, 1128479.377829450881109 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640591.426997083704919, 1128700.286944603314623 ], [ 2640573.361636431887746, 1128637.066995644243434 ], [ 2640556.93290679436177, 1128583.629711782792583 ], [ 2640542.596627078484744, 1128558.19851510412991 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640634.315186736639589, 1128852.741162184858695 ], [ 2640591.426997083704919, 1128700.286944603314623 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640568.961676721926779, 1128552.194320729700848 ], [ 2640568.337842764332891, 1128565.952696043998003 ], [ 2640568.74016746878624, 1128579.484499869169667 ], [ 2640587.083803089335561, 1128644.896304163848981 ], [ 2640647.432219261303544, 1128864.557466678787023 ], [ 2640659.338045843876898, 1128905.280373595189303 ], [ 2640676.703749062959105, 1128951.998599978163838 ], [ 2640700.721416229382157, 1129000.395790871465579 ], [ 2640735.063188516069204, 1129056.522040174109861 ], [ 2640763.695441822055727, 1129095.601357552455738 ], [ 2640796.467309108469635, 1129131.907301587052643 ], [ 2640843.617250185459852, 1129177.870903872651979 ], [ 2640914.054522940889001, 1129229.350613886723295 ], [ 2640937.723099950235337, 1129244.651777419727296 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640757.80959601374343, 1129101.386887629283592 ], [ 2640727.794745446648449, 1129061.709101487416774 ], [ 2640692.160525057464838, 1129004.173487601801753 ], [ 2640668.653601761441678, 1128954.279000776587054 ], [ 2640650.857570433523506, 1128906.246127808233723 ], [ 2640634.315186736639589, 1128852.741162184858695 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640937.723099950235337, 1129244.651777419727296 ], [ 2640884.575979494024068, 1129216.567132767755538 ], [ 2640834.505635797511786, 1129178.809903194895014 ], [ 2640790.685560660902411, 1129138.249340022914112 ], [ 2640757.80959601374343, 1129101.386887629283592 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2640937.723099950235337, 1129244.651777419727296 ], [ 2640973.317155364435166, 1129262.068163626361638 ], [ 2641021.061589201912284, 1129281.356940332101658 ], [ 2641055.12410988798365, 1129292.827070144470781 ], [ 2641100.07058742782101, 1129304.360212822444737 ], [ 2641203.197658776305616, 1129329.585350579814985 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641203.197658776305616, 1129329.585350579814985 ], [ 2641256.336991337127984, 1129344.899588175816461 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641256.336991337127984, 1129344.899588175816461 ], [ 2641331.679016280453652, 1129367.703025979921222 ], [ 2641375.308749107643962, 1129384.643097701016814 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641375.308749107643962, 1129384.643097701016814 ], [ 2641419.539288274478167, 1129402.67704408778809 ], [ 2641449.172559849917889, 1129419.566300727892667 ], [ 2641474.845244272146374, 1129438.86312183085829 ], [ 2641499.090044118929654, 1129461.91884477622807 ], [ 2641515.893251080997288, 1129482.789167637005448 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641515.893251080997288, 1129482.789167637005448 ], [ 2641527.053390990942717, 1129499.818857635604218 ], [ 2641542.371573850046843, 1129530.517409328836948 ], [ 2641554.813468728680164, 1129560.529234275687486 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641554.813468728680164, 1129560.529234275687486 ], [ 2641571.798691755626351, 1129600.799743870506063 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641571.798691755626351, 1129600.799743870506063 ], [ 2641579.028411061968654, 1129618.069324130192399 ], [ 2641593.81690870039165, 1129645.062481506960467 ], [ 2641609.590179868973792, 1129667.726906317286193 ], [ 2641630.164067408069968, 1129690.102023821556941 ], [ 2641657.937641301658005, 1129712.782477673143148 ], [ 2641672.702508094720542, 1129721.877654568525031 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641672.702508094720542, 1129721.877654568525031 ], [ 2641697.607520514167845, 1129734.922516576014459 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2641988.126171602867544, 1129979.00014924351126 ], [ 2642000.519842355512083, 1129984.444486993132159 ], [ 2642013.733443362638354, 1129989.383173610316589 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642013.733443362638354, 1129989.383173610316589 ], [ 2642022.667884388938546, 1129992.735808059573174 ], [ 2642033.673836796078831, 1129995.035640133311972 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642033.673836796078831, 1129995.035640133311972 ], [ 2642047.220861368812621, 1129997.430974067887291 ], [ 2642062.389676512684673, 1129999.203959874808788 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642216.498414577450603, 1130018.775964488973841 ], [ 2642332.844259990844876, 1130036.964690679684281 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642332.844259990844876, 1130036.964690679684281 ], [ 2642363.633902007248253, 1130041.904902320355177 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642363.633902007248253, 1130041.904902320355177 ], [ 2642421.233895586337894, 1130053.569923185510561 ] ] } },
                                 { "type": "Feature", "properties": { "name": null, "junction": null, "roadtype": "primary", "tunnel": null }, "geometry": { "type": "LineString", "coordinates": [ [ 2642421.233895586337894, 1130053.569923185510561 ], [ 2642490.890797237399966, 1130068.066032829461619 ], [ 2642540.520605927333236, 1130081.632762401597574 ], [ 2642591.412120816297829, 1130098.654991130810231 ], [ 2642639.416912191547453, 1130118.692182783503085 ], [ 2642713.507402535993606, 1130154.132812721887603 ], [ 2642760.311464536935091, 1130176.119046938605607 ] ] } }
                                 ]
        }')

The part where parallel lines occur should be simplified to one line. I tried to buffer the lines and if a line is within the buffer of another, only keep one, but I struggle to have one consecutive line.

Any idea how to achieve this goal with R?

1

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.