1

I have been working with classes, and I have fixed several mistakes related to indentation, but in this case I have been checking all the possible errors. It might be a concept error. If someone can tell me what is my mistake, it will be very helpful.

From this code, I get the error "NameError": global name 'vector' is not defined

import qgis
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

class Varias_Funciones:

         vector = QgsVectorLayer("ROUTE","Municipios","ogr")

     #LA CLASE TENDRA LA CAPACIDAD DE ENTRAR EN EL INTERFAZ DE QGIS
         def __init__(self, iface):
             self.iface = iface

     #CARGA DE LA CAPA DE MUNICIPIOS (CAPA VECTORIAL)
         def capa_vectorial(self):
             QgsMapLayerRegistry.instance().addMapLayer(vector)

     #CARGA DE LA CAPA DE RASTER MTN50
         def capa_raster(self):
             raster = QgsRasterLayer("ROUTE", "MTN50")
             QgsMapLayerRegistry.instance().addMapLayers([raster])

     #CARGA DE SISTEMAS DE COORDEANDAS
         def coordenadas(self):
             crs = QgsCoordinateReferenceSystem(4258, QgsCoordinateReferenceSystem.EpsgCrsId)
             vector.setCrs(crs)

     #ABRIR LAS PROPIEDADES
         def abrir_propiedades(self):
             self.iface.showLayerProperties(vector)


comprobar_funciones = Varias_Funciones(qgis.utils.iface)
comprobar_funciones.capa_vectorial()
comprobar_funciones.capa_raster()
comprobar_funciones.coordenadas()
comprobar_funciones.abrir_propiedades()
3
  • Is this a standalone script or for the command line in qgis? Standalone it won´t work this way. – Matte Apr 8 '16 at 9:55
  • well, I was writing in Python IDLE, then open it in Text Editor in QGIS – user64823 Apr 8 '16 at 10:01
  • try using self.vector instead of vector in coordenadas() ...? – Steven Kay Apr 8 '16 at 11:50
0

It is a basic problem of creation of a Class.

vector = QgsVectorLayer("ROUTE","Municipios","ogr") is a class variable defined inside the class

You can access it with:

class myclass:
    vector = QgsVectorLayer("ROUTE","Municipios","ogr")
print myclass.vector
<qgis._core.QgsVectorLayer object at 0x1154d55a8>

If I use part of your class

class Varias_Funciones:
     vector = QgsVectorLayer("ROUTE","Municipios","ogr")
     def capa_vectorial(vector):
         return QgsMapLayerRegistry.instance().addMapLayer(vector)

 layer = Varias_Funciones
 layer.capa_vectorial()
 ....error...

You should use theoretically

QgsMapLayerRegistry.instance().addMapLayer(Varias_Funciones.vector)

But it is not correct and that is why a class has constructor or initialization method that Python calls when you create a new instance of this class.

class Varias_Funciones(object):
    def __init__(self):
       self.vector = QgsVectorLayer("ROUTE","Municipios","ogr")
    def capa_vectorial(self):
      return QgsMapLayerRegistry.instance().addMapLayer(self.vector)

Then

 layer = Varias_Funciones()
 layer.capa_vectorial()
 # or directly
 layer = Varias_Funciones().capa_vectorial()

work

1
  • Thank you very much for this explanation. I will test it during this afternoon. I completely understood it, actually, I got "something" like your code because I kept working on it... well, I wasn't in a bad way at all. Thank you again – user64823 Apr 8 '16 at 15:17

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.