【问题标题】:Auto Refresh in ArcGIS using Python使用 Python 在 ArcGIS 中自动刷新
【发布时间】:2013-05-11 20:13:16
【问题描述】:

我正在尝试为 ArcMap 创建一个“自动刷新”工具,以刷新 DataFrame。我相信版本 10 有一个您可以为此目的下载的附加组件。但是我们在工作中运行的是 10.1,并且没有这样的工具。

EDIT wxPython 的计时器应该可以工作,但是在 arc 中使用 wx 很棘手。这是当前代码的样子:

import arcpy
import pythonaddins
import os
import sys
sMyPath = os.path.dirname(__file__)
sys.path.insert(0, sMyPath)

WATCHER = None

class WxExtensionClass(object):
    """Implementation for Refresher_addin.extension (Extension)"""
    _wxApp = None
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
    def startup(self):
        from wx import PySimpleApp
        self._wxApp = PySimpleApp()
        self._wxApp.MainLoop()
        global WATCHER
        WATCHER = watcherDialog()


class RefreshButton(object):
    """Implementation for Refresher_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)

您会注意到其中的 PySimpleApp 内容。我是从 Cederholm 的演讲中得到的。我想知道我是否误会了什么。我应该为扩展创建一个完全独立的插件吗?那么,用我需要的代码创建我的工具栏/栏插件吗?我问这个是因为我没有看到下面代码中引用的 PySimpleApp,或者在启动覆盖方法中从 wx 导入的任何内容......我认为这是必需的/所有这一切的重点。我很感激你的帮助。请告诉我您在我的代码中看到的内容。

【问题讨论】:

  • 您将需要某种并发,time.sleep 会阻塞并锁定 Arcmap。

标签: python arcgis


【解决方案1】:

您不能按照您尝试的方式执行此操作,因为time.sleep 会阻塞并锁定整个应用程序。 ArcGIS 中的 Python 插件是相当新的东西,还有很多功能尚未实现。其中之一是某种更新或计时器事件,就像您在 .NET 和 ArcObjects 中获得的一样。在这种情况下,您可能会考虑使用 threading.Thread 和 threading.Event,但在 Python 插件环境中与线程无关。至少我不能让它工作。所以我在这种情况下所做的就是使用 wxPython 和 Timer 类。如果插件设置正确,下面的代码将起作用。

import time
import os, sys
import wx
import arcpy

mp = os.path.dirname(__file__)
sys.path.append(mp)

WATCHER = None

class LibLoader1(object):
    """Extension Implementation"""
    def __init__(self):
        self.enabled = True

    def startup(self):
        global WATCHER
        WATCHER = watcherDialog()

class ButtonClass5(object):
    """Button Implementation"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)

制作一个带有工具栏和按钮类的扩展插件。覆盖扩展的startup 方法,如上所示。这将创建一个带有计时器的 Frame 子类的实例。然后,每当您单击工具栏上的按钮时,计时器就会打开或关闭。 Timer 参数以毫秒为单位,因此显示的代码将每 5 秒刷新一次。

您可以在插件here 中阅读有关使用 wxPython 的更多信息。特别注意 MCederholm 的帖子,比如关于 print 语句不起作用的帖子。

编辑

代码使用插件扩展类的startup 方法覆盖。此方法应该在 Arcmap 启动时运行,但从您的 cmets 看来,此启动方法在启动时无法运行。如果您没有正确创建插件,这是可能的,但在我的测试中它对我来说很好。如果您继续收到“AttributeError: 'NoneType' object has no attribute 'timer'”,请更改按钮类的 onClick 方法,如下所示:

def onClick(self):

    if WATCHER is None:
        global WATCHER
        WATCHER = watcherDialog()

    if not WATCHER.timer.IsRunning():
        WATCHER.timer.Start(5000)
    else:
        WATCHER.timer.Stop()

前 3 行检查以确保 WATCHER 变量已设置为 watcherDialog 的实例,并且仍未设置为 None。不知道为什么您的启动方法没有运行,但希望这会为您解决问题。

【讨论】:

  • 太棒了!您提供的代码看起来很棒..谢谢!我一直在努力让 wx 在 ArcMap 的 python 窗口中导入。不确定它是否与我安装的 wxPython 有关。我已经将它专门安装在 ArcGIS10.1\lib 中,但是,尝试导入 wx 时总是出错。我即将观看 Mark Cederholm 的 2012 年开发者峰会演示文稿,该演示文稿是从您引用的链接中获得的。 (见我的另一个问题:stackoverflow.com/questions/16184829/…
  • 另外我得到的错误是:运行时错误 Traceback(最近一次调用最后):文件“”,第 1 行,在 文件“C:\Python27\ArcGIS10.1 \lib\site-packages\wx-2.8-msw-unicode\wx_init_.py”,第 45 行,在 中来自 wx._core import * 文件“C:\Python27\ArcGIS10. 1\lib\site-packages\wx-2.8-msw-unicode\wx_core.py",第 4 行,在 import core ImportError: DLL load failed: %1 is not a valid Win32应用程序。
  • 尝试在python窗口中导入wx,或者使用启动方法在插件扩展中导入wx时是否出现此错误?您必须完全按照 Cederholm 的说明进行操作,否则您会遇到类似这样的各种错误。在带有启动方法的扩展中导入 wx,你应该已经在路上了。
  • OK - 终于让扩展显示在 Arc 中,并且按钮似乎想要很好地播放。我得到一个回溯:第 29 行,onClick 如果不是 WATCHER.timer.IsRunning(): AttributeError: 'NoneType' object has no attribute 'timer'
  • 您需要进入插件管理器并加载插件。然后重新启动 Arcmap。这样做一次,它应该会自动加载到那台机器上。
【解决方案2】:

您可以使用RefreshTOCRefreshActiveView 方法。只需添加timer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2013-03-11
    • 2010-10-05
    • 1970-01-01
    相关资源
    最近更新 更多