Mã này sẽ hoạt động trên bản dựng dev mới nhất của QGIS.
from qgis.utils import iface
from qgis.core import *
from PyQt4.QtCore import QVariant
import random
def createRandomPoints(count):
# Create a new memory layer to store the points.
vl = QgsVectorLayer("Point", "distance nodes", "memory")
pr = vl.dataProvider()
pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
layer = iface.mapCanvas().currentLayer()
# For each selected object
for feature in layer.selectedFeatures():
geom = feature.geometry()
length = geom.length()
feats = []
# Loop until we reach the needed count of points.
for i in xrange(0,count):
# Get the random distance along the line.
distance = random.uniform(0, length)
# Work out the location of the point at that distance.
point = geom.interpolate(distance)
# Create the new feature.
fet = QgsFeature()
fet.setAttributeMap( { 0 : distance } )
fet.setGeometry(point)
feats.append(fet)
pr.addFeatures(feats)
vl.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(vl)
Tôi biết bạn nói rằng bạn không quen thuộc với mã Python nhưng bạn có thể chạy nó khá dễ dàng. Sao chép mã ở trên vào một tệp (tên của tôi được gọi locate.py
) và đặt mã đó vào ~/.qgis/python
nếu bạn ở trên Windows 7 sẽ ở trong C:\Users\{your user name}\.qgis\python\
hoặc trên Windows XPC:\Documents and Settings\{your user name}\.qgis\python\
Khi tệp nằm trong thư mục python, hãy mở QGIS và chọn một số đối tượng dòng.

Sau đó mở bàn điều khiển Python và chạy đoạn mã sau:
import locate.py
locate.createRandomPoints(10)

Kết quả sẽ trông giống như thế này

Nếu bạn muốn chạy lại nó, chỉ cần chọn thêm một số dòng và chạy lại locate.createRandomPoints(10)
trong bảng điều khiển Python.
Lưu ý: location.createRandomPoints (10) 10 ở đây là số điểm cần tạo trên mỗi dòng