3

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?

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

4
  • 1
    Are you sure the script is in your python path? Traceback is very clear about not being able to find such a module. Commented May 9, 2014 at 6:56
  • @zimmi, traceback is informing a problem in line not in directory?! So, the script must be saved in the directory of utils.py or not necessary? Commented May 9, 2014 at 15:31
  • I do not know, but I am pretty sure python just cannot find the module you are asking for. You might want to try to copy it to the folder you mentioned and see what happens. Commented May 9, 2014 at 15:35
  • I would start by confirming that you are putting the module in the correct directory. In the python console you can write something like import os then print(os.getcwd()). That way you can make sure that you are putting the files in the right directory.
    – krishnab
    Commented May 9, 2014 at 16:03

1 Answer 1

4

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.

2
  • I update the question and I ask you to see what happens. Commented May 11, 2014 at 1:21
  • see the update in may answer
    – gene
    Commented May 12, 2014 at 6:31

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.