TransWikia.com

QGIS Python - 'NoneType' object has no attribute 'addFeatures'

Geographic Information Systems Asked by CanadaRunner on July 4, 2021

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'

One Answer

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])  

Answered by CanadaRunner on July 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP