1

I am trying to run a script from the QGis Python console (similar to: http://spatialgalaxy.net/2012/01/27/qgis-running-scripts-in-the-python-console/)

The line feat=QgsFeature() operates fine from the Python console, however within the script it throws up the following error:

Traceback (most recent call last): File "", line 1, in File "getLineFeaturesAndPoints.py", line 47, in write_polylines feat = QgsFeature() NameError: global name 'QgsFeature' is not defined

The code is saved in getLineFeaturesAndPoints.py file:

class WritePolylines:
def __init__(self, iface):
    """Initialize using the qgis.utils.iface 
    object passed from the console.
    """
    self.iface = iface

def write_polylines(self, file_dir):

    # Get the active layer
    # qgis.utils.iface.mapCanvas()
    canvas = self.iface.mapCanvas()
    cLayer = canvas.currentLayer()
    #cLayer.name()

    # Get reference to the data provider
    provider = cLayer.dataProvider()

    # get all features (but no geometry)
    provider.select()

    # get attributes
    selectList = []
    columns = provider.fields()
    for key,value in columns.items():
        column = str(value.name())
        selectList.append(provider.fieldNameIndex(column))

    provider.select(selectList)
    features_file = open(path.join(file_dir, "features.txt"), "w")
    points_file = open(path.join(file_dir, "featurepoints.txt"), "w")
    writeFeature = csv.writer(features_file, quoting=csv.QUOTE_ALL)
    writePoint = csv.writer(points_file, quoting=csv.QUOTE_ALL)        

    # get one feature id and geometry
    feat = QgsFeature()

Similar to the referenced HTML file, I invoke it as follows:

from getLineFeaturesAndPoints import WritePolylines 
pl = WritePolylines(qgis.utils.iface)
myPath = 'C:\\Users\\Documents'
pl.write_polylines(myPath)
3

You are missing:

from qgis.core import QgsFeature

at the top of your script

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.