Skip to content

Instantly share code, notes, and snippets.

@michaeltryby
Created September 27, 2018 12:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeltryby/f034273af2c884402cc77dac82f5448a to your computer and use it in GitHub Desktop.
Save michaeltryby/f034273af2c884402cc77dac82f5448a to your computer and use it in GitHub Desktop.
PyQGIS ShapeViewer Demo
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QVBoxLayout
from PyQt5.QtGui 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 = os.getenv("QGISHOME")
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.show()
self.canvas.setCanvasColor(Qt.white)
self.canvas.enableAntiAliasing(True)
# Lay our widgets out in the main window using a
# vertical box layout
self.layout = QVBoxLayout(self.frame)
self.layout.addWidget(self.canvas)
# layout is set - open a layer
# Add an OGR layer to the map
filename, filetype = QFileDialog.getOpenFileName(self,
"Open Shapefile", ".", "Shapefiles (*.shp)")
#fileInfo = QFileInfo(file)
# Add the layer
layer = QgsVectorLayer(filename, "test", "ogr")
if not layer.isValid():
return
QgsProject.instance().addMapLayer(layer)
self.canvas.setExtent(layer.extent())
self.canvas.setLayers([layer])
layer.refresh()
self.canvas.show()
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)
@KimarStarima
Copy link

that's great !!!
your code is the best for the standalone PyQgis sample.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment