Pendekatan dasar adalah:
- buat vektor-penulis untuk shapefile baru (lihat Cookbook , Bagian "Menulis Layer Vektor", contoh ke-2) sebagai (multi) garis / poligon atau apa pun yang Anda butuhkan (lihat enum )
- muat layer titik Anda dari
iface.legendInterface().layers()
- beralih melalui poin Anda dan tambahkan fitur geometri di bentuk baru Anda
€: hanya untuk bersenang-senang:
from PyQt4.QtCore import *
# easy 2 config vars
myPath = "/media/ymirsson/MyData/GIS/tmp/"
myPts = "test_coords.shp"
myLines = "test_lines.shp"
sortField = "No"
myCRS = "25832"
# load the points layer
myPtsLayer = QgsVectorLayer(myPath + myPts, "My Points", "ogr")
# field def. 4 lines-layer
fields = QgsFields()
fields.append(QgsField("id", QVariant.Int))
fields.append(QgsField("name", QVariant.String))
# create writer
writer = QgsVectorFileWriter(myPath + myLines, "utf-8", fields, QGis.WKBLineString, QgsCoordinateReferenceSystem(int(myCRS), QgsCoordinateReferenceSystem.EpsgCrsId), "ESRI Shapefile")
if writer.hasError() == QgsVectorFileWriter.NoError:
# featureobject to list
features = myPtsLayer.getFeatures()
Points=([])
for feature in features:
geom = feature.geometry().asPoint()
sortID = feature.attributes()[myPtsLayer.fieldNameIndex(sortField)]
Points.append([sortID,geom])
Points.sort()
# create lines
for i in range(1,len(Points)):
Line = QgsFeature()
Line.setGeometry(QgsGeometry.fromPolyline([Points[i-1][1], Points[i][1]]))
Line.setAttributes([i,str(Points[i-1][0]) + " to " + str(Points[i][0])])
writer.addFeature(Line)
# don't forget the last one -.-
Line = QgsFeature()
Line.setGeometry(QgsGeometry.fromPolyline([Points[len(Points)-1][1], Points[0][1]]))
Line.setAttributes([len(Points),str(Points[len(Points)-1][0]) + " to " + str(Points[0][0])])
writer.addFeature(Line)
# flush 2 disk
del writer
# load both layers into qgis-project
QgsMapLayerRegistry.instance().addMapLayer(myPtsLayer)
QgsMapLayerRegistry.instance().addMapLayer(QgsVectorLayer(myPath + myLines, "My Lines", "ogr"))
print "Done .. "