I need to get the first and the last vertex from a multilinestring layer from PostGIS and create a memory point layer with them. The code below works for some line layers and doesn't for others in QGIS 3.6 and I can't figure out why. When I run it in QGIS 3.4 it simply doesn't work and don't get any error message.
line_lyr = qgis.utils.iface.activeLayer()
feat = QgsFeature()
point_layer = QgsVectorLayer("Point?crs=epsg:31984", "point_layer", "memory")
pr = point_layer.dataProvider()
for feature in line_lyr.getFeatures():
geom = feature.geometry().asMultiPolyline()
for line in geom:
start_point = QgsPointXY(geom[0][0])
end_point = QgsPointXY(geom[-1][-1])
feat.setGeometry(QgsGeometry.fromPointXY(start_point))
pr.addFeatures([feat])
feat.setGeometry(QgsGeometry.fromPointXY(end_point))
pr.addFeatures([feat])
QgsProject.instance().addMapLayer(point_layer)
I had missed something in the code? How can I improve this code for PyQGIS3?
if feature.geometry() is not None:directly after the linefor feature in line_lyr.getFeatures():and indent accordingly. This should ignore null geometries. – Joseph Jun 28 '19 at 11:42