from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import sys
import os
# Import our GUI
from shapeviewer_gui import Ui_MainWindow
# Environment variable QGISHOME must be set to the install directory
# before running the application
qgis_prefix = "/usr"
class ShapeViewer(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
# Required by Qt4 to initialize the UI
self.setupUi(self)
# Set the title for the app
self.setWindowTitle("ShapeViewer")
# Create the map canvas
self.canvas = QgsMapCanvas()
self.canvas.useImageToRender(False)
self.canvas.show()
# Lay our widgets out in the main window using a
# vertical box layout
self.layout = QVBoxLayout(self.frame)
self.layout.addWidget(self.canvas)
layer = QgsVectorLayer("greece.shp", "greece", "ogr")
if not layer.isValid():
print 'not valid layer'
return
# Add layer to the registry
QgsMapLayerRegistry.instance().addMapLayer(layer);
if layer.isUsingRendererV2():
rendererV2 = layer.rendererV2()
symbols = layer.rendererV2().symbols()
symbol = symbols[0]
symbol.setColor(QColor.fromRgb(192,192,192))
symbol.setAlpha(0.7)
else:
renderer = layer.renderer()
if not hasattr(layer, 'isUsingRendererV2'):
print "You have an old version of QGIS"
# Set extent to the extent of our layer
self.canvas.setExtent(layer.extent())
# Set up the map canvas layer set
cl=QgsMapCanvasLayer(layer)
self.layers = []
self.layers.insert( 0, QgsMapCanvasLayer(cl) )
self.canvas.setLayerSet( self.layers )
self.canvas.setCanvasColor(QColor(213,235,255))
def main(argv):
# create Qt application
app = QApplication(argv)
# Initialize qgis libraries
QgsApplication.setPrefixPath(qgis_prefix, True)
QgsApplication.initQgis()
# create main window
wnd = ShapeViewer()
# Move the app window to upper left
wnd.move(100,100)
wnd.show()
# run!
retval = app.exec_()
# exit
QgsApplication.exitQgis()
sys.exit(retval)
if __name__ == "__main__":
main(sys.argv)