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.)

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
alice.2000
  • 13
  • 3
  • 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 Aug 12 '22 at 13:32

1 Answers1

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)
Comrade Che
  • 7,091
  • 27
  • 58
  • 2
    You should add a caveat: This only works with projected coordinate systems – Vince Aug 12 '22 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 Mar 07 '23 at 11:27
  • @Marco First you have to reproject the data. – Comrade Che Mar 07 '23 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 Mar 07 '23 at 13:37
  • @Marco Try this: https://gis.stackexchange.com/questions/316002/reprojecting-layer-using-pyqgis – Comrade Che Mar 07 '23 at 15:32
  • tank you, I resolved by an intuition (like your). src_crs = QgsCoordinateReferenceSystem(4326) # wgs84 dest_crs = QgsCoordinateReferenceSystem(3857) # wgs84/pseudoMercator # set trasform (degress <-> meters) to recalc points by distance (in meters) self.to_m_form = QgsCoordinateTransform(src_crs, dest_crs, QgsProject().instance()) self.to_d_form = QgsCoordinateTransform(dest_crs, src_crs, QgsProject().instance()) so I transform degrees-coords to meters-coord , than I add distance in meters, than reconvert to degrees – Marco Mar 07 '23 at 16:24