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)