TransWikia.com

Creating line connecting polygons/points based on their location using QGIS

Geographic Information Systems Asked on November 19, 2021

I have polygon layer which looks like this (here is small cutout):
Original layer

What I need is to connect all polygons with line in somehow "logical" order – it means to go from first to last polygon (eventually point as I can create centroids) and create line connecting two nearest polygons BUT with one condition – there can’t be closed polygon (it means I can’t draw lines between three points so they create triangle) and there can be several branches – to make it clear below is the image how it should looks like.

What I get so far:

  • in QGIS I can’t use Points to Path as I have no order field (all the polygons has same ID = 1)
  • in ArcMap I am able to use two function Generate Near Table and then XY to Line which connects at least some polygons together –>
    enter image description here
  • finally I need to manually draw lines for the rest –> enter image description here

Is there any automatic way to solve this using QGIS?

Maybe adding order field to layer with values based on location or something like that. I would ideally create an automatic model in the end as I deal with lots of these layers.

One Answer

There's a Minimum Spanning Tree plugin for QGIS:

enter image description here

If you don't see it in the plugins system make sure you have the settings to show experimental plugins.

Start it from the Plugins menu and the "Minimum Spanning Tree" entry. Using the "Automatic" tab:

enter image description here

I set a simple polygon layer as the "Vector Layer" and got out a line shapefile.

enter image description here

It seems to use the centroids of the polygons for the distance ("weight") algorithm rather than the distance of nearest approach between polygons. The lines connect centroids.

The attribute table of the lines doesn't show what polygons each line comes from so you may have to do some more overlaying and intersection to get that. First use "Extract Vertices" to get two points for each line connection, and then use "Intersection" of those points over the polygons to get another point layer with the attributes of the polygon at each point. I get this:

enter image description here

that tells me that the line with id=0 connects the polygon with i equal to 9 to the one with i equal to 12. The next line, with id=1, connect polygon 9 to polygon 6, and so on for the complete tree.

Note it only seems to work on Shapefiles rather than loaded layers. Its also "experimental" and I managed to break it once or twice. I don't understand the difference between the "Vector" and the "Automatic" tab sections, for example, but the "Automatic" one seemed to work for my simple case. Hmm the "Vector" tab says the input "must be linestring" so I'm not sure what that is doing. For polygons, "Automatic" does the job.

The MST plugin doesn't seem to expose itself as a Processing algorithm so you won't be able to build all this into a single Processing step. Perhaps worth suggesting as an enhancement on their github code page.

There's an article here: https://www.sciencedirect.com/science/article/pii/S2352711020301771

Answered by Spacedman on November 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP