2

I want to create points along a line (works!) and then additionally move the points by a certain offset (perpendicular to the line) with pyqgis 3. See this sketch for further illustration:

points along line with offset

My current python script does following: It basically interpolates the position at the certain length of the line and then it constructs the position of the offset point by triangulation. The script works so far, but the drawn point has a slight deviation in it's x coordinate (see figure 2).

Fig 2: Slight deviation of created points

My code so far:

def createPointsAtOffset(distance, offset, geom):
length = geom.length()
currentdistance = distance
feats = []

point = geom.interpolate(currentdistance)
fet = QgsFeature()
fet.setAttributes([currentdistance, offset])

if offset is not 0:
    xy = point.asPoint()
    alpha = (math.degrees(geom.interpolateAngle(currentdistance)) - 90) # return in degree
    # create delta x and y via triangulating
    delY = math.cos(math.radians(alpha)) * offset
    delX = math.sin(math.radians(alpha))* offset #12
    delT = math.sqrt(offset*offset - delY*delY)

    pointX = xy[0] + delX
    pointY = xy[1] + delY
    fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(pointX,pointY)))

else:
    fet.setGeometry(point)

feats.append(fet)

return feats

def pointsAlongLineOffset(distance, offset):
# Create a new memory layer and add a distance attribute
vl = QgsVectorLayer("Point", "distance offset nodes", "memory")
pr = vl.dataProvider()
pr.addAttributes( [ QgsField("distance", QVariant.Double), QgsField("offset", QVariant.Double) ] )
layer = iface.mapCanvas().currentLayer()
# Loop though all the features
for feature in layer.getFeatures():
    geom = feature.geometry()
    # hier muss dann eine schleife durch alles distance offset paare durchgehen
    #features = createPointsAt(distance, geom)
    features = createPointsAtOffset(distance, offset, geom)
    pr.addFeatures(features)
    vl.updateExtents()

QgsProject.instance().addMapLayer(vl)

# example for the image above:
pointsAlongLineOffset(1.2,2.5)

I think my error is that I use a wrong angle in the triangulation part. In the example the line is tilted about 6° from east but when I insert about 12° into the calculation of delX it works? Am I missing some kind of deviation from north or is my approach completely wrong?

3
  • Eyeball-wise from your example, it looks like your routine might be starting from an angle of zero. Check to see if geom.interpolateAngle(currentdistance) is what you expect. What is the angle of a point anyway?
    – Dave X
    Nov 15, 2018 at 16:38
  • geom is the selected line layer. geom.interpolateAngle(currentdistance) is the angle of the line layer, measured from north. For the triangulation I need the angle from the horizontal (W-E) to the line. That's why I substract 90° on the output.
    – Valentin
    Nov 15, 2018 at 16:48
  • I understand the reason for -90, but if the symptom is that delX = 0, it might be because alpha = 90 (or -90) which in turn could be because geom.interpolateAngle(currentdistance) is something like 0.
    – Dave X
    Nov 15, 2018 at 16:58

0

Your Answer

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

Browse other questions tagged or ask your own question.