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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 8, 2013 at 6:31

1 Answer 1

3

You are missing:

from qgis.core import QgsFeature

at the top of your script

answered May 8, 2013 at 8:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.