from qgis.PyQt.QtCore import QVariant from qgis.core import (QgsFeature, QgsField, QgsGeometry, QgsPointXY, QgsVectorLayer, QgsProject) # Locate the edges layer edges_layer = QgsProject.instance().mapLayersByName('EDGES')[0] # Create a new temporary layer for the poles poles_layer = QgsVectorLayer('Point?crs=epsg:4326', 'POLES', 'memory') # Define the data provider pr = poles_layer.dataProvider() # Add a new field to the new layer pr.addAttributes([QgsField('id', QVariant.Int)]) poles_layer.updateFields() # Store coordinates of existing poles to avoid duplicates existing_poles = set() # Iterate over every feature of the EDGES layer for feature in edges_layer.getFeatures(): # Only consider edges of type 'Aerial' if feature['type'] == 'Aerial': # Get the geometry of the feature geom = feature.geometry() # Handle both 2D and 3D geometries if geom.isMultipart(): vertices = [v for part in geom.asMultiPolyline() for v in part] else: vertices = geom.asPolyline() for i, vertex in enumerate(vertices): # Do not create a new pole if one already exists at this location vertex_coordinates = (vertex[0], vertex[1]) if vertex_coordinates in existing_poles: continue # Create a new feature in the POLES layer for each vertex pole = QgsFeature() pole.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(*vertex_coordinates))) pole.setAttributes([i]) pr.addFeature(pole) # Remember this location so we don't create a duplicate pole existing_poles.add(vertex_coordinates) # Update the layer's extent when new features have been added poles_layer.updateExtents() # Add the new layer to the project QgsProject.instance().addMapLayer(poles_layer)