Felt like adding some details to artwork's answer (read it out first):
Your script would probbably work even with QgsVectorLayer in your global statement, however you should not use global at all. In this case, the right way would be to pass all these variables as arguments to your function, have a look:
def activatePlugin (self, limitAer, limitSou, limitOra, limitENE, limitCha, layer):
layer = QgsVectorLayer('C:/Users/fse/Desktop/54_028_380_EXE_CCE_SID_CIR_001_B2/SHAPE/couche_test.shp', 'couche_test', 'ogr')
And pass the variables when calling the function, so instead of
my_plugin.activatePlugin()
call it like
my_plugin.activatePlugin(limitAer, limitSou, limitOra, limitENE, limitCha, layer)
Keep in mind that this is not crucial for your code to work.
About the path string to your shapefile:
- It can not have spaces
- It should use forward slashes "/", but some versions of qgis will accept scaped backward slashes "\\"
- It must be an absolute path
Here are some more info in loading a layer and the QgsVectorLayer documentation
Also, you did not specify how you are running this code, so you may need to add the import statements to all classes you are using and, if you are running this script as a standalone app, you will have to start a QgsApplication (If you do not do this, your QgsVectorLayer will not be a valid layer).
EDIT(considering comment from op):
To create a shapefile, you must first create a layer.
layer = QgsVectorLayer('Point?crs=epsg:4326&field=example:integer', 'couche_test', 'memory')
layer
now holds a QgsVectorLayer that can hold QgsFeature
with Point geometries, has EPSG:4326 as it coordinate reference system and has a field named example with the type integer.It is possible to add features to this layer using the function layer.addFeatures()
, Google it up if needed. To save this layer, use QgsVectorFileWriter
. Simple example:
QgsVectorFileWriter.writeAsVectorFormat(layer, "C:/Users/fse/Desktop/54_028_380_EXE_CCE_SID_CIR_001_B2/SHAPE/couche_test.shp", "utf-8", None,"ESRI Shapefile")