2

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?

  • 2
    It is not a plugin but a processing tool (menu processing then toolbox then enter hub in the search box) – JGH Jul 7 at 19:15
  • Hi Taras, I can see I stupidly missed this and can see where this is now in Q toolbox, thank you. Best wishes, H – Harriet Jul 8 at 12:45
2

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

| improve this answer | |
  • Hi babel - apologies for slow response, I've finally managed to try this and it has worked beautifully. This is a huge help - thank you! I'm not sure however, what you mean by the following - 'You have to use geometry(feature) to convert the feature to a geometry (a point, in this case).' I've added the expression and have a temporary layer with lines. I exported this and saved as a shapefile layer but perhaps I have missed a step here? Appreciate your help, thank you again. Best wishes, H. – Harriet Jul 8 at 12:43
  • 1
    You have not missed anything. I mentioned it just to clarify why you have to include "geometry()" in the expression. It is not enough to select the feature with "get_feature_by_id()". You than get a feature, but you need a point (a geometry) that can be used as an input to make a line from two points. Thus you have to convert the feature to a geometry - and this is done with the expression geometry(). It's no more than just expaining the function of each command in the expression. Please accept the answer if it was useful and solved your problem. – babel Jul 8 at 12:58
  • Ah i see, thank you. Certainly did help. Accepted! :-) – Harriet Jul 8 at 13:02
  • On looking closer at the lines generated these do not connect the two points. I have checked that the record IDs match (/unique ids) and they do and also checked layers were added using correct coordinates and this is fine but the line generated goes correctly from the first point but then to a random location and not the second point. I'm trying join by lines (hub lines) but this is taking so long (1% complete after c.40mins) and GIS shut down once whilst processing the algorithm and I've started again. Any suggestions would be a massive help?! – Harriet Aug 5 at 17:07
  • Maybe a screenshot or the exact expression you used could help. Otherwise, no idea what went wrong – babel Aug 7 at 9:10

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.