{"id":2432,"date":"2020-01-13T17:43:32","date_gmt":"2020-01-13T17:43:32","guid":{"rendered":"http:\/\/www.meiotic.co.uk\/my\/?p=2432"},"modified":"2020-01-14T10:09:41","modified_gmt":"2020-01-14T10:09:41","slug":"merging-intersecting-lines-r","status":"publish","type":"post","link":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/","title":{"rendered":"Merging Intersecting Lines in R"},"content":{"rendered":"<p><script>document.getElementById('menu-item-102').className += \" current-menu-item current_page_item\";<\/script><br \/>\nI came across a problem without a clear solution. I had lots (and lots) of line segments, which represented river stretches across GB. All I wanted to do was join together touching segments into a single line feature, essenitally giving all hydrologically-independent river systems as an output. I had no attributes to help, it was purely a spatial problem, and one I thought would be easy to solve!<\/p>\n<p>But it wasn&#8217;t. There are a number of solutions that *should* work, but don&#8217;t, and a lot of suggested methods that don&#8217;t fully understand the task. I tried, and failed, with suggestions in <a href=\"https:\/\/community.esri.com\/thread\/99327\" target=\"_bottom\">ArcGIS<\/a>, <a href=\"https:\/\/gis.stackexchange.com\/questions\/198317\/join-merge-lines-that-touch-and-intersect-in-qgis\" target=\"_bottom\">QGIS<\/a> and <a href=\"https:\/\/stackoverflow.com\/questions\/9751619\/join-multiple-line-segments-into-one-line-gis\" target=\"_bottom\">rgeos<\/a>. The most promising method was using <a href=\"https:\/\/www.rdocumentation.org\/packages\/sf\/versions\/0.8-0\/topics\/aggregate.sf\" target=\"_bottom\">aggregate.sf<\/a> (with join=st_intersects) but alas, it was also fruitless.<\/p>\n<div id=\"attachment_2442\" style=\"width: 775px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-2442\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines-765x385.jpg\" alt=\"mergeLines concept\" width=\"765\" height=\"385\" class=\"size-medium wp-image-2442\" srcset=\"http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines-765x385.jpg 765w, http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines-768x386.jpg 768w, http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines-840x422.jpg 840w, http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines-400x200.jpg 400w, http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines.jpg 941w\" sizes=\"(max-width: 765px) 100vw, 765px\" \/><p id=\"caption-attachment-2442\" class=\"wp-caption-text\">This is the result I was aiming for.<\/p><\/div>\n<p>I particularly wanted a solution in R, but was open to any GIS solution, but in the end had to give up and fix it myself. Please feel free to do what you wish with the resulting script &#8211; it&#8217;s on <a href=\"https:\/\/github.com\/ScienceAndMaps\/mergeLines\/blob\/master\/mergeLines.R\" target=\"_blank\">GitHub<\/a> and embedded below:<\/p>\n<style type=\"text\/css\">\n.gist-it-gist .gist-file .gist-data {background-color:#fcf7f2 !important; border-left:solid 4px rgb(230, 164, 85) !important;}\n.gist-meta {background-color:white !important;}\n.prettyprint {overflow:hidden !important;}\n<\/style>\n<div style=\"margin-left:5% !important; margin-right:5% !important;\">\n<script src=\"http:\/\/gist-it.appspot.com\/https:\/\/github.com\/ScienceAndMaps\/mergeLines\/blob\/master\/mergeLines.R\" style=\"background-color:white;\"><\/script>\n<\/div>\n<h4 style=\"margin-top:20px;\">Data &#038; DOI<\/h4>\n<ul>\n<li>You can use any lines, but I was using this <a href=\"https:\/\/catalogue.ceh.ac.uk\/documents\/7d5e42b6-7729-46c8-99e9-f9e4efddde1d\" target=\"_blank\">GB river network<\/a>.<\/li>\n<li><a href=\"https:\/\/zenodo.org\/badge\/latestdoi\/233652814\"><img decoding=\"async\" src=\"https:\/\/zenodo.org\/badge\/233652814.svg\" alt=\"DOI\"><\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I came across a problem without a clear solution. I had lots (and lots) of line segments, which represented river stretches across GB. All I wanted to do was join together touching segments into a<\/p>\n","protected":false},"author":1,"featured_media":2442,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[8,122,9,123,110,58],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.7.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Merging Intersecting Lines in R | *meiotic*<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merging Intersecting Lines in R | *meiotic*\" \/>\n<meta property=\"og:description\" content=\"I came across a problem without a clear solution. I had lots (and lots) of line segments, which represented river stretches across GB. All I wanted to do was join together touching segments into a\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/\" \/>\n<meta property=\"og:site_name\" content=\"*meiotic*\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-13T17:43:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-14T10:09:41+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"941\" \/>\n\t<meta property=\"og:image:height\" content=\"473\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"meiotic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ScienceAndMaps\" \/>\n<meta name=\"twitter:site\" content=\"@ScienceAndMaps\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"meiotic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/\",\"url\":\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/\",\"name\":\"Merging Intersecting Lines in R | *meiotic*\",\"isPartOf\":{\"@id\":\"https:\/\/www.meiotic.co.uk\/my\/#website\"},\"datePublished\":\"2020-01-13T17:43:32+00:00\",\"dateModified\":\"2020-01-14T10:09:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/1995e1cecd67d2ff1e6a638aa331d032\"},\"breadcrumb\":{\"@id\":\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.meiotic.co.uk\/my\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Merging Intersecting Lines in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.meiotic.co.uk\/my\/#website\",\"url\":\"https:\/\/www.meiotic.co.uk\/my\/\",\"name\":\"*meiotic*\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.meiotic.co.uk\/my\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/1995e1cecd67d2ff1e6a638aa331d032\",\"name\":\"meiotic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/cacd48b532259fb621d7acf4cc462290?s=96&d=retro&r=r\",\"contentUrl\":\"http:\/\/0.gravatar.com\/avatar\/cacd48b532259fb621d7acf4cc462290?s=96&d=retro&r=r\",\"caption\":\"meiotic\"},\"sameAs\":[\"http:\/\/meiotic.co.uk\",\"https:\/\/twitter.com\/ScienceAndMaps\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Merging Intersecting Lines in R | *meiotic*","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_GB","og_type":"article","og_title":"Merging Intersecting Lines in R | *meiotic*","og_description":"I came across a problem without a clear solution. I had lots (and lots) of line segments, which represented river stretches across GB. All I wanted to do was join together touching segments into a","og_url":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/","og_site_name":"*meiotic*","article_published_time":"2020-01-13T17:43:32+00:00","article_modified_time":"2020-01-14T10:09:41+00:00","og_image":[{"width":941,"height":473,"url":"http:\/\/www.meiotic.co.uk\/my\/wp-content\/uploads\/2020\/01\/mergeLines.jpg","type":"image\/jpeg"}],"author":"meiotic","twitter_card":"summary_large_image","twitter_creator":"@ScienceAndMaps","twitter_site":"@ScienceAndMaps","twitter_misc":{"Written by":"meiotic","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/","url":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/","name":"Merging Intersecting Lines in R | *meiotic*","isPartOf":{"@id":"https:\/\/www.meiotic.co.uk\/my\/#website"},"datePublished":"2020-01-13T17:43:32+00:00","dateModified":"2020-01-14T10:09:41+00:00","author":{"@id":"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/1995e1cecd67d2ff1e6a638aa331d032"},"breadcrumb":{"@id":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.meiotic.co.uk\/my\/research\/merging-intersecting-lines-R\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.meiotic.co.uk\/my\/"},{"@type":"ListItem","position":2,"name":"Merging Intersecting Lines in R"}]},{"@type":"WebSite","@id":"https:\/\/www.meiotic.co.uk\/my\/#website","url":"https:\/\/www.meiotic.co.uk\/my\/","name":"*meiotic*","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.meiotic.co.uk\/my\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/1995e1cecd67d2ff1e6a638aa331d032","name":"meiotic","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.meiotic.co.uk\/my\/#\/schema\/person\/image\/","url":"http:\/\/0.gravatar.com\/avatar\/cacd48b532259fb621d7acf4cc462290?s=96&d=retro&r=r","contentUrl":"http:\/\/0.gravatar.com\/avatar\/cacd48b532259fb621d7acf4cc462290?s=96&d=retro&r=r","caption":"meiotic"},"sameAs":["http:\/\/meiotic.co.uk","https:\/\/twitter.com\/ScienceAndMaps"]}]}},"_links":{"self":[{"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/posts\/2432"}],"collection":[{"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/comments?post=2432"}],"version-history":[{"count":17,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/posts\/2432\/revisions"}],"predecessor-version":[{"id":2450,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/posts\/2432\/revisions\/2450"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/media\/2442"}],"wp:attachment":[{"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/media?parent=2432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/categories?post=2432"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.meiotic.co.uk\/my\/wp-json\/wp\/v2\/tags?post=2432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}