# Connecting thousand of points in QGIS

I have thousand of points generated along river channels. I am trying to connect these points based on distance. I tried "Join attributes by nearest Points" and "geometry by expression" in QGIS. The method was mentioned here by Babel. It works somehow. However, there are many gaps between the line segments and some lines are repeated drew.

How can I use QGIS to fill the gap and remove the repeated line segments?

• As far as I can see, this solution should work: gis.stackexchange.com/a/382246/88814 Dec 30 '20 at 0:09
• Please decide whether it’s ArcMap, QGIS or Python that you wish to ask about in this particular question. If it’s Python then please include a code snippet that illustrates what you have tried and where you are stuck.
– PolyGeo
Dec 30 '20 at 0:13
• As long as the question is closed, I can't add an answer, so please update your question (stating you're interested in a solution in QGIS) so that it can be reopened. Just to give a few ideas: 1. from the red lines, create start- and end points and use these to cretate a connecting line where you have gaps. 2. Repeated line segments: define a min/max value for the angle of your line to avoid this. 3. Can you use river date (e.g. OpenStreetMap)? You could snap your points to this line and try this: gis.stackexchange.com/a/382253/88814 (should work better with rivers than with streets). Dec 30 '20 at 11:08
• I think part of the question (and resulting answer) is going to be "what do you want the output to look like"? --- Those straight line segments are probably not the exact course traveled. A moving average applied to each segment, followed by a curve fitting is probably more representative of the route taken; but you should be clear in your question if you want to preserve the error or beautify the output (at risk of introducing error).
– Rob
Dec 30 '20 at 17:23
• Does this answer your question? Points from various sources to one line Dec 31 '20 at 18:38

Find a river centerline dataset or digitize your own.

Then use the processing steps:

Result:

• Thank you. The points are obtained from the google earth engine and I am trying to build river channel lines for global rivers. Therefore, I cannot digitize them manually. The existed global channel lines are simplified also. Jan 1 at 0:22
Let's suppose your points do not have an order attribute like an id or timestamp that would make it ease to connect the points accordingly (from 1 to 2 to 3 to 4 etc.). So I suppose your points are just randomly distributed all along the river with varying distances between them. I created such a points layer (see my first screenshot with the id as label) to demonstrate how to generate a connecting line based on location alone. There are five steps involved, including the Plugin `Join multiple lines`. Install it before you start from `Menu Plugins / Manage and Install Plugins…`. This plugin should do what you want to do, connecting the separate lines on your screenshot. Steps 1 to 3 generate such lines from a points layer.
1. `Menu Processing / Toolbox / Join attributes by nearest` - set `Maximum nearest neighbors` to 1 only! Optionally, you can set a `Maximum distance`
2. `Menu Processing / Toolbox / Geometry by expression`: select the output from above as input layer, for `Output geometry type` select `line` and as `Geometry expression` insert this expression: `make_line ( make_point ( "feature_x" , "feature_y" ) , make_point ( "nearest_x" , "nearest_y" ) )`
3. `Menu Processing / Toolbox / Delete duplicate geometries` and set the output from 2 as input layer
4. On the output layer from 3, select all features (I use Ctrl - A on my Windows machine, or click on the `Select all` icon in the Toolbar )
5. Open/run the Plugin `Join multiple lines` (from Menu Vector): you get the connecting line you wanted, see screenshot: