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.
~/workspace/temp/ne_10m_populated_places.shpexists and is readable on the Linux box? – BradHards Aug 01 '15 at 04:57