1

I've been following along the examples from the book "Building Mapping Applications with QGIS and for the life of me I can't figure out why my shapefiles aren't loading and displaying in the app window under a Ubuntu system. The same code runs fine with my Macbook Pro setup.

I use a run.sh file to set up the linux environment:

export PYTHONPATH="/usr/share/qgis/python"
export LD_LIBRARY_path="/usr/share/qgis/lib"
export QGIS_PREFIX="/usr/share/qgis"
python externalApp.py

I have tested that I can import all qgis, qgis.core, qgis.gui etc files without errors in Python.

The Linux script I'm using is pasted below. I have played around with the shapefile name and location.

import sys
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt

#############################################################################

class MapViewer(QMainWindow):
    def __init__(self, shapefile):
        QMainWindow.__init__(self)
        self.setWindowTitle("Map Viewer")

        canvas = QgsMapCanvas()
        canvas.useImageToRender(False)
        canvas.setCanvasColor(Qt.white)
        canvas.show()

        layer = QgsVectorLayer(shapefile, "layer1", "ogr")
        #if not layer.isValid():
        #    raise IOError("Invalid shapefile")

        QgsMapLayerRegistry.instance().addMapLayer(layer)
        canvas.setExtent(layer.extent())
        canvas.setLayerSet([QgsMapCanvasLayer(layer)])

        layout = QVBoxLayout()
        layout.addWidget(canvas)

        contents = QWidget()
        contents.setLayout(layout)
        self.setCentralWidget(contents)

#############################################################################

def main():
    """  Our main program.
    """
    QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
    QgsApplication.initQgis()

    app = QApplication(sys.argv)

    viewer = MapViewer("/home/cary/workspace/temp/alaska.shp")
    viewer.show()

    app.exec_()

    QgsApplication.exitQgis()

#############################################################################

if __name__ == "__main__":
    main()

Computers used: 1) Toshiba Satellite windows machine, reconfigured with ubuntu 14.04 2) Macbook Pro computer

The Toshiba Satellite machine was a new machine and newly configured with Ubuntu. I basically just installed Ubuntu then updated the system and then installed QGIS with this script:

sudo sh -c 'echo "deb http://qgis.org/debian trusty main" >> /etc/apt/sources.list'  

sudo sh -c 'echo "deb-src http://qgis.org/debian trusty main" >> /etc/apt/sources.list'  

gpg --keyserver keyserver.ubuntu.com --recv 47765B75  

gpg --export --armor 47765B75 | sudo apt-key add -  

sudo apt-get update && sudo apt-get install qgis python-qgis  

Setup for both computers: Both computers are using Python 2.7.6

Standard setups for both of the machines. I pretty much just ran the script above for the Linux box.

Verified that the "/usr/share/qgis" folders were setup in Linux properly Verified that the structures on the Mac all matched up with the description of folders and contents from the book on page 21 for setting up PYTHONPATH, QGIS_PREFIX etc...

The Linux file structures were different: in the book: PYTHONPATH="/usr/share/qgis/build/output/python/" while I noticed on my system that the "/build/output/" directories were missing.

The same code worked on the Mac setup but not the Linux setup. I also verified that the path to the shapefiles were correct for the linux system.

Fortunately the Mac setup works and I can continue with the rest of the tutorial on that box but I would love to know why the Ubuntu box doesn't work.

  • Perhaps you could at least tell us what happens instead of what you expected (e.g. does it crash, do you get error messages) and what debugging you've already tried? Perhaps saying what version of QGIS might be useful, and maybe you could simplify the code to be a more minimal example. – BradHards Jul 31 '15 at 22:41
  • It doesn't crash at all. The app window appears and it's just an empty frame. The terminal code that comes up is in the header of this post. I've tried this with both 2.9 and 2.10 versions of QGIS and the Ubuntu version is 14.04. BTW: the very same code runs perfectly in my Mac OSX computer. So I'm having a tough time trying to figure out why it won't run on Linux. Also I've tried this on two different machines....One of them was on a brand new computer with fresh installs of Ubuntu, Eclipse and QGIS – Cary Correia Jul 31 '15 at 23:47
  • 1
    Please edit the question. The part about running on one computer and not another is probably important, so perhaps you can say more about how you installed on Linux (in particular, exact locations, python versions, etc). – BradHards Aug 1 '15 at 1:33
  • 2
    Your edits still don't describe that the setup works on the mac. Assume that anything in comments could be hidden. Can you confirm ~/workspace/temp/ne_10m_populated_places.shp exists and is readable on the Linux box? – BradHards Aug 1 '15 at 4:57
  • 1
    I have tested your code on my Ubuntu-based computer, the only strange thing I see is a QGIS prefix that doesn't work for the common QGIS installation. Try changing your QGIS_PREFIX variable to "/usr". If that works I can tell you why "/usr" is the QGIS prefix. In fact, by using your QGIS_PREFIX I also get an empty frame. – Germán Carrillo Aug 1 '15 at 14:59
4

I have tested your code on my Ubuntu-based computer, the only strange thing I see is a QGIS prefix that doesn't work for the common QGIS installation on GNU/Linux.

Try changing your QGIS_PREFIX variable to "/usr". The QGIS prefix is where QGIS can start to look for data providers and other resources (see Failed to create memory layers in QGIS application on Linux for details). In fact, by using your QGIS_PREFIX I also get an empty frame.

  • 2
    And "/usr" it was! Worked right away for me, thanks! – heltonbiker Mar 22 '16 at 1:48
  • QgsApplication.setPrefixPath('/usr', True) fixed it for me. – Ben Mar 19 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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