By KaRolthas


2019-02-10 14:22:24 8 Comments

I have a point vector layer with angle field. How can I create line segments at these points with certain length and angle?

I did this with style options but I need this physically as lines vector layer: enter image description here

2 comments

@Vadym 2019-02-10 17:27:19

I put an example of solving the same task with a pyqgis (3.2) standalone application. Below the python code

from qgis.core import QgsPointXY, QgsApplication, QgsVectorLayer, QgsFeature, QgsGeometry
from PyQt5.QtWidgets import QApplication
import sys
import math

def main():
    print('Start program')

    qgis_prefix_path = 'C:\\OSGeo4W64\\apps\\qgis'
    app = QApplication(sys.argv)
    QgsApplication.setPrefixPath(qgis_prefix_path, True)

    QgsApplication.initQgis()

    point_path = 'd:/Users/Bogomolov/Qgis/Test_prj/point.shp'
    line_path = 'd:/Users/Bogomolov/Qgis/Test_prj/lines.shp'
    point_layer = QgsVectorLayer(point_path, "pointlayer", "ogr")
    layer = QgsVectorLayer(line_path, "linelayer", "ogr")


    for feature in point_layer.getFeatures():
        geom: QgsGeometry = feature.geometry()
        pnt: QgsPointXY = geom.asPoint()
        length = feature['distance']
        bearing = feature['bearing']
        id = feature['id']
        print('id=', id)
        pnt0 = direct_geodetic_task(pnt, length / 2, bearing + 180)
        pnt1 = direct_geodetic_task(pnt, length / 2, bearing)
        points = []
        points.append(pnt0)
        points.append(pnt1)
        fields = layer.dataProvider().fields()
        feature = QgsFeature()
        feature.setGeometry(QgsGeometry.fromPolylineXY(points))
        feature.setFields(fields)
        feature.setAttribute('id', id)
        layer.dataProvider().addFeature(feature)

    # layer.commitChanges()

    QgsApplication.exitQgis()

def direct_geodetic_task(pnt, dist, bear):
    if bear > 360.0:
        bear = bear - 360
    if bear < 0:
        bear = 360 + bear

    deg = bear * math.pi / 180

    dx = dist * math.sin(deg)
    dy = dist * math.cos(deg)
    x = pnt.x() + dx
    y = pnt.y() + dy

    return QgsPointXY(x, y)

if __name__ == '__main__':
    main()

The result is the same enter image description here

@Kazuhito 2019-02-10 15:52:36

One possible tool is Geometry by expression in the Processing Toolbox > Vector geometry.

enter image description here

A Geometry expression to create lines (length= 100 m) is as below:

make_line(project($geometry, 50, radians("angle")), project($geometry, 50, radians("angle"+180)))
  • project($geometry, 50, radians("angle")) part creates a new point by moving your points to "angle" direction by 50 meters.
  • project($geometry, 50, radians("angle"+180)) creates another point to the oposite direction.
  • make_line() ties the above two points, so the total line length is 100 meters.
  • project() function assumes that your "angle" is measured clockwise from north, so this expression may require edits depending on how your "angle" field is made.

enter image description here

NB. Do not forget saving created Modified geometry layer as a new dataset, otherwise it will be lost when you finish QGIS session.

@KaRolthas 2019-02-10 15:58:40

Thank You! Thats exactly what I needed.

@Kazuhito 2019-02-11 11:35:29

@KaRolthas Thanks! I am glad it worked out.

Related Questions

Sponsored Content

0 Answered Questions

3 Answered Questions

3 Answered Questions

[SOLVED] Create mid point from line layer

1 Answered Questions

[SOLVED] Create point and lines in QGIS 2.18?

0 Answered Questions

Create a perpendicular line of points to another line

0 Answered Questions

Merge line segments by length (in QGIS)

2 Answered Questions

[SOLVED] How to generate line segments between all points

  • 2015-04-01 06:26:29
  • Nick_W
  • 1629 View
  • 2 Score
  • 2 Answer
  • Tags:   qgis

1 Answered Questions

[SOLVED] PostGIS, create line from points

2 Answered Questions

[SOLVED] How to draw points around a circle in QGIS?

  • 2014-11-11 13:56:55
  • Matteo Mandrile
  • 949 View
  • 4 Score
  • 2 Answer
  • Tags:   qgis point

0 Answered Questions

Extract coordinates from polyline in QGIS

  • 2014-04-04 05:37:42
  • Toke
  • 78 View
  • 1 Score
  • 0 Answer
  • Tags:   qgis line

Sponsored Content