python 搭建一个gis界面 有一个按钮可以选择本地shp文件进行加载显示在地图中 并有工具按钮可以绘制点、线、面
时间: 2024-06-12 17:06:02 浏览: 18
要搭建一个GIS界面,需要使用一些Python GIS库和框架。其中,最常用的是QGIS API和PyQt5。QGIS API是一个开源GIS软件,可以用来读取和处理GIS数据。PyQt5是一个GUI框架,可以用来设计和构建GIS应用程序的用户界面。
以下是一个简单的示例程序,演示如何使用QGIS API和PyQt5来搭建一个GIS界面,其中包含一个按钮可以选择本地shp文件进行加载显示在地图中,并有工具按钮可以绘制点、线、面:
```python
from qgis.core import *
from qgis.gui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class GISWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 初始化QGIS应用程序
QgsApplication.setPrefixPath('/usr', True)
self.qgs = QgsApplication([], False)
self.qgs.initQgis()
# 创建地图窗口和图层
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(Qt.white)
self.layer = None
self.canvas.setExtent(QgsRectangle(-180, -90, 180, 90))
self.setCentralWidget(self.canvas)
# 创建菜单栏和工具栏
self.createMenuBar()
self.createToolBar()
# 设置窗口大小和标题
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('GIS')
def createMenuBar(self):
# 创建菜单栏
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
# 添加打开文件选项
openFile = QAction('Open File', self)
openFile.triggered.connect(self.openFile)
fileMenu.addAction(openFile)
def createToolBar(self):
# 创建工具栏
toolbar = self.addToolBar('Tools')
# 添加绘制点、线、面工具
pointAction = QAction(QIcon('icons/point.png'), 'Point', self)
pointAction.triggered.connect(self.drawPoint)
toolbar.addAction(pointAction)
lineAction = QAction(QIcon('icons/line.png'), 'Line', self)
lineAction.triggered.connect(self.drawLine)
toolbar.addAction(lineAction)
polygonAction = QAction(QIcon('icons/polygon.png'), 'Polygon', self)
polygonAction.triggered.connect(self.drawPolygon)
toolbar.addAction(polygonAction)
def openFile(self):
# 打开文件对话框,选择shp文件
fileName, _ = QFileDialog.getOpenFileName(self, 'Open File', '.', 'Shapefile (*.shp)')
# 如果选择了文件,加载shp文件到地图中
if fileName:
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
self.layer = QgsVectorLayer(fileName, baseName, 'ogr')
QgsProject.instance().addMapLayer(self.layer)
self.canvas.setExtent(self.layer.extent())
self.canvas.setLayers([self.layer])
self.canvas.refresh()
def drawPoint(self):
# 创建绘制点工具,并启动
self.tool = QgsMapToolEmitPoint(self.canvas)
self.canvas.setMapTool(self.tool)
def drawLine(self):
# 创建绘制线工具,并启动
self.tool = QgsMapToolEmitLine(self.canvas)
self.canvas.setMapTool(self.tool)
def drawPolygon(self):
# 创建绘制面工具,并启动
self.tool = QgsMapToolEmitPolygon(self.canvas)
self.canvas.setMapTool(self.tool)
if __name__ == '__main__':
app = QApplication([])
window = GISWindow()
window.show()
app.exec_()
```
在上述代码中,我们首先创建了一个GISWindow类,继承自QMainWindow。在initUI()方法中,我们初始化了QGIS应用程序,创建了地图窗口和图层,并创建了菜单栏和工具栏。在菜单栏中,我们创建了一个打开文件选项,用于选择本地shp文件进行加载。在工具栏中,我们创建了绘制点、线、面工具,用于在地图中绘制点、线、面。
在openFile()方法中,我们使用QFileDialog打开文件对话框,选择shp文件。如果选择了文件,则将其加载到地图中,并设置地图的范围和图层。在drawPoint()、drawLine()和drawPolygon()方法中,我们分别创建了绘制点、线、面工具,并启动它们。
最后,在if __name__ == '__main__'中,我们创建了一个QApplication对象和GISWindow对象,并显示窗口。当应用程序退出时,QGIS应用程序也会退出。
相关推荐
![tbx](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)