2

I have a road network network (polyline shape layer) with multiple line lsegments. Each line segment has different line length as shown in image 1.

  • I am trying to generate points along lines based on specific distance. For instance, If the length of lines is less than 80 meters generate one point along line else two points for higher lengths.
  • I tried to generate points along lines by using PyQGIS as the script is given below. The problem is, it generate only midpoints of lines by dividing the line.

I searched some of the existing answers: answer 1, answer 2, answer 3. But the problem is these solutions are only generating random points along line without considering length of line.

layer = iface.activeLayer()

temp = QgsVectorLayer("Point?crs=epsg:4283", "result", "memory")
temp.startEditing()

attrs = layer.dataProvider().fields().toList()
temp_prov = temp.dataProvider()
temp_prov.addAttributes(attrs)
temp.updateFields()

for elem in layer.getFeatures():
    feat = QgsFeature()
    geom = elem.geometry().interpolate(elem.geometry().length()/2)
    feat.setGeometry(geom)
    feat.setAttributes(elem.attributes())
    temp.addFeatures([feat])
    temp.updateExtents()

temp.commitChanges()
QgsProject.instance().addMapLayer(temp)

enter image description here

1 Answer 1

1

We can check the length of line from the 'length' attribute and then add an if statement to capture the lines where the length is <80 and create a point at 1/2 the length.
Otherwise, else, generate a point at 1/3 and at 2/3 of the length of the line.

layer = iface.activeLayer()

temp = QgsVectorLayer("Point?crs=epsg:4283", "result", "memory")
temp.startEditing()

attrs = layer.dataProvider().fields().toList()
temp_prov = temp.dataProvider()
temp_prov.addAttributes(attrs)
temp.updateFields()


for elem in layer.getFeatures():
    feat = QgsFeature()
    #read length field of feature
    len = elem["length"]
    #print (len)
    #if length is <80
    if len < 80:
        geom = elem.geometry().interpolate(elem.geometry().length()/2)
        feat.setGeometry(geom)
        feat.setAttributes(elem.attributes())
        temp.addFeatures([feat])
        temp.updateExtents()
    else:    
        #get one 1/3 of line length
        geom = elem.geometry().interpolate(elem.geometry().length()*(1/3)) 
        feat.setGeometry(geom)
        feat.setAttributes(elem.attributes())
        temp.addFeatures([feat])
        # get one 2/3 of line length
        geom = elem.geometry().interpolate(elem.geometry().length()*(2/3))
        feat.setGeometry(geom)
        feat.setAttributes(elem.attributes())
        temp.addFeatures([feat])        
        temp.updateExtents()

temp.commitChanges()
QgsProject.instance().addMapLayer(temp)
2
  • Following the answer. How can we create after 50 meter in a line? For example, if the line length is 150, then three points. Also, if the line length is between 50 and 100, then 2 points. If the line length is between 40 and 200 , then 4 points along line
    – Case Msee
    Oct 6, 2021 at 5:55
  • That would be a different question, I would suggest searching to see if that has already been asked and if not creating a new question for that.
    – Cushen
    Oct 8, 2021 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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