1

This is the error I am getting in QGIS Bonn 3.2.2:

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 3, in <module>
NameError: name 'QgsMapLayerRegistry' is not defined

This is the script i am trying to:

from qgis.PyQt.QtCore import QVariant

for layer in QgsMapLayerRegistry.instance().mapLayers().values():   
    if layer.wkbType() == QGis.WKBPolygon:
        with edit(layer):
            layer.dataProvider().addAttributes( [ QgsField("Node_ID", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("Node_Type", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("T_1", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("R_1", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("S_1", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("QS_1", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("T_2", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("R_2", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("S_2", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("QS_2", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("T_3", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("R_3", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("S_3", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("QR_3", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("T_4", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("R_4", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("S_3", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("QR_4", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("Meridian", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("Disc_Mon", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("Dis_Lat", QVariant.Double) ] )
            layer.dataProvider().addAttributes( [ QgsField("Dis_Long", QVariant.Double) ] )
            layer.dataProvider().addAttributes( [ QgsField("Dis_Dist", QVariant.Int) ] )
            layer.dataProvider().addAttributes( [ QgsField("Dis_Azi", QVariant.Int) ] )
    else:
        pass
  • (take a minute and check out how to format code - e.g. {} button above the text window. I edited it.) – geozelot Aug 27 '18 at 20:44
  • 2
    QgsMapLayerRegistry (among others) has been replaced with QgsProject, see e.g. this post. the API changes are quite massive...make sure you check the linked changelog. – geozelot Aug 27 '18 at 20:49
  • so not sure what I need to do to make this work in the new environment? – lowsparked Sep 10 '18 at 13:48

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.