import sys
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import(
QMainWindow,
QVBoxLayout,
QWidget,
QFileDialog,
QAction
)
from qgis.core import (
QgsApplication,
QgsVectorLayer,
QgsRasterLayer,
QgsProject
)
from qgis.gui import (
QgsMapCanvas,
QgsMapTool
)
class PanTool(QgsMapTool):
def __init__(self,mapCanvas):
super(PanTool,self).__init__(mapCanvas)
self.setCursor(Qt.OpenHandCursor)
self.dragging=False
def canvasMoveEvent(self,event):
if event.buttons() == Qt.LeftButton:
if not self.dragging:
self.dragging=True
self.canvas().panAction(event)
def canvasReleaseEvent(self,event):
if event.button()== Qt.LeftButton and self.dragging:
self.canvas().panActionEnd(event.pos())
self.dragging=False
class MapViewer(QMainWindow):
def __init__(self):
super(MapViewer,self).__init__()
self.setWindowTitle("Map Viewer")
self._layers=[]
self.create_widget()
self.setup_file_menu()
def create_widget(self):
"""
创建布局
"""
self._canvas =QgsMapCanvas()
self._canvas.setCanvasColor(Qt.white)
self._canvas.show()
layout=QVBoxLayout()
layout.addWidget(self._canvas)
contents=QWidget()
contents.setLayout(layout)
self.setCentralWidget(contents)
def setup_file_menu(self):
"""
创建菜单和工具栏
"""
file_menu=self.menuBar().addMenu("文件")
view_menu=self.menuBar().addMenu("视图")
mode_menu=self.menuBar().addMenu("Mode")
open_shpfile_action=self.create_action("打开shp",self.open_shpfile)
open_rasterfile_action=self.create_action("打开tif",self.open_rasterfile)
toolbar_=self.addToolBar("tools")
zoomIn_action=self.create_action("ZoomIn",self.zoomIn,None,'images/mActionZoomIn.svg',"ZoomIn")
zoomOut_action=self.create_action("ZoomOut",self.zoomOut,None,'images/mActionZoomOut.svg',"ZoomOut")
self.pan_action=self.create_action("Pan",self.setPanMode,None,'images/mActionPan.svg',"Pan",True)
self.panTool=PanTool(self._canvas)
self.panTool.setAction(self.pan_action)
self.add_action(file_menu,(open_shpfile_action,open_rasterfile_action,None))
self.add_action(toolbar_,(zoomIn_action,zoomOut_action,self.pan_action))
def open_shpfile(self):
"""打开shp文件"""
filename,_=QFileDialog.getOpenFileName(self,'open shp file','.','(*.shp)')
if filename:
layer=QgsVectorLayer(filename,"layer1","ogr")
if not layer.isValid():
raise IOError("Invalid shapefile")
QgsProject.instance().addMapLayer(layer)
self._layers.append(layer)
self._canvas.setExtent(layer.extent())
self._canvas.setLayers(self._layers)
def open_rasterfile(self):
"""打开tif文件"""
filename,_=QFileDialog.getOpenFileName(self,'open raster file','.','(*.tif)')
if filename:
layer=QgsRasterLayer(filename,"rlayer1")
if not layer.isValid():
raise IOError("Invalid shapefile")
QgsProject.instance().addMapLayer(layer)
self._layers.append(layer)
self._canvas.setExtent(layer.extent())
self._canvas.setLayers(self._layers)
def create_action(self,text,slot=None,shotcut=None,icon=None,tip=None,checkable=False,signal='triggered'):
"""
创建QAction
"""
action=QAction(text,self)
if icon is not None:
action.setIcon(QIcon(icon))
if shotcut is not None:
action.setShortcut(shotcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
getattr(action,signal).connect(slot)
if checkable:
action.setCheckable(True)
return action
def add_action(self,target,actions):
"""
menu and action
"""
for action in actions:
if action is None:
target.addSeparator()
else:
target.addAction(action)
def zoomIn(self):
'''
放大
'''
self._canvas.zoomIn()
def zoomOut(self):
"""
缩小
"""
self._canvas.zoomOut()
def setPanMode(self):
"""
平移
"""
self.pan_action.setChecked(True)
self._canvas.setMapTool(self.panTool)
def setExploreMode(self):
"""
查询
"""
pass
if __name__=="__main__":
QgsApplication.setPrefixPath("qgis",True)
qgs=QgsApplication([],True)
qgs.initQgis()
viewer=MapViewer()
viewer.show()
exitCode=qgs.exec()
qgs.exitQgis()
