简体   繁体   English

QGIS渲染shapefile时如何让Python等待

[英]How to make Python wait while QGIS renders a shapefile

I have written code for my QGIS 2.8.1 which can successfully take screenshot of one shape file, but unfortunately it doesn't work when I try with multiple shapefiles.我已经为我的QGIS 2.8.1编写了代码,它可以成功地截取一个形状文件的屏幕截图,但不幸的是,当我尝试使用多个形状文件时它不起作用。

So basically if I replace allFiles = ["C:/Shapefiles/Map_00721.shp"] in the code below with allFiles = ["C:/Shapefiles/Map_00721.shp", "C:/Shapefiles/Map_00711.shp", "C:/Shapefiles/Map_00731.shp", "C:/Shapefiles/Map_00791.shp", "C:/Shapefiles/Map_00221.shp"] , the loop iterates over the array without waiting for the rendering and snapshot process to happen.因此,基本上,如果我更换allFiles = ["C:/Shapefiles/Map_00721.shp"]在下面的代码与allFiles = ["C:/Shapefiles/Map_00721.shp", "C:/Shapefiles/Map_00711.shp", "C:/Shapefiles/Map_00731.shp", "C:/Shapefiles/Map_00791.shp", "C:/Shapefiles/Map_00221.shp"] ,循环遍历数组而不等待渲染和快照过程发生。

I have tried using time.sleep in the code below, but it stops the rendering of shapefiles too and the result doesn't came as expected.我曾尝试在下面的代码中使用time.sleep ,但它也停止了 shapefile 的渲染,结果没有达到预期。

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

Processing.initialize()
Processing.updateAlgsList()

OutputFileName = "ABC"    # Temprory global placeholder for filename
canvas = iface.mapCanvas()


def startstuffs():
    qgis.utils.iface.zoomToActiveLayer()    # Zoom to Layer
    scale=canvas.scale()    # Get current Scale
    scale =  scale * 1.5
    canvas.zoomScale(scale)   # Zoomout a bit
    QTimer.singleShot(2000,saveImg)   # Jump to save img

def saveImg():
    qgis.utils.iface.mapCanvas().saveAsImage(OutputFileName)
    QgsMapLayerRegistry.instance().removeAllMapLayers()


# Add array of address below
allFiles = ["C:/Shapefiles/Map_00721.shp"]
filesLen = len(allFiles)

TexLayer = "C:/US_County_NAD27.shp"

for lop in range(filesLen):

    currentShpFile = allFiles[lop]
    currentShpFileName = currentShpFile.strip("C:/Shapefiles/")
    OutputFileName = "C:/ImageOut/" + currentShpFileName + ".png"
    wb = QgsVectorLayer(currentShpFile, currentShpFileName, 'ogr')
    wbTex = QgsVectorLayer(TexLayer, 'CountyGrid', 'ogr')
    QgsMapLayerRegistry.instance().addMapLayer(wb)    # Add the shapefile
    QgsMapLayerRegistry.instance().addMapLayer(wbTex)    # Add the county shapefile
    qgis.utils.iface.setActiveLayer(wb)   # Makes wb as active shapefile

    QTimer.singleShot(3000, startstuffs)    # This start stuffs

print "Done!"

Avoid using time.sleep() since that will completely stall your entire program.避免使用time.sleep()因为这会完全停止你的整个程序。 Instead, use processEvents() which allows your program to render in the background.相反,使用processEvents()允许您的程序在后台呈现。

import time

def spin(seconds):
    """Pause for set amount of seconds, replaces time.sleep so program doesn't stall"""

    time_end = time.time() + seconds
    while time.time() < time_end:
        QtGui.QApplication.processEvents()

This method should work fine for a quick fix, but in the long term it may generate difficult problems to track.这种方法应该可以快速修复,但从长远来看,它可能会产生难以跟踪的问题。 It is better to use a QTimer with a event loop for a permanent solution.最好使用带有事件循环的 QTimer 作为永久解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用QGIS / Python将Shapefile中的属性更改为特定值 - How to change an attribute in Shapefile with a certain value with QGIS / Python 复制和粘贴特征或附加shapefile-qGis python - Copy and paste features or append shapefile - qGis python 如何使用QGIS或python gdal库转换ESRI shapefile格式以提取纬度 - How to convert ESRI shapefile format using QGIS or python gdal library to extract lat lon 在独立的Python QGis应用程序中加载图层/形状文件时出错 - Error Loading Layer/Shapefile in a Standalone Python QGis Application Python Multiprocessing-如何使进程在活动时等待? - Python Multiprocessing - how to make processes wait while active? 如何使Python等待输入? - How to make Python Wait for input? 如何让线程在 Python 中等待? - How to make thread wait in Python? 如何在 python 中的 map 上显示 shapefile? - How to show shapefile on map in python? python selenium 网页抓取。 ```while``` 循环中的错误。 如何让代码等待页面加载并重试 - python selenium webscraping. Mistake in ```while``` loop. how to make the code wait for page to load and try again Python-如何使函数在tkinter中等待或使用while循环而不会冻结窗口 - Python- How to make a function wait or use a while loop in tkinter without the window freezing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM