1

I am begining with PyQGIS and QGIS in general. I wanted a way to create a layer via python, so i downloaded "Plugin Builder" and starded coding. Here's the code :

def activatePlugin (self):
    global limitAer,limitSou ,limitOra , limitENE, limitCha, layer, QgsVectorLayer

    layer = QgsVectorLayer('/Users/fse/Desktop/54_028_380_EXE_CCE_SID_CIR_001_B2/SHAPE/couche_test.shp', 'couche_test', 'ogr')

    QgsMapLayerRegistry.instance().addMapLayer(layer)

    if not layer.isValid():
        print "Layer was not loaded!"
    else:
        print "Layer was loaded successfully!"

I get a "Layer was not loaded" response and a print in the OGR console telling me that the source data is invalid.

I have tried all sorts of slashes, backslashes etc... It is basically always the same.

4
  • Have you tried adding an r on your path on line 4? Like: r'Users\fse\...
    – reevesii
    Commented Aug 1, 2018 at 15:40
  • Never seen an imported class declared global: QgsVectorLayer. May be that's wrong? Commented Aug 1, 2018 at 18:30
  • Reveesii : Yes, I did and it did not changed anything :/
    – Mathieu
    Commented Aug 2, 2018 at 7:16
  • Andreas Muller : I tried that in the end, desperately but it wasn't working before too...
    – Mathieu
    Commented Aug 2, 2018 at 7:17

2 Answers 2

3

Remove the QgsVectorLayer from the global variable statement and inlcude the directory letter (e.g "C:") in the path to the shapefile.

def activatePlugin (self):
    global 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')
1
  • Hi, thanks for the answer. I tried without QgsVectorLayer in the globals, and I also tried with "c:/", without it, with "r'/User/'. The only way I made it work was layer2 = QgsVectorLayer('Point?crs=epsg:4326', 'couche_test', 'memory')
    – Mathieu
    Commented Aug 3, 2018 at 13:55
1

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")
6
  • Hi, thanks for taking the time to answer ! I got rid of all the variables because I was not using them anyway. So I end up with def activatePlugin2 (self): layer2 = QgsVectorLayer('C:/Users/fse/Desktop/54_028_380_EXE_CCE_SID_CIR_001_B2/SHAPE/couche_test.shp', 'couche_test', 'memory') #layer2.setProviderEncoding('System') QgsMapLayerRegistry.instance().addMapLayer(layer2) if not layer2.isValid(): print "Layer was not loaded!" else: print "Layer was loaded successfully!"
    – Mathieu
    Commented Aug 3, 2018 at 14:02
  • And i still get the same error with ogr... But in the end, i think I don't have the whole ogr library, because this problem is not anywhere else on the internet, i must have messed up with my installation.
    – Mathieu
    Commented Aug 3, 2018 at 14:03
  • Pointing out, I belive the memory provider cannot be use with a shapefile, it can only be used with a memory data source (like what you did with "Point?.."). Use "ogr" for shapefiles. also, can you open the shp using the qgis interface?
    – xlDias
    Commented Aug 3, 2018 at 14:31
  • Whenever I use ogr, I get a source data error. And I am trying to create a shp, not opening one.
    – Mathieu
    Commented Aug 3, 2018 at 14:35
  • ohh, that explains it. So this is not the way you should do it. This code will only create a layer from a data source that already exists. Notice that a shapefile and a layer are two different things. I will update the answer with some more info
    – xlDias
    Commented Aug 3, 2018 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.