4

I would like to generate lines from origin to destination points as illustrated below. In this example, ID is a unique ID of each point and used as origin whereas Dest is used as destination from ID.

enter image description here

I found some posts which related to my question but it cannot directly solve my issue.

Workflow for creating line features between two coordinate pairs in QGIS

Creating lines from starting to arrival points?

QGIS: writting an expression in geometry generator for a straight line with given azimuth

I think the third post using Geometry by expression tool seems to be the one which solve my issue but I am not sure how to draw line from start to end points.

I do not need to use geometry generator if other solutions are easier and faster.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
HSJ
  • 355
  • 1
  • 7

3 Answers3

5

Assuming that you are working with a single layer Points, having the above attributes (ID, Dest & Name) and your Point geometries, I'd probably go for a Virtual Layer via the DB Manager; run:

SELECT
  a."Name" AS "Ori_Name",
  b."Name" AS "Dest_Name",
  MakeLine(a.geometry, b.geometry) AS geom
FROM
  "Points" AS a
  JOIN
  "Points" AS b
    ON a."Dest" = b."ID"
WHERE
  a."Dest" <> b."ID"
;

and add the result as a new layer.

geozelot
  • 30,050
  • 4
  • 32
  • 56
5

Easiest is probably using QGIS expressions with Geometry generator (for visualization only) or Geometry by expressions (to create actual geomtries) (see here for details on both options) with this expression:

make_line (
    $geometry ,
    geometry (get_feature (@layer, 'ID', "DEST"))
)

Using Geomtry by expression, you must repace the variable @layer with the name of the points layer in single quotes, e.g. 'points' Form a layer named points.

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208
  • Thank you, visualization in sympoliby works well but I cannot generate lines using geometry by expression. I am using the same expressions in both methods. I specified layer name in @layer accordingly. – HSJ Jul 10 '22 at 10:25
  • See updated answer: you must replace @layer by the name of your layer in single quotes. – Babel Jul 10 '22 at 10:27
  • Van you bei note Preise what wenn wrong? So you get an error. message? Can you Post a screenshot? – Babel Jul 10 '22 at 10:30
  • I also realized that I need to enclose layer name by single quotes. Thank you, it worked. – HSJ Jul 10 '22 at 10:31
  • 1
    Sorry, did not mention single quotes in NY initial answer. Glad to hear it worked. – Babel Jul 10 '22 at 10:42
4

You can also use pyqgis:

pointlayer = QgsProject.instance().mapLayersByName('pointlayer')[0] #Change pointlayer to the name of your point layer
points = {f['ID']:f.geometry().asPoint() for f in pointlayer.getFeatures()} #A dictionary of all point ID's and their points

#Create a memory line layer newlayer = QgsVectorLayer("LineString?crs={}&index=yes".format(pointlayer.crs().authid()), 'newLayer', 'memory') provider = newlayer.dataProvider()

#Add new features to it for f in pointlayer.getFeatures(): if f['ID']!=f['Dest']: #If the origin and source ids are not the same newfeature = QgsFeature() #Create a new feature newgeometry = QgsLineString([points[f['ID']], points[f['Dest']]]) #Create a line geometry using the dictionary of ids and points newfeature.setGeometry(newgeometry) provider.addFeature(newfeature)

QgsProject.instance().addMapLayer(newlayer)

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161