3

I am asking this question based on the result I received from this earlier post. The following script only works if I set geometryType() == 3 as that is what is being printed out in the Python console even though @nickves from the previous post mentioned that 0, 1 and 2 are for points, lines and polygons respectively.

Could someone point out my mistake please? Feel free to cross this post over to Stack Overflow, I only posted it here as the script is based on geometry data.

##Test=name
import os
import glob
from qgis.core import *
from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():
#   Set directory and search for all polygon .shp files
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"): 
        shapefile = QgsVectorLayer( os.chdir(path_dir + "Shapefiles\\"), fname, "ogr" )
#      print shapefile.geometryType()
        print shapefile.wkbType()

#   Clip .shp files with the polygon shapefile "Grid.shp" and save files to Result folder
#        if shapefile.wkbType() == 2:
#            polygon_output=processing.runalg("qgis:clip", path_dir + "Grid.shp", fname, path_res  + "/"+ fname)

#   Use Distance to nearest hub function on point.shp files with the polygon shapefile "Grid.shp" as Hub layer and save files to Result folder  
        if shapefile.wkbType() == 0:
            point_output=processing.runalg("qgis:distancetonearesthub", path_dir + "Grid.shp", fname, 'HubName', 0, 0, path_res  + "/"+ fname)
run()
7
  • So, are you getting 3 as the geometry type of both point and polygon layers? Commented Jan 29, 2015 at 0:11
  • Hi @gcarrillo, yes I get 3 as the geometry type.
    – Joseph
    Commented Jan 29, 2015 at 11:12
  • 1
    Strange @Joseph, I've just tested it and get 0 (point), 1 (line), and 2 (polygon). Can you tell me what you get when you run myLayer.wkbType() for the point layer which gives you geometryType=3? The list of possible values for wkbType() is here. Commented Jan 29, 2015 at 15:20
  • Thank you @gcarrillo, I will give it another go and report back. I have moved on from this problem so it's not too important but would be quite useful to resolve.
    – Joseph
    Commented Jan 29, 2015 at 15:33
  • @gcarrillo I still receive the same when using myLayer.wkbType().
    – Joseph
    Commented Jan 29, 2015 at 16:25

1 Answer 1

3

The problem you are facing is due to not valid layers constructed by QgsVectorLayer(). If you print shapefile.isValid() you will be getting False for all layers.

This happens because you need to set the QGIS prefix path and initialize QgsApplication before using QgsVectorLayer in standalone scripts/apps.

The solution for your problem is to:

  • Add these two lines to your script (below the import section):

    QgsApplication.setPrefixPath("/usr", True) # Adjust it to your path
    QgsApplication.initQgis()
    
  • Adjust the arguments you are passing to QgsVectorLayer to:

    shapefile = QgsVectorLayer( path_dir + "Shapefiles\\" + fname, fname, "ogr" )
    

Now you could print the following layer properties and get appropriate results (0 for points, 1 for lines, and 2 for polygons):

print fname, shapefile.isValid(), shapefile.geometryType()
2
  • 1
    Great! @Joseph, do you agree to change the title to sth like "Getting random values from geometryType() in a standalone PyQGIS script"? I think the post could be more visible for search engines this way. Commented Jan 30, 2015 at 13:47
  • 1
    Agreed and done :)
    – Joseph
    Commented Jan 30, 2015 at 13:56

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.