5.3 地图工具Map Tool使用

前言

  • 介绍地图工具Map Tool的使用
  • 说明:文章中的示例代码均来自开源项目pyqgis示例大全qgis_py_api_apps

QGis软件中的地图工具

  • QGis有多种地图工具,如下图

在默认情况下,地图视图处于平移模式,这种模式也最常用。在该模式下,不但可以使用鼠标左键拖曳平移地图,也可以通过鼠标滚轮放大或缩小地图,还可以直接通过单击的方式定位地图。另外,按住Ctrl键的同时滚动鼠标滚轮,可以更精细地控制地图缩放的比例尺。

pyqgis中地图工具

QgsMapTool

  • pyqgis文档QgsMapTool定义如下图

  • QgsMapTool类图如下图

  • QgsMapToolPan即为平移工具

  • sample_mapcanvas_maptool_layertree为例,介绍pyqgis中如何使用QgsMapcanvas,整体效果如下图所示

  • 示例工程中添加了五个maptool

  • 代码结构如下图

  • 在mymainwindow.py构造函数MainWindow中创建map tool

    class MainWindow(QMainWindow, Ui_MainWindow):
    def init(self):
    super(MainWindow, self).init()
    self.setupUi(self)
    ......
    # 添加maptool
    self.gsMapToolPan = QgsMapToolPan(self.gsMapCanvas)
    self.gsMapToolZoomIn = QgsMapToolZoom(self.gsMapCanvas,False)
    self.gsMapToolZoomOut = QgsMapToolZoom(self.gsMapCanvas,True)
    self.gsMapToolIdentifyFeature = QgsMapToolIdentifyFeature(self.gsMapCanvas)
    self.gsMapToolIdentifyFeature.featureIdentified.connect(self.identify_callback)
    self.gsMapToolRectangleMapTool = RectangleMapTool(self.gsMapCanvas)
    self.gsMapCanvas.setMapTool(self.gsMapToolPan)
    # 添加ToolBar用于切换Maptool
    tb = self.addToolBar('MapTools')
    self.actionPan = QAction(QIcon(':/images/mActionPan.png'),'Pan',self)
    self.actionPan.setCheckable(True)
    self.actionPan.setChecked(True)
    self.actionZoomIn = QAction(QIcon(':/images/mActionZoomIn.png'),'ZoomIn',self)
    self.actionZoomIn.setCheckable(True)
    self.actionZoomOut = QAction(QIcon(':/images/mActionZoomOut.png'),'ZoomOut',self)
    self.actionZoomOut.setCheckable(True)
    self.actionIdentifyFeature = QAction(QIcon(':/images/mActionIdentify.png'),'Identify',self)
    self.actionIdentifyFeature.setCheckable(True)
    self.actionRectangle = QAction(QIcon(':/images/mActionAddBasicRectangle.png'),'Rectangle',self)
    self.actionRectangle.setCheckable(True)
    tb.addAction(self.actionPan)
    tb.addAction(self.actionZoomIn)
    tb.addAction(self.actionZoomOut)
    tb.addAction(self.actionIdentifyFeature)
    tb.addAction(self.actionRectangle)
    tb.actionTriggered[QAction].connect(self.toolbtnpressed)

  • self.gsMapToolPan = QgsMapToolPan(self.gsMapCanvas)创建pan map tool

  • self.gsMapCanvas.setMapTool(self.gsMapToolPan)用于设置当前map tool

  • 其他map tool类似

编写自定义地图工具

You can write your custom tools, to implement a custom behavior to actions performed by users on the canvas.
Map tools should inherit from the QgsMapTool, class or any derived class, and selected as active tools in the canvas using the setMapTool() method as we have already seen.
Here is an example of a map tool that allows to define a rectangular extent by clicking and dragging on the canvas. When the rectangle is defined, it prints its boundary coordinates in the console. It uses the rubber band elements described before to show the selected rectangle as it is being defined.

  • 翻译如下

    您可以编写自定义工具,以实现对用户在画布上执行的操作的自定义行为。

    地图工具应继承自QgsMapTool类或任何派生类,并使用setMapTool()方法如我们之前所见那样,被选为画布上的活动工具。

    以下是一个地图工具的示例,该工具允许通过在画布上点击和拖动来定义一个矩形范围。当矩形被定义后,它会在控制台中打印其边界坐标。它使用了之前描述的橡皮筋元素来在定义矩形时显示所选的矩形。

  • 示例项目sample_mapcanvas_maptool_layertree中,文件rectanglemaptool.py是pyqgis提供的示例map tool,效果如下

  • 关键函数如下

  1. activate
    called when set as currently active map tool
  2. deactivate
    called when map tool is being deactivated
  3. canvasDoubleClickEvent
    Mouse double-click event for overriding.
  4. canvasMoveEvent
    Mouse move event for overriding.
  5. canvasPressEvent
    Mouse press event for overriding.
  6. canvasReleaseEvent
    Mouse release event for overriding.

总结

  • 介绍了地图工具的使用
相关推荐
GIS阵地4 个月前
Warning 1: PROJ: proj_create_from_database
数据库·c++·mybatis·qgis·开源gis·pyqgis
GIS阵地5 个月前
如何统计QGIS里栅格图层的面积呢
c++·qgis·开源gis·pyqgis
GIS阵地10 个月前
CSV转换为QGIS的简单分类符号
arcgis·二次开发·qgis·地理信息系统·pyqgis
GIS阵地10 个月前
在QGIS中将多个图层导出到一个geojson里 gdb转geojson qgis强大的格式转换工具
二次开发·qgis·地理信息系统·pyqgis