在Python控制台中:
1)创建一个内存层类(完整类(无属性),位于crea_mem_layer.py)
class Create_vlayer(object):
'''creation of a virtual layer'''
def __init__(self,nom,type):
self.type=type
self.name = nom
self.layer = QgsVectorLayer(self.type, self.name , "memory")
self.pr =self.layer.dataProvider()
def create_point(self,geometry):
# add point to the layer
self.seg = QgsFeature()
self.seg.setGeometry(QgsGeometry.fromPoint(geometry))
self.pr.addFeatures([self.seg])
self.layer.updateExtents()
@property
def display_layer(self):
#end of layer and display layer
QgsMapLayerRegistry.instance().addMapLayers([self.couche])
2)向量代数函数(来自algèbre_vect_PyQGIS.py)
import math
def mag(point):
# magnitude of a vector
return math.sqrt(point.x()**2 + point.y()**2)
def diff(point2, point1):
# substraction betwen two vector
return QgsPoint(point2.x()-point1.x(), point2.y() - point1.y())
def length(point1,point2):
# with PyQGIS: sqrDist
return math.sqrt(point1.sqrDist(point2))
3)方向余弦
def dircos(point):
cosa = point.x() / mag(point)
cosb = point.y()/ mag(point)
return cosa,cosb
4)处理线或线段
def pairs(list):
# list pairs iteration
for i in range(1, len(list)):
yield list[i-1], list[i]
layer = qgis.utils.iface.activeLayer()
# interval between points
interval = 5 m
# create virtual layer
gen_pt = Create_vlayer("mid5", "Point")
for elem in layer():
line = elem.geometry()
for seg_start, seg_end in pairs(line.asPolyline()):
line_start = QgsPoint(seg_start)
line_end = QgsPoint(seg_end)
# mid point = vector coordinates [x2-x1,y2-y1]
pointm =diff(line_end, line_start)
# direction cosines of the segment
cosa,cosb = dircos(pointm)
# length of the segment
lg = length(line_end, line_start)
# generate and add points to the virtual layer
for i in range(interval,lg,interval):
gen_pt.create_point(QgsPoint(line_start.x() + (i * cosa), line_start.y() + (i*cosb)))
# display layer
gen_pt.display_layer
结果
折线,Shapely或PyQGIS2等距点,方向余弦
然后只要调整间隔