QGIS – Linking Points with Lines for Display in QGIS for Better Visualization

linepointqgisqgis-3visualisation

I have 2 point shapefiles which contain lat and longs and I need to connect these with a line for display, not analysis. I am using QGIS 3.4.

To explain further, for one individual species/animal (i.e. record) I have finding location A and finding location B in a row in csv-doc – this has been added twice to QGIS to map both lat and long points independently for a single record.

I now need to, however, link for each record point A with B with lines either across shapefiles or within one shapefile. A unique ID is attached to the records. I have thousands of individual species so cannot manually digitise the connecting lines.

This is useful but I can't find the plugin – How to join many points with lines?.

Can anyone advise?

Best Answer

You can connect the points using geometry generator. Let's say you have two points layers, where the fid is matching the potints in each layer you want to connect with a line. For demonstration, I just created two layers with 10 random points on each.

In one of the layers, add a symbol layer and use geometry generator for symbol layer type - see screenshot below. As geometry type, set LineString. Now you have to define the expression that generates your line (see below for how the expression should look).

The advantage is that the line adapts automatically to every change (adding or removing points, moving points to another place etc.) in realtime. And you don't need a separate layer. However, if you would like to make the lines permanent and store them in a separate file and show them in an own layer, you can paste the same expression to the geometry by expression algorithm (from processing toolbox).

The expression could look like this, just adapt to include the layer-name you use (instead of 'your_layer2' - layer names shold look someting like 'layer_2_d5d61a64_fe2d_44bf_a62e_1168bd89d4ea' - best select the layer from the expression editor):

make_line(
   $geometry,  
   geometry(
       get_feature_by_id( 
       'your_layer2',  "fid" )
    )
)

Explanation: Selecting layer 1, use make_line (point1, point2) to create the line. Point1 can be set to $geometry, thus for each feature on your layer1, you want to have a line starting from there. The line should go to the corresponding point on the other layer: to the feature on layer2 with the same fid. Thus use get_feature_by_id(layer,feature_id) and add the layer and the fid-field (if you just paste the field-name, it will take the field from layer1 - that is what you want, because the fid for layer2 should be the same as for layer1). Best use the column in the middle of the expression editor and select the entries from there (expand the headlines for map layers and fields and values). This returns you a feature. You have to use geometry(feature) to convert the feature to a geometry (a point, in this case). The parameter (feature) is the expression we just discussed before.

You see the two points layers, each in a different color. I added a label with the fid to show how the lines are drawn.

enter image description here

Related Question