[GIS] How to open a shapefile using Python in QGIS

openlayers-2pythonqgisshapefile

I have tried to open my shape files with the help of a tutorial (http://spatialgalaxy.net/2012/01/27/qgis-running-scripts-in-the-python-console/) but unfortunately I couldn't open my shapefiles for my first work in QGIS. I put the directory of the shapes and the script in my python path how the tutorial recommended. So above are two pictures showing the problem by script and in command prompt:

enter image description here

enter image description here

Are there any other solutions (in a form of python language) to open shapefiles? I am having headeches because when I'm trying to do like the website exposes I couldn't open the shaoes and doing the commands of importing Loader I got messages of errors. Hope someone here help me!

///////////////////////////////////////////////////////////////////////////////////////////

I have tried this afertnoon reproduce all the process dicussed here. I put the the script in the folder of my python path I think is correct. As I said any times later when I use the command "sys.path" many directories appears showing where are my "python paths" and after that I resolve put the script in "C:\OSGeo4W\bin" but I am not sure if here is the correct local.

The photo below show what happens when I try to load the shapefiles by python:

enter image description here

@gene, I follow your instruction, but how I update the question a mistake persists in the running process. First I have many "python paths" and I don't know where do I have to put the script, second when I use the command ldr.load_shapefiles('F:/qgis_1/exercicio_buffer_L1') I recive an error message like this in the photo.

What do I have to do?

///////////////////////////////////////////////////////////////////////////////////////////

Best Answer

I you read the documentation of the script:

"""Load all shapefiles in a given directory.
  This script (loader.py) runs from the QGIS Python console.
  From the console, use:
    from loader import Loader
    ldr = Loader(qgis.utils.iface)
    ldr.load_shapefiles('/my/path/to/shapefile/directory')"""

You need to import the script (interest of the Class Loader()) and not run the script with:

execfile("your_python_script.py")

because in this case you run an external script (Python does not know where to find QGIS) and it is the same thing as running a PyQGIS script from outside QGIS (Getting dataProvider from vector layer outside QGIS, Standalone applications using QGIS and environment variables or How to run a simple python script for QGIS from outside (e.g. Sublime Text)?)

If the Loader class is in you script teste.py, simply use:

from teste import Loader

like any other Python module, with the script is in your python path

If you still want to run your external script , use the Script runner Plugin of the same author (Gary Sherman) and if you want to simply load a shapefile from the console, read the PyQGIS Cookbook Loading Layers

Update:

To test your script in the console, use the function dir()

The Loader class from QGIS: Running Scripts in the Python Console:

class Loader:
    def __init__(self, iface):
        self.iface = iface
    def load_shapefiles(self, shp_path):
        print "Loading shapes from %s" % path.join(shp_path, "*.shp")
        shps = glob(path.join(shp_path, "*.shp"))
        for shp in shps:
            shpdir, shpfile  = path.split(shp)
            self.iface.addVectorLayer(shp, shpfile, 'ogr')

Now, in the console:

from loader import Loader
dir(Loader)
['__doc__', '__init__', '__module__', 'load_shapefiles']

and the load_shapefiles function is here and you can use it

The error

AttributeError: Loader instance has no attibute 'load_shapefiles'

means that your script is not correct.

Related Question