9

Let's say there's point layer A with 300 points and point layer B with one point

How to create a line layer that connects all points in layer A to the one point in layer B?

Using QGIS and/or PostGIS.

  • try using the st_makeline function – ziggy Mar 30 '17 at 14:29
  • @ziggy was trying to do just that but the documentation isn't very clear and there's no example with geometries from different tables – Luffydude Mar 30 '17 at 14:30
  • ive never used this function but I would imagine it would be something like this: select t1.columns etc,t2.columns etc.. st_makeline(t1.geom,t2.geom) from t1,t2 – ziggy Mar 30 '17 at 14:33
  • @ziggy getting syntax error on the brackets when I tried select id, geom as st_makeline(a.geom, b.geom), – Luffydude Mar 30 '17 at 14:37
  • post your whole query – ziggy Mar 30 '17 at 14:38
4

In QGIS, you can use the Connect Points plugin which you can download from:

Plugins > Manage and Install Plugins...

Example:

  1. Here are a couple of layers, layer_A has a bunch of points; layer_B has one. Make sure both layers contain an integer field where the values are exactly the same (e.g. both my layers have an id field where all values are 1). The plugin uses this to connect your points. When your plugin is enabled, go to its settings:

    Interface


  1. Select the options:

    Settings


  1. Result:

    Result


Note that this plugin is experimental so you will need to enable the Show also experimental plugins option (thanks to @blue_chip):

Plugins > Manage and Install Plugins > Settings
  • 2
    Not sure why my edit was deleted but anyway in case you haven't read it: wow guys some pretty varied answers here. One Python, one plugin and one Postgis. Giving the tick to the plugin one as it's the most GIS noob friendly and also because of the smiley face. Thanks all! – Luffydude Mar 30 '17 at 17:23
  • @Luffydude - Your edit was probably deleted because it didn't add much to your overall question but you're right, very different answers! :) – Joseph Mar 31 '17 at 9:01
  • 1
    You need to enable experimental plugins to find this one. I had to look a bit longer until it dawned on me. Plugins > Setting > Check Show also experimental plugins – blue_chip Oct 4 '17 at 6:10
  • @blue_chip - Apologies, I should have mentioned that. Will edit that in :) – Joseph Oct 4 '17 at 8:58
3

if both of your layers are points this below query should work just plug in your own data. I did a test with creating lines from 1 layer with 150+ point rows and a layer with 1 point

drop table if exists line;
create table line as
select layer1.id ,st_makeline(layer1.geom,point.geom) as geom from layer1,point

enter image description here

3

Assuming to start from this situation (one point layer with one feature and one point layer with 300 features):

enter image description here

you may run this code from the Python Console (after having loaded the two layers of interest in QGIS):

from qgis.core import *
from qgis.PyQt.QtCore import QVariant

layer1 = QgsMapLayerRegistry.instance().mapLayersByName('1point')[0]
crs = layer1.crs().toWkt()
layer2 = QgsMapLayerRegistry.instance().mapLayersByName('300points')[0]

outLayer = QgsVectorLayer('Linestring?crs='+ crs, 'line_output' , 'memory')
prov = outLayer.dataProvider()
fields = layer1.pendingFields()
for field in layer2.pendingFields():
    fields.append(field)
prov.addAttributes(fields)
outLayer.updateFields()

for feature in layer1.getFeatures():
    coords = feature.geometry().asPoint()
    attr1 = feature.attributes()

    for feat in layer2.getFeatures():
        seg_start = coords
        seg_end = feat.geometry().asPoint()
        attr2 = feat.attributes()
        attrs = attr1 + attr2
        outGeom = QgsFeature()
        outGeom.setGeometry(QgsGeometry.fromPolyline([seg_start, seg_end]))
        outGeom.setAttributes(attrs)
        prov.addFeatures([outGeom])

QgsMapLayerRegistry.instance().addMapLayer(outLayer)

for obtaining this result:

enter image description here

You only need to adapt the names for the layers ('1point' and '300points') to the names with which they are loaded in the Layers Panel.

My approach will work independently from the number of features in both layers.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.