0

Trying to convert a python console script to a standalone version of it. By following the general guidelines from QGIS-documentary https://docs.qgis.org/2.14/en/docs/pyqgis_developer_cookbook/intro.html

But unfortunately, I run in a "simple" problem, but unable to solve it... This is my code:

import sys
sys.path.append(r"/Applications/QGIS.app/Contents/Resources/python")
sys.path.append(r"/Applications/QGIS.app/Contents/Resources/python/plugins")    

from qgis.core import *    

import json, requests
import processing       

# supply path to qgis install location
QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", False)
# create a reference to the QgsApplication, setting the # second argument to False disables the GUI
qgs = QgsApplication([], False)
# load providers
qgs.initQgis()    

geometry_decode = [(0.0,0.0),(1.2,1.3)]    

geometry = [QgsPoint(point[1],point[0]) for point in geometry_decode]    

line = QgsGeometry.fromPolyline(geometry)
route = QgsVectorLayer("LineString?crs=epsg:4326&index=yes","route","memory")
pr = route.dataProvider()
print(route)
route.startEditing()    

feature = QgsFeature()
feature.setGeometry(line)    

pr.addFeatures([feature])    

# Write your code here to load some layers, use processing algorithms, etc.
# When your script is complete, call exitQgis() to remove the provider and # layer registries from memory
qgs.exitQgis()

This is the error-message:

Error in sys.excepthook:
Traceback (most recent call last):
  File "/Applications/QGIS.app/Contents/Resources/python/qgis/utils.py", line 196, in qgis_excepthook
    showException(type, value, tb, None, messagebar=True)
  File "/Applications/QGIS.app/Contents/Resources/python/qgis/utils.py", line 107, in showException
    open_stack_dialog(type, value, tb, msg)
  File "/Applications/QGIS.app/Contents/Resources/python/qgis/utils.py", line 142, in open_stack_dialog
    iface.messageBar().popWidget()
AttributeError: 'NoneType' object has no attribute 'messageBar'    

Original exception was:
Traceback (most recent call last):
  File "test_standalone_copy.py", line 33, in <module>
    pr.addFeatures([feature])
AttributeError: 'NoneType' object has no attribute 'addFeatures'
6
  • Please include the whole error message – underdark Jan 28 '18 at 12:35
  • Try using route.addFeatures([feature]) – Ben W Jan 28 '18 at 12:40
  • added error message – CanadaRunner Jan 28 '18 at 12:42
  • route.addFeatures([feature]) fixed problem, but why does it work in the qgis console? – CanadaRunner Jan 28 '18 at 12:52
  • I'm not sure exactly what code you ran in the console, but the addFeatures() method belongs to the QgsVectorLayer class, which you instantiated and assigned to the variable route. – Ben W Jan 28 '18 at 13:46
2

thank to Ben-W's help, The function .addFeatures() is related to the VectorLayer and not dataProvider

So this is the right way to add the feature

route.addFeatures([feature])  

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.