【问题标题】:How to select and zoom to a layer in QGIS using Python如何使用 Python 在 QGIS 中选择和缩放到图层
【发布时间】:2019-08-12 12:36:34
【问题描述】:

我使用的是 QGIS 2.8.1,我想选择名为“tempshpfile”的 shapefile 并缩放到该多边形 shapefile 上的图层。

我的代码是:

import ogr,os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
import qgis.utils
import glob
import processing
from processing.core.Processing import Processing
from PyQt4.QtCore import QTimer

Processing.initialize()
Processing.updateAlgsList()

# Add array of address below

allFiles = ["C:/Shapefiles/Map_0077421.shp"]
filesLen = len(allFiles)

TexLayer = "C:/Texas_NAD27/Texas_NAD27.shp"

for lop in range(filesLen):
    wb = QgsVectorLayer(allFiles[lop], 'tempshpfile', 'ogr')
    wbTex = QgsVectorLayer(TexLayer, 'TexasGrid', 'ogr')
    QgsMapLayerRegistry.instance().addMapLayer(wb)
    QgsMapLayerRegistry.instance().addMapLayer(wbTex)

【问题讨论】:

标签: python pyqt pyqt4 qgis


【解决方案1】:

由于您的wb 层已经注册,只需在循环后激活该层:

qgis.utils.iface.setActiveLayer(wb)

并缩放到活动层的范围:

qgis.utils.iface.zoomToActiveLayer()

【讨论】:

  • 谢谢,您的解决方案有效,但我发现我需要先使用 QTimer 添加等待时间,让 python 加载 shapefile。
猜你喜欢
  • 2015-10-09
  • 2018-01-15
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多