I'd like to extend the solution proposed to Can vector layer get start point and end point of line using PyQGIS? and retrieve linestring attributes (columns) to the start and end points from the original object.
In the example below the Polyline object have a column [ID_Rota] that I'd like to copy to the resulting point table.
I have - so far unsuccessfully - tried to implement @joseph's recommendation and add a feat.setAttributes(feature.attributes())
with:
line_layer = qgis.utils.iface.activeLayer()
feat = QgsFeature()
point_layer = QgsVectorLayer("Point?crs=epsg:4326", "point_layer", "memory")
pr = point_layer.dataProvider()
for feature in line_layer.getFeatures():
geom = feature.geometry().asPolyline()
start_point = QgsPoint(geom[0])
end_point = QgsPoint(geom[-1])
feat.setGeometry(QgsGeometry.fromPoint(start_point))
pr.addFeatures([feat])
#feat.setGeometry(QgsGeometry.fromPoint(end_point))
#pr.addFeatures([feat])
id_rota = feat.setAttributes(feature.attributes(ID_Rota))
pr.addFeatures([feat])
QgsMapLayerRegistry.instance().addMapLayer(point_layer)
and the error
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "~/temp/tmph65dmx.py", line 15, in <module>
id_rota = feat.setAttributes(feature.attributes(ID_Rota))
NameError: name 'ID_Rota' is not defined