1

In the user interface of QGIS I have the option to draw lines with the "Advances Digitizing tool", where I can enter coordinates of a point to set the first vertex. To set the second vertex I can now enter the angle and distance and the "Advances Digitizing Tool" sets the point automatically with the fitting coordinates.

Is it possible to do the same thing using PyQGIS? Drawing a line not from a list of points, but from one point, angle and distance? (I'm a total beginner using PyQGIS.)

1
  • Cartesian trigonometry would cover this easily for an projected coordinate reference system. For a geographic coordinate system, I'd recommend using a geodesic library which implements the Forward (aka Direct) problem of geodesy.
    – Vince
    Commented Aug 12, 2022 at 13:32

1 Answer 1

4

This function will create a new point based on: distance (meters), azimuth (degrees) and the starting point start_QgsPoint which is qgis.core.QgsPoint class. Then you can connect the coordinates into a line. This only works with projected coordinate systems.

def new_point_by_distance_and_azimuth(start_QgsPoint, distance, azimuth):
    start_x, start_y = start_QgsPoint.x(), start_QgsPoint.y()
    azimuth_radians = math.radians(azimuth)
    new_x = start_x + distance * math.cos(azimuth_radians)
    new_y = start_y + distance * math.sin(azimuth_radians)
    return qgis.core.QgsPointXY(new_x, new_y)
6
  • 2
    You should add a caveat: This only works with projected coordinate systems
    – Vince
    Commented Aug 12, 2022 at 13:34
  • Please, I have same problem, but I can't understadn how to do. If I have start point lon/lat in DEGREES (wgs84) and distance in METERS, how to solve addition?
    – Marco
    Commented Mar 7, 2023 at 11:27
  • @Marco First you have to reproject the data. Commented Mar 7, 2023 at 11:54
  • tank you @ComradeChe, should you give me a little suggestion how to do that in pytthon? I'm quite newby and ithis is my problem (I can't find any precise istuction)
    – Marco
    Commented Mar 7, 2023 at 13:37
  • @Marco Try this: gis.stackexchange.com/questions/316002/… Commented Mar 7, 2023 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.