1

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
2
  • "ID_Rota" needs to be in quotes, like that. See if that helps or if there are other errors too.
    – AlecZ
    Feb 8 '18 at 16:11
  • @alecz by taking argument with whotes I get "TypeError: QgsFeature.attributes(): too many arguments" Feb 8 '18 at 16:32
2

Maybe something like the following which creates another ID_Rota field in the point output file and copies the attribute values from the line feature:

from PyQt4.QtCore import QVariant

line_layer = qgis.utils.iface.activeLayer()
feat = QgsFeature()

point_layer = QgsVectorLayer("Point?crs=epsg:4326", "point_layer", "memory")
pr = point_layer.dataProvider()
field = QgsField("ID_Rota", QVariant.Int) 
pr.addAttributes([field])
point_layer.updateFields()

for feature in line_layer.getFeatures():
    geom = feature.geometry().asPolyline()
    start_point = QgsPoint(geom[0])
    end_point = QgsPoint(geom[-1])
    feat.setGeometry(QgsGeometry.fromPoint(end_point))
    feat.setAttributes([feature["ID_Rota"]])
    pr.addFeatures([feat])

QgsMapLayerRegistry.instance().addMapLayer(point_layer)
2
  • 1
    perfect! can we call additional fields in the same two lines field = QgsField("ID_Rota", QVariant.Int); field = QgsField("ID_Rota", QVariant.Int) pr.addAttributes([field]) or add as many lines as new fields? Feb 8 '18 at 16:45
  • 1
    @PauloCardoso - Glad it worked! Yes, addAttributes() requires a list of QgsFields. So we could just use pr.addAttributes([QgsField("field1", QVariant.Int), QgsField("field2", QVariant.Int)]) in a single line. Then we can transfer attributes using feat.setAttributes([feature["field1"], feature["field2"]]).
    – Joseph
    Feb 9 '18 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.