I try to use the build-in Python-console of QGIS to create voronoi skeleton lines from a set of polygons. Therefore I iterate over the features of my input layer (the polygons) and then I use the runalg command for v.voronoi. However, I reckon I mess things up by defining the output-file, because I keep getting the same error-message:

>>> mapLines(layer, coord_ext)
Layer failed to load!
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "c:/users/.../tmp1qh0ii.py", line 43, in mapLines
    cent_feat = centroid_layer_obj.getFeatures()
AttributeError: 'NoneType' object has no attribute 'getFeatures'

I use the following code:

import sys
from PyQt4.QtCore import *
import processing

#Retrieve a layer
layer = processing.getObject("voetpaden_test_gebied")
#Retrieve layer extent
ext = layer.extent()
coord_ext = "%s, %s, %s, %s" %(ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
#output folder
output_folder = r"F:\My Documents\test"

#create layer for centroid lines
line_layer = QgsVectorLayer('LineString?crs=epsg:28992', 'line', 'memory')
line_prov = line_layer.dataProvider()

#This function should create a centroid line per feature of layer
def mapLines(one_layer, coord_ext):
    for f in one_layer.getFeatures():
        #create a temporary polygon layer for each feature
        poly_layer = QgsVectorLayer('Polygon?crs=epsg:28992', 'polygon', 'memory')
        poly_prov = poly_layer.dataProvider()
        #place temporary layer on map canvas

        #create a centroid line for the temporary layer
        centroid_line = processing.runalg('grass7:v.voronoi.skeleton', poly_layer, -1.0, coord_ext, -1.0, 0.0, 2, output_folder + "/temp.shp")
        #open the centroid line layer
        centroid_layer = iface.addVectorLayer('temp.shp', 'centroid_temp', 'ogr')
        if not centroid_layer:
            print("Layer failed to load!")

        #get the centroid layer    
        centroid_layer_obj = processing.getObject("centroid_temp")
        #get the feature of the centroid layer
        cent_feat = centroid_layer_obj.getFeatures()
        #remove the temporary layer
    #Add the new layer with centroid lines to the map canvas

Unfortunately, I do not see any saved object in the folder I specified (F:\My Documents\test). I figured that is why I get the NoneType AttributeError.

  • Can you include the statement that calls the mapLines() function? – artwork21 Sep 18 '18 at 11:56
  • @artwork21 It's on the first line of the error-message part. – Tristian Sep 18 '18 at 13:06
  • I see that but I'm not sure where in your code you are calling it. – artwork21 Sep 18 '18 at 13:14
  • @artwork21 I first run the code from the editor. After that I call mapLines(layer, coord_ext) in the console (both in QGIS). – Tristian Sep 18 '18 at 13:29
  • I think you need to run the runalg command in python, was this your attempt at it layer = processing.getObject("voetpaden_test_gebied") ? See this qa which uses that command in python, gis.stackexchange.com/questions/219854/… – artwork21 Sep 18 '18 at 14:11

Your Answer

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

Browse other questions tagged or ask your own question.