Uma abordagem básica seria:
- crie um gravador de vetor para um novo shapefile (consulte Livro de receitas , seção "Escrevendo camadas de vetor", segundo exemplo) como (multi) linha / polígono ou o que for necessário (consulte enumerações )
- carregue sua camada de pontos de
iface.legendInterface().layers()
- percorra seus pontos e adicione o recurso de geometria em sua nova forma
€: apenas por diversão:
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 .. "