[Qgis-developer] Digitize Line starting at a point vertex with a defined angle and distance

gene martin.laloux at gmail.com
Sat Apr 6 03:01:50 PDT 2013


I am a geologist and I perform frequently this type of calculation with a
Python script in the console or with a Python script without QGIS (see
similar solution with Fiona module in  How do I find vector line bearing in
QGIS or GRASS?
<http://gis.stackexchange.com/questions/55449/how-do-i-find-vector-line-bearing-in-qgis-or-grass/55480#55480> 
)

The solution is geometrically simple (here in 2D in a projected coordinate
system in meters)

      from PyQt4.QtCore import *
      from math import cos, sin, radians

      def ptor2ptfin(p, angle, dist):
           ''' position of the point located at a distance dist and azimuth
angle from the point p '''
           dist_x, dist_y = (dist * sin(radians(angle)),dist *
cos(radians(angle)))
           xfinal, yfinal = (p.x() + dist_x, p.y() + dist_y)
           return QgsPoint(xfinal,yfinal)

     # for QGIS 1.8
     distance = 200
     angle = 20
     line_start = QgsPoint(122.989235,13679.083853)
     line_end = ptor2ptfin(line_start, angle, distance)
     # memory layer
     v_layer = QgsVectorLayer("LineString", "azimuth_lines", "memory")
     pr = v_layer.dataProvider()
     pr.addAttributes( [ QgsField("azimuth", QVariant.Int),
QgsField("distance", QVariant.Double)])
     seg = QgsFeature()
     seg.setGeometry(QgsGeometry.fromPolyline([line_start, line_end]))
     seg.addAttribute(0, QVariant(int(angle)))
     seg.addAttribute(1, QVariant(distance))
     pr.addFeatures( [ seg ] )
     v_layer.updateExtents()
     v_layer.updateFieldMap()
     QgsMapLayerRegistry.instance().addMapLayers([v_layer])

And and you just need to change the function ptor2ptfin to fit your
requirements (haversine, 3D distance, miles, etc.)




--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Digitize-Line-starting-at-a-point-vertex-with-a-defined-angle-and-distance-tp5044921p5044949.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list