Un enfoque básico sería:
- cree un escritor de vectores para un nuevo archivo de formas (vea el Libro de cocina , Sección "Escribir capas de vectores", segundo ejemplo) como (multi) línea / polígono o lo que necesite (ver enumeraciones )
- cargar su capa de puntos desde
iface.legendInterface().layers()
- iterar a través de sus puntos y agregar la función de geometría en su nueva forma
€: solo por diversión:
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 .. "